Skip to content

Commit 5a6bdd4

Browse files
Improve testNoBufferingOrBlockingOfSequence determinism
Removed use of Thread.sleep to fix non-determinism issue reported in #329
1 parent 7426b65 commit 5a6bdd4

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

rxjava-core/src/main/java/rx/operators/OperationNext.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.concurrent.ExecutorService;
2828
import java.util.concurrent.Executors;
2929
import java.util.concurrent.Future;
30+
import java.util.concurrent.TimeUnit;
3031
import java.util.concurrent.atomic.AtomicBoolean;
3132
import java.util.concurrent.atomic.AtomicInteger;
3233

@@ -313,6 +314,8 @@ private static class TestException extends RuntimeException {
313314
@Test
314315
public void testNoBufferingOrBlockingOfSequence() throws Throwable {
315316
final CountDownLatch finished = new CountDownLatch(1);
317+
final int COUNT = 30;
318+
final CountDownLatch timeHasPassed = new CountDownLatch(COUNT);
316319
final AtomicBoolean running = new AtomicBoolean(true);
317320
final AtomicInteger count = new AtomicInteger(0);
318321
final Observable<Integer> obs = Observable.create(new Func1<Observer<Integer>, Subscription>() {
@@ -326,7 +329,7 @@ public void run() {
326329
try {
327330
while (running.get()) {
328331
o.onNext(count.incrementAndGet());
329-
Thread.sleep(0, 100);
332+
timeHasPassed.countDown();
330333
}
331334
o.onCompleted();
332335
} catch (Throwable e) {
@@ -350,19 +353,14 @@ public void run() {
350353
// we should have a different value
351354
assertTrue("a and b should be different", a != b);
352355

353-
// wait for some time
354-
Thread.sleep(100);
355-
// make sure the counter in the observable has increased beyond b
356-
while (count.get() <= (b + 10)) {
357-
Thread.sleep(100);
358-
}
356+
// wait for some time (if times out we are blocked somewhere so fail ... set very high for very slow, constrained machines)
357+
timeHasPassed.await(8000, TimeUnit.MILLISECONDS);
359358

360359
assertTrue(it.hasNext());
361-
int expectedHigherThan = count.get();
362360
int c = it.next();
363361

364362
assertTrue("c should not just be the next in sequence", c != (b + 1));
365-
assertTrue("expected that c [" + c + "] is higher than " + expectedHigherThan, c > expectedHigherThan);
363+
assertTrue("expected that c [" + c + "] is higher than or equal to " + COUNT, c >= COUNT);
366364

367365
assertTrue(it.hasNext());
368366

0 commit comments

Comments
 (0)