Skip to content

Commit 9f6e9a4

Browse files
Fix unit test determinism
1 parent abee009 commit 9f6e9a4

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

rxjava-core/src/test/java/rx/observers/SerializedObserverTest.java

+19-6
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ public void runConcurrencyTest() {
284284
public void testNotificationDelay() throws InterruptedException {
285285
ExecutorService tp = Executors.newFixedThreadPool(2);
286286

287-
final CountDownLatch onNextCount = new CountDownLatch(1);
287+
final CountDownLatch firstOnNext = new CountDownLatch(1);
288+
final CountDownLatch onNextCount = new CountDownLatch(2);
288289
final CountDownLatch latch = new CountDownLatch(1);
289290

290291
TestSubscriber<String> to = new TestSubscriber<String>(new Observer<String>() {
@@ -301,8 +302,7 @@ public void onError(Throwable e) {
301302

302303
@Override
303304
public void onNext(String t) {
304-
// know when the first thread gets in
305-
onNextCount.countDown();
305+
firstOnNext.countDown();
306306
// force it to take time when delivering so the second one is enqueued
307307
try {
308308
latch.await();
@@ -313,10 +313,10 @@ public void onNext(String t) {
313313
});
314314
Observer<String> o = serializedObserver(to);
315315

316-
Future<?> f1 = tp.submit(new OnNextThread(o, 1));
317-
Future<?> f2 = tp.submit(new OnNextThread(o, 1));
316+
Future<?> f1 = tp.submit(new OnNextThread(o, 1, onNextCount));
317+
Future<?> f2 = tp.submit(new OnNextThread(o, 1, onNextCount));
318318

319-
onNextCount.await();
319+
firstOnNext.await();
320320

321321
Thread t1 = to.getLastSeenThread();
322322
System.out.println("first onNext on thread: " + t1);
@@ -431,14 +431,24 @@ public void call(Subscriber<? super String> s) {
431431
*/
432432
public static class OnNextThread implements Runnable {
433433

434+
private final CountDownLatch latch;
434435
private final Observer<String> observer;
435436
private final int numStringsToSend;
436437
final AtomicInteger produced;
437438

439+
OnNextThread(Observer<String> observer, int numStringsToSend, CountDownLatch latch) {
440+
this(observer, numStringsToSend, new AtomicInteger(), latch);
441+
}
442+
438443
OnNextThread(Observer<String> observer, int numStringsToSend, AtomicInteger produced) {
444+
this(observer, numStringsToSend, produced, null);
445+
}
446+
447+
OnNextThread(Observer<String> observer, int numStringsToSend, AtomicInteger produced, CountDownLatch latch) {
439448
this.observer = observer;
440449
this.numStringsToSend = numStringsToSend;
441450
this.produced = produced;
451+
this.latch = latch;
442452
}
443453

444454
OnNextThread(Observer<String> observer, int numStringsToSend) {
@@ -449,6 +459,9 @@ public static class OnNextThread implements Runnable {
449459
public void run() {
450460
for (int i = 0; i < numStringsToSend; i++) {
451461
observer.onNext(Thread.currentThread().getId() + "-" + i);
462+
if (latch != null) {
463+
latch.countDown();
464+
}
452465
produced.incrementAndGet();
453466
}
454467
}

0 commit comments

Comments
 (0)