From be5a6593a946442a57ce9bcd87d977c41b954d33 Mon Sep 17 00:00:00 2001 From: Abhishek Radhakrishnan Date: Tue, 13 Jun 2023 18:07:51 -0700 Subject: [PATCH] Reset `RuntimeInfo` to fix flaky test `ParametrizedUriEmitterConfigTest`. (#14405) * Add injector so JVM settings are correctly set up and bound for the test. * Add VisibleForTesting IDE annotation. * spacing --- .../java/org/apache/druid/utils/JvmUtils.java | 2 + .../ParametrizedUriEmitterConfigTest.java | 49 ++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/processing/src/main/java/org/apache/druid/utils/JvmUtils.java b/processing/src/main/java/org/apache/druid/utils/JvmUtils.java index 7a8b68d96fa..493ac901420 100644 --- a/processing/src/main/java/org/apache/druid/utils/JvmUtils.java +++ b/processing/src/main/java/org/apache/druid/utils/JvmUtils.java @@ -19,6 +19,7 @@ package org.apache.druid.utils; +import com.google.common.annotations.VisibleForTesting; import com.google.common.primitives.Ints; import com.google.inject.Inject; @@ -144,6 +145,7 @@ public class JvmUtils /** * Only for testing. */ + @VisibleForTesting public static void resetTestsToDefaultRuntimeInfo() { RUNTIME_INFO = new RuntimeInfo(); diff --git a/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterConfigTest.java b/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterConfigTest.java index 960cbb0932b..53740a79141 100644 --- a/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterConfigTest.java +++ b/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterConfigTest.java @@ -20,8 +20,12 @@ package org.apache.druid.java.util.emitter.core; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.inject.Guice; +import com.google.inject.Injector; import org.apache.druid.java.util.common.Pair; import org.apache.druid.utils.JvmUtils; +import org.apache.druid.utils.RuntimeInfo; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Test; @@ -29,20 +33,40 @@ import java.util.Properties; public class ParametrizedUriEmitterConfigTest { + /** + * The JVM RuntimeInfo is static, and the default settings that depend on it such + * as {@link BaseHttpEmittingConfig#DEFAULT_BATCH_QUEUE_SIZE_LIMIT} are static as well. So bind the runtime info + * for this test, so it doesn't get pawned by other tests setting the runtime to different values. + */ + private static Injector makeInjector(Properties props) + { + return Guice.createInjector( + binder -> { + JvmUtils.resetTestsToDefaultRuntimeInfo(); + binder.bind(RuntimeInfo.class) + .toInstance(JvmUtils.getRuntimeInfo()); + binder.requestStaticInjection(JvmUtils.class); + + final ParametrizedUriEmitterConfig paramConfig = new ObjectMapper().convertValue( + Emitters.makeCustomFactoryMap(props), ParametrizedUriEmitterConfig.class); + final HttpEmitterConfig httpEmitterConfig = paramConfig.buildHttpEmitterConfig("http://example.com/topic"); + binder.bind(HttpEmitterConfig.class).toInstance(httpEmitterConfig); + } + ); + } + + @AfterClass + public static void teardown() + { + JvmUtils.resetTestsToDefaultRuntimeInfo(); + } + @Test public void testDefaults() { - final Properties props = new Properties(); + final Injector injector = makeInjector(new Properties()); + final HttpEmitterConfig config = injector.getInstance(HttpEmitterConfig.class); - final ObjectMapper objectMapper = new ObjectMapper(); - final ParametrizedUriEmitterConfig paramConfig = objectMapper.convertValue(Emitters.makeCustomFactoryMap(props), ParametrizedUriEmitterConfig.class); - final HttpEmitterConfig config = paramConfig.buildHttpEmitterConfig("http://example.com/topic"); - - Assert.assertEquals(60000, config.getFlushMillis()); - Assert.assertEquals(500, config.getFlushCount()); - Assert.assertEquals("http://example.com/topic", config.getRecipientBaseUrl()); - Assert.assertNull(config.getBasicAuthentication()); - Assert.assertEquals(BatchingStrategy.ARRAY, config.getBatchingStrategy()); Pair batchConfigPair = BaseHttpEmittingConfig.getDefaultBatchSizeAndLimit( JvmUtils.getRuntimeInfo().getMaxHeapSizeBytes() ); @@ -62,9 +86,8 @@ public class ParametrizedUriEmitterConfigTest props.setProperty("org.apache.druid.java.util.emitter.httpEmitting.maxBatchSize", "4"); props.setProperty("org.apache.druid.java.util.emitter.httpEmitting.flushTimeOut", "1000"); - final ObjectMapper objectMapper = new ObjectMapper(); - final ParametrizedUriEmitterConfig paramConfig = objectMapper.convertValue(Emitters.makeCustomFactoryMap(props), ParametrizedUriEmitterConfig.class); - final HttpEmitterConfig config = paramConfig.buildHttpEmitterConfig("http://example.com/topic"); + final Injector injector = makeInjector(props); + final HttpEmitterConfig config = injector.getInstance(HttpEmitterConfig.class); Assert.assertEquals(1, config.getFlushMillis()); Assert.assertEquals(2, config.getFlushCount());