Skip to content

Commit c3dbef4

Browse files
authored
Fix flaky test: SimplePublisherTest#stochastic_onNext_multiProducerSeemsThreadSafe (#4206)
Before this change, a producer in the test would not wait for all other producers to finish sending their messages before calling complete(). Because a producer could end up calling complete() before the other publishers finished sending, the test would see those "trailing" messages after the complete() as having been dropped. To fix this, we now wait to call complete() until all producers have finished writing their messages.
1 parent 4fae33d commit c3dbef4

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

utils/src/test/java/software/amazon/awssdk/utils/async/SimplePublisherTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ private void seemsThreadSafeWithProducerCount(int producerCount) {
414414
AtomicLong messageSendCount = new AtomicLong(0);
415415
AtomicLong messageReceiveCount = new AtomicLong(0);
416416

417+
CountDownLatch producersDone = new CountDownLatch(producerCount);
417418
Semaphore productionLimiter = new Semaphore(101);
418419
Semaphore requestLimiter = new Semaphore(57);
419420
ExecutorService executor = Executors.newFixedThreadPool(2 + producerCount);
@@ -431,6 +432,10 @@ private void seemsThreadSafeWithProducerCount(int producerCount) {
431432
productionLimiter.acquire();
432433
publisher.send(messageSendCount.getAndIncrement());
433434
}
435+
436+
// Complete once all producers are done
437+
producersDone.countDown();
438+
producersDone.await();
434439
publisher.complete().thenRun(() -> completed.complete(null)); // All but one producer sending this will fail.
435440
return null;
436441
}));

0 commit comments

Comments
 (0)