Skip to content

Commit 10e8b78

Browse files
Test to prove non-blocking despite blocking onNext
1 parent 974b4ad commit 10e8b78

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

rxjava-core/src/test/java/rx/operators/OperatorObserveOnTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.CountDownLatch;
2323
import java.util.concurrent.TimeUnit;
2424
import java.util.concurrent.atomic.AtomicInteger;
25+
import java.util.concurrent.atomic.AtomicLong;
2526

2627
import org.junit.Test;
2728
import org.mockito.InOrder;
@@ -310,6 +311,41 @@ public void call(Integer t1) {
310311
});
311312
}
312313

314+
@Test
315+
public void testNonBlockingOuterWhileBlockingOnNext() throws InterruptedException {
316+
317+
final CountDownLatch latch = new CountDownLatch(1);
318+
final AtomicLong completeTime = new AtomicLong();
319+
// use subscribeOn to make async, observeOn to move
320+
Observable.range(1, 1000).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread()).subscribe(new Observer<Integer>() {
321+
322+
@Override
323+
public void onCompleted() {
324+
System.out.println("onCompleted");
325+
completeTime.set(System.nanoTime());
326+
latch.countDown();
327+
}
328+
329+
@Override
330+
public void onError(Throwable e) {
331+
332+
}
333+
334+
@Override
335+
public void onNext(Integer t) {
336+
337+
}
338+
339+
});
340+
341+
long afterSubscribeTime = System.nanoTime();
342+
System.out.println("After subscribe: " + latch.getCount());
343+
assertEquals(1, latch.getCount());
344+
latch.await();
345+
assertTrue(completeTime.get() > afterSubscribeTime);
346+
System.out.println("onComplete nanos after subscribe: " + (completeTime.get() - afterSubscribeTime));
347+
}
348+
313349
@Test
314350
public final void testBackpressureOnFastProducerSlowConsumerWithUnsubscribeNewThread() throws InterruptedException {
315351
testBackpressureOnFastProducerSlowConsumerWithUnsubscribe(Schedulers.newThread());

0 commit comments

Comments
 (0)