This is an automated email from the ASF dual-hosted git repository.

karan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git


The following commit(s) were added to refs/heads/master by this push:
     new 76ac5e47f3b fix flaky test (#17904)
76ac5e47f3b is described below

commit 76ac5e47f3bec10b94d8b825e6d2f0cea9059c8d
Author: Cece Mei <[email protected]>
AuthorDate: Sat Apr 12 05:25:37 2025 -0700

    fix flaky test (#17904)
    
    This test can fail when the emitting process (emit/flush) took 0ms (could 
be the clock issue), the fix is to make sure fillTimeMs is at least 1 .
---
 .../java/util/emitter/core/HttpEmitterTest.java    | 59 +++++++++++++---------
 1 file changed, 34 insertions(+), 25 deletions(-)

diff --git 
a/processing/src/test/java/org/apache/druid/java/util/emitter/core/HttpEmitterTest.java
 
b/processing/src/test/java/org/apache/druid/java/util/emitter/core/HttpEmitterTest.java
index 624ad8ab50b..32f33ffcd76 100644
--- 
a/processing/src/test/java/org/apache/druid/java/util/emitter/core/HttpEmitterTest.java
+++ 
b/processing/src/test/java/org/apache/druid/java/util/emitter/core/HttpEmitterTest.java
@@ -26,10 +26,11 @@ import org.asynchttpclient.Request;
 import org.asynchttpclient.Response;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
+import org.junit.Assume;
 import org.junit.Before;
 import org.junit.Test;
 
-import java.io.IOException;
+import java.lang.reflect.Field;
 import java.time.Duration;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -65,35 +66,43 @@ public class HttpEmitterTest
   }
 
   @Test
-  public void timeoutEmptyQueue() throws IOException, InterruptedException
+  public void timeoutEmptyQueue() throws Exception
   {
-    float timeoutAllowanceFactor = 2.0f;
+    // In HttpPostEmitter, when batch is empty, the timeout is 
lastBatchFillTimeMillis * config.httpTimeoutAllowanceFactor, and 
lastBatchFillTimeMillis is at least 1.
+    double timeoutAllowanceFactor = 2.0d;
     final HttpEmitterConfig config = new 
HttpEmitterConfig.Builder("http://foo.bar";)
         .setBatchingStrategy(BatchingStrategy.ONLY_EVENTS)
-        .setHttpTimeoutAllowanceFactor(timeoutAllowanceFactor)
+        .setHttpTimeoutAllowanceFactor((float) timeoutAllowanceFactor)
         .setFlushTimeout(BaseHttpEmittingConfig.TEST_FLUSH_TIMEOUT_MILLIS)
         .build();
-    try (final HttpPostEmitter emitter = new HttpPostEmitter(config, 
httpClient, OBJECT_MAPPER)) {
-      long startMs = System.currentTimeMillis();
-      emitter.start();
-      emitter.emitAndReturnBatch(new IntEvent());
-      emitter.flush();
-      long fillTimeMs = System.currentTimeMillis() - startMs;
-      MatcherAssert.assertThat(
-          (double) timeoutUsed.get(),
-          Matchers.lessThan(fillTimeMs * (timeoutAllowanceFactor + 0.5))
-      );
+    Field lastBatchFillTimeMillis = 
HttpPostEmitter.class.getDeclaredField("lastBatchFillTimeMillis");
+    lastBatchFillTimeMillis.setAccessible(true);
+    final HttpPostEmitter emitter = new HttpPostEmitter(config, httpClient, 
OBJECT_MAPPER);
 
-      startMs = System.currentTimeMillis();
-      final Batch batch = emitter.emitAndReturnBatch(new IntEvent());
-      Thread.sleep(1000);
-      batch.seal();
-      emitter.flush();
-      fillTimeMs = System.currentTimeMillis() - startMs;
-      MatcherAssert.assertThat(
-          (double) timeoutUsed.get(),
-          Matchers.lessThan(fillTimeMs * (timeoutAllowanceFactor + 0.5))
-      );
-    }
+    long startMs = System.currentTimeMillis();
+    emitter.start();
+    emitter.emitAndReturnBatch(new IntEvent());
+    emitter.flush();
+
+    // sometimes System.currentTimeMillis() - startMs might be 0, so we need 
to use Math.max(1, System.currentTimeMillis() - startMs)
+    long fillTimeMs = Math.max(1, System.currentTimeMillis() - startMs);
+    Assume.assumeTrue(fillTimeMs >= (Long) 
lastBatchFillTimeMillis.get(emitter));
+    MatcherAssert.assertThat(
+        (double) timeoutUsed.get(),
+        Matchers.lessThanOrEqualTo(fillTimeMs * timeoutAllowanceFactor)
+    );
+
+    startMs = System.currentTimeMillis();
+    final Batch batch = emitter.emitAndReturnBatch(new IntEvent());
+    Thread.sleep(1000);
+    batch.seal();
+    emitter.flush();
+    // sometimes System.currentTimeMillis() - startMs might be 0, so we need 
to use Math.max(1, System.currentTimeMillis() - startMs)
+    fillTimeMs = Math.max(1, System.currentTimeMillis() - startMs);
+    Assume.assumeTrue(fillTimeMs >= (Long) 
lastBatchFillTimeMillis.get(emitter));
+    MatcherAssert.assertThat(
+        (double) timeoutUsed.get(),
+        Matchers.lessThanOrEqualTo(fillTimeMs * timeoutAllowanceFactor)
+    );
   }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to