Test Stability: Fix Global Ruby Runtime Setup

Fixes #7957
This commit is contained in:
Armin 2017-08-09 15:57:44 +02:00 committed by Armin Braun
parent 7c514689bb
commit 10cda63f1d
7 changed files with 41 additions and 29 deletions

View file

@ -13,7 +13,6 @@ import org.joda.time.DateTime;
import org.jruby.RubyString; import org.jruby.RubyString;
import org.jruby.RubySymbol; import org.jruby.RubySymbol;
import org.logstash.ackedqueue.Queueable; import org.logstash.ackedqueue.Queueable;
import org.logstash.bivalues.BiValues;
import org.logstash.bivalues.NullBiValue; import org.logstash.bivalues.NullBiValue;
import org.logstash.ext.JrubyTimestampExtLibrary; import org.logstash.ext.JrubyTimestampExtLibrary;
@ -335,7 +334,7 @@ public final class Event implements Cloneable, Queueable {
*/ */
private void initTag(final String tag) { private void initTag(final String tag) {
final ConvertedList list = new ConvertedList(1); final ConvertedList list = new ConvertedList(1);
list.add(BiValues.RUBY.newString(tag)); list.add(RubyUtil.RUBY.newString(tag));
Accessors.set(data, TAGS_FIELD, list); Accessors.set(data, TAGS_FIELD, list);
} }

View file

@ -0,0 +1,28 @@
package org.logstash;
import org.jruby.Ruby;
/**
* Utilities around interaction with the {@link Ruby} runtime.
*/
public final class RubyUtil {
/**
* Reference to the global {@link Ruby} runtime.
*/
public static final Ruby RUBY = setupRuby();
private RubyUtil() {
}
/**
* Sets up the global {@link Ruby} runtime and ensures the creation of the "LogStash" module
* on it.
* @return Global {@link Ruby} Runtime
*/
private static Ruby setupRuby() {
final Ruby ruby = Ruby.getGlobalRuntime();
ruby.getOrCreateModule("LogStash");
return ruby;
}
}

View file

@ -32,13 +32,13 @@ public final class Rubyfier {
|| input instanceof JrubyTimestampExtLibrary.RubyTimestamp) { || input instanceof JrubyTimestampExtLibrary.RubyTimestamp) {
return (IRubyObject) input; return (IRubyObject) input;
} }
if (input instanceof String) return BiValues.RUBY.newString((String) input); if (input instanceof String) return RubyUtil.RUBY.newString((String) input);
if (input instanceof Double || input instanceof Float) { if (input instanceof Double || input instanceof Float) {
return BiValues.RUBY.newFloat(((Number) input).doubleValue()); return RubyUtil.RUBY.newFloat(((Number) input).doubleValue());
} }
if (input instanceof Timestamp) { if (input instanceof Timestamp) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp( return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, (Timestamp) input RubyUtil.RUBY, (Timestamp) input
); );
} }
if (input instanceof BiValue) return ((BiValue) input).rubyValue(runtime); if (input instanceof BiValue) return ((BiValue) input).rubyValue(runtime);

View file

@ -59,24 +59,24 @@ public final class Valuefier {
return o; return o;
} }
if (o instanceof String) { if (o instanceof String) {
return BiValues.RUBY.newString((String) o); return RubyUtil.RUBY.newString((String) o);
} }
if (o instanceof Float || o instanceof Double) { if (o instanceof Float || o instanceof Double) {
return BiValues.RUBY.newFloat(((Number) o).doubleValue()); return RubyUtil.RUBY.newFloat(((Number) o).doubleValue());
} }
if (o instanceof Timestamp) { if (o instanceof Timestamp) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp( return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, (Timestamp) o RubyUtil.RUBY, (Timestamp) o
); );
} }
if (o instanceof RubyTime) { if (o instanceof RubyTime) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp( return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, new Timestamp(((RubyTime) o).getDateTime()) RubyUtil.RUBY, new Timestamp(((RubyTime) o).getDateTime())
); );
} }
if (o instanceof DateTime) { if (o instanceof DateTime) {
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp( return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
BiValues.RUBY, new Timestamp((DateTime) o) RubyUtil.RUBY, new Timestamp((DateTime) o)
); );
} }
if (o instanceof RubyHash) { if (o instanceof RubyHash) {

View file

@ -3,7 +3,6 @@ package org.logstash.bivalues;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.HashMap; import java.util.HashMap;
import org.jruby.Ruby;
import org.jruby.RubyBignum; import org.jruby.RubyBignum;
import org.jruby.RubyBoolean; import org.jruby.RubyBoolean;
import org.jruby.RubyInteger; import org.jruby.RubyInteger;
@ -50,8 +49,6 @@ public enum BiValues {
return hm; return hm;
} }
public static final Ruby RUBY = Ruby.getGlobalRuntime();
public static final NullBiValue NULL_BI_VALUE = NullBiValue.newNullBiValue(); public static final NullBiValue NULL_BI_VALUE = NullBiValue.newNullBiValue();
private final BiValueType biValueType; private final BiValueType biValueType;

View file

@ -6,7 +6,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.logstash.bivalues.BiValues;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -21,7 +20,7 @@ public class AccessorsTest {
data.put("foo", "bar"); data.put("foo", "bar");
String reference = "foo"; String reference = "foo";
assertEquals( assertEquals(
BiValues.RUBY.newString("bar"), get(ConvertedMap.newFromMap(data), reference) RubyUtil.RUBY.newString("bar"), get(ConvertedMap.newFromMap(data), reference)
); );
} }
@ -39,7 +38,7 @@ public class AccessorsTest {
data.put("foo", "bar"); data.put("foo", "bar");
String reference = "[foo]"; String reference = "[foo]";
assertEquals( assertEquals(
BiValues.RUBY.newString("bar"), get(ConvertedMap.newFromMap(data), reference) RubyUtil.RUBY.newString("bar"), get(ConvertedMap.newFromMap(data), reference)
); );
} }
@ -51,7 +50,7 @@ public class AccessorsTest {
inner.put("bar", "baz"); inner.put("bar", "baz");
String reference = "[foo][bar]"; String reference = "[foo][bar]";
assertEquals( assertEquals(
BiValues.RUBY.newString("baz"), get(ConvertedMap.newFromMap(data), reference) RubyUtil.RUBY.newString("baz"), get(ConvertedMap.newFromMap(data), reference)
); );
} }
@ -73,7 +72,7 @@ public class AccessorsTest {
inner.add("bar"); inner.add("bar");
String reference = "[foo][0]"; String reference = "[foo][0]";
assertEquals( assertEquals(
BiValues.RUBY.newString("bar"), get(ConvertedMap.newFromMap(data), reference) RubyUtil.RUBY.newString("bar"), get(ConvertedMap.newFromMap(data), reference)
); );
} }

View file

@ -6,9 +6,7 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.logstash.bivalues.BiValues;
import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals; import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -16,15 +14,6 @@ import static org.junit.Assert.assertFalse;
public final class EventTest { public final class EventTest {
/**
* Some of these tests require a fully initialized global {@link org.jruby.Ruby} instance
* so we force the creation of the "LogStash" module here.
*/
@BeforeClass
public static void before() {
BiValues.RUBY.getOrCreateModule("LogStash");
}
@Test @Test
public void queueableInterfaceRoundTrip() throws Exception { public void queueableInterfaceRoundTrip() throws Exception {
Event e = new Event(); Event e = new Event();