Skip to content

Commit

Permalink
Improve testNoBufferingOrBlockingOfSequence determinism
Browse files Browse the repository at this point in the history
Removed use of Thread.sleep to fix non-determinism issue reported in ReactiveX#329
  • Loading branch information
benjchristensen committed Aug 31, 2013
1 parent 5b87d1f commit e2c379c
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions rxjava-core/src/main/java/rx/operators/OperationNext.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -313,6 +314,8 @@ private static class TestException extends RuntimeException {
@Test
public void testNoBufferingOrBlockingOfSequence() throws Throwable {
final CountDownLatch finished = new CountDownLatch(1);
final int COUNT = 30;
final CountDownLatch timeHasPassed = new CountDownLatch(COUNT);
final AtomicBoolean running = new AtomicBoolean(true);
final AtomicInteger count = new AtomicInteger(0);
final Observable<Integer> obs = Observable.create(new Func1<Observer<Integer>, Subscription>() {
Expand All @@ -326,7 +329,7 @@ public void run() {
try {
while (running.get()) {
o.onNext(count.incrementAndGet());
Thread.sleep(0, 100);
timeHasPassed.countDown();
}
o.onCompleted();
} catch (Throwable e) {
Expand All @@ -350,19 +353,14 @@ public void run() {
// we should have a different value
assertTrue("a and b should be different", a != b);

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

assertTrue(it.hasNext());
int expectedHigherThan = count.get();
int c = it.next();

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

assertTrue(it.hasNext());

Expand Down

0 comments on commit e2c379c

Please sign in to comment.