Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions logstash-core/lib/logstash/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ module Environment
Setting::Boolean.new("metric.collect", true),
Setting::SettingString.new("pipeline.id", "main"),
Setting::Boolean.new("pipeline.system", false),
Setting::PositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum),
Setting::PositiveInteger.new("pipeline.batch.size", 125),
Setting::SettingPositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum),
Setting::SettingPositiveInteger.new("pipeline.batch.size", 125),
Setting::SettingNumeric.new("pipeline.batch.delay", 50), # in milliseconds
Setting::Boolean.new("pipeline.unsafe_shutdown", false),
Setting::Boolean.new("pipeline.reloadable", true),
Expand Down
34 changes: 4 additions & 30 deletions logstash-core/lib/logstash/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,37 +415,11 @@ def coerce(value)
java_import org.logstash.settings.Boolean
java_import org.logstash.settings.SettingNumeric

class Integer < Coercible
def initialize(name, default = nil, strict = true)
super(name, ::Integer, default, strict)
end

def coerce(value)
return value unless value.is_a?(::String)

coerced_value = Integer(value) rescue nil

if coerced_value.nil?
raise ArgumentError.new("Failed to coerce value to Integer. Received #{value} (#{value.class})")
else
coerced_value
end
end
end
java_import org.logstash.settings.SettingInteger

class PositiveInteger < Integer
def initialize(name, default = nil, strict = true)
super(name, default, strict) do |v|
if v > 0
true
else
raise ArgumentError.new("Number must be bigger than 0. Received: #{v}")
end
end
end
end

class Port < Integer
java_import org.logstash.settings.SettingPositiveInteger

class Port < SettingInteger
VALID_PORT_RANGE = 1..65535

def initialize(name, default = nil, strict = true)
Expand Down
4 changes: 2 additions & 2 deletions logstash-core/spec/logstash/queue_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
LogStash::Setting::SettingNumeric.new("queue.checkpoint.writes", 1024),
LogStash::Setting::Boolean.new("queue.checkpoint.retry", false),
LogStash::Setting::SettingString.new("pipeline.id", pipeline_id),
LogStash::Setting::PositiveInteger.new("pipeline.batch.size", 125),
LogStash::Setting::PositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum)
LogStash::Setting::SettingPositiveInteger.new("pipeline.batch.size", 125),
LogStash::Setting::SettingPositiveInteger.new("pipeline.workers", LogStash::Config::CpuCoreStrategy.maximum)
]
end

Expand Down
4 changes: 2 additions & 2 deletions logstash-core/spec/logstash/settings/integer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
require "spec_helper"
require "logstash/settings"

describe LogStash::Setting::Integer do
describe LogStash::Setting::SettingInteger do
subject { described_class.new("a number", nil, false) }
describe "#set" do
context "when giving a number which is not an integer" do
it "should raise an exception" do
expect { subject.set(1.1) }.to raise_error(ArgumentError)
expect { subject.set(1.1) }.to raise_error(java.lang.IllegalArgumentException)
end
end
context "when giving a number which is an integer" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.logstash.settings;

import java.util.function.Predicate;

public class SettingInteger extends Coercible<Integer> {

public SettingInteger(String name, Integer defaultValue) {
super(name, defaultValue, true, noValidator());
}

// constructor used only in tests, but needs to be public to be used in Ruby spec
public SettingInteger(String name, Integer defaultValue, boolean strict) {
super(name, defaultValue, strict, noValidator());
}

// Exposed to be redefined in subclasses
protected SettingInteger(String name, Integer defaultValue, boolean strict, Predicate<Integer> validator) {
super(name, defaultValue, strict, validator);
}

@Override
public Integer coerce(Object obj) {
if (!(obj instanceof String)) {
// it's an Integer and cast
if (obj instanceof Integer) {
return (Integer) obj;
}
// JRuby bridge convert ints to Long
if (obj instanceof Long) {
return ((Long) obj).intValue();
}
} else {
// try to parse string to int
try {
return Integer.parseInt(obj.toString());
} catch (NumberFormatException e) {
// ugly flow control
}
}

// invalid coercion
throw new IllegalArgumentException(coercionFailureMessage(obj));
}

private String coercionFailureMessage(Object obj) {
return String.format("Failed to coerce value to SettingInteger. Received %s (%s)", obj, obj.getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.logstash.settings;

import java.util.function.Predicate;

public class SettingPositiveInteger extends SettingInteger {

public SettingPositiveInteger(String name, Integer defaultValue) {
super(name, defaultValue, true, new Predicate<Integer>() {
@Override
public boolean test(Integer v) {
if (v <= 0) {
throw new IllegalArgumentException("Number must be bigger than 0. Received: " + v);
}
return true;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.logstash.settings;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class SettingIntegerTest {

private SettingInteger sut;

@Before
public void setUp() {
sut = new SettingInteger("a number", null, false);
}

@Test(expected = IllegalArgumentException.class)
public void givenNumberWhichIsNotIntegerWhenSetIsInvokedThrowsException() {
sut.set(1.1);
}

@Test
public void givenNumberWhichIsIntegerWhenSetIsInvokedThenShouldSetTheNumber() {
sut.set(100);

assertEquals(Integer.valueOf(100), sut.value());
}

@Test
public void givenStringWhichIsIntegerThenCoerceCastIntoInteger() {
assertEquals(Integer.valueOf(100), sut.coerce("100"));
}

@Test
public void givenIntegerInstanceThenCoerceCastIntoInteger() {
assertEquals(Integer.valueOf(100), sut.coerce(100));
}

@Test
public void givenLongInstanceThenCoerceCastIntoInteger() {
assertEquals(Integer.valueOf(100), sut.coerce(100L));
}

@Test(expected = IllegalArgumentException.class)
public void givenDoubleInstanceThenCoerceThrowsException() {
sut.coerce(1.1);
}

@Test(expected = IllegalArgumentException.class)
public void givenObjectInstanceThenCoerceThrowsException() {
sut.coerce(new Object());
}
}