use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableTimeoutTests method shouldTimeoutIfSynchronizedObservableEmitFirstOnNextNotWithinTimeout.
@Test
public void shouldTimeoutIfSynchronizedObservableEmitFirstOnNextNotWithinTimeout() throws InterruptedException {
final CountDownLatch exit = new CountDownLatch(1);
final CountDownLatch timeoutSetuped = new CountDownLatch(1);
final Observer<String> observer = TestHelper.mockObserver();
final TestObserver<String> ts = new TestObserver<String>(observer);
new Thread(new Runnable() {
@Override
public void run() {
Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> observer) {
observer.onSubscribe(Disposables.empty());
try {
timeoutSetuped.countDown();
exit.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
observer.onNext("a");
observer.onComplete();
}
}).timeout(1, TimeUnit.SECONDS, testScheduler).subscribe(ts);
}
}).start();
timeoutSetuped.await();
testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
InOrder inOrder = inOrder(observer);
inOrder.verify(observer, times(1)).onError(isA(TimeoutException.class));
inOrder.verifyNoMoreInteractions();
// exit the thread
exit.countDown();
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableWithLatestFromTest method testSourceThrows.
@Test
public void testSourceThrows() {
PublishSubject<Integer> source = PublishSubject.create();
PublishSubject<Integer> other = PublishSubject.create();
Observable<Integer> result = source.withLatestFrom(other, COMBINER);
TestObserver<Integer> ts = new TestObserver<Integer>();
result.subscribe(ts);
assertTrue(source.hasObservers());
assertTrue(other.hasObservers());
other.onNext(1);
source.onNext(1);
source.onError(new TestException());
ts.assertTerminated();
ts.assertValue((1 << 8) + 1);
ts.assertError(TestException.class);
ts.assertNotComplete();
assertFalse(source.hasObservers());
assertFalse(other.hasObservers());
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableWithLatestFromTest method testOtherThrows.
@Test
public void testOtherThrows() {
PublishSubject<Integer> source = PublishSubject.create();
PublishSubject<Integer> other = PublishSubject.create();
Observable<Integer> result = source.withLatestFrom(other, COMBINER);
TestObserver<Integer> ts = new TestObserver<Integer>();
result.subscribe(ts);
assertTrue(source.hasObservers());
assertTrue(other.hasObservers());
other.onNext(1);
source.onNext(1);
other.onError(new TestException());
ts.assertTerminated();
ts.assertValue((1 << 8) + 1);
ts.assertNotComplete();
ts.assertError(TestException.class);
assertFalse(source.hasObservers());
assertFalse(other.hasObservers());
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableToFutureTest method testCancellationDuringFutureGet.
@Test
public void testCancellationDuringFutureGet() throws Exception {
Future<Object> future = new Future<Object>() {
private AtomicBoolean isCancelled = new AtomicBoolean(false);
private AtomicBoolean isDone = new AtomicBoolean(false);
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
isCancelled.compareAndSet(false, true);
return true;
}
@Override
public boolean isCancelled() {
return isCancelled.get();
}
@Override
public boolean isDone() {
return isCancelled() || isDone.get();
}
@Override
public Object get() throws InterruptedException, ExecutionException {
Thread.sleep(500);
isDone.compareAndSet(false, true);
return "foo";
}
@Override
public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return get();
}
};
Observer<Object> o = TestHelper.mockObserver();
TestObserver<Object> ts = new TestObserver<Object>(o);
Observable<Object> futureObservable = Observable.fromFuture(future);
futureObservable.subscribeOn(Schedulers.computation()).subscribe(ts);
Thread.sleep(100);
ts.dispose();
ts.assertNoErrors();
ts.assertNoValues();
ts.assertNotComplete();
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableWindowWithSizeTest method testWindowUnsubscribeOverlapping.
@Test
public void testWindowUnsubscribeOverlapping() {
TestObserver<Integer> ts = new TestObserver<Integer>();
final AtomicInteger count = new AtomicInteger();
Observable.merge(Observable.range(1, 10000).doOnNext(new Consumer<Integer>() {
@Override
public void accept(Integer t1) {
count.incrementAndGet();
}
}).window(5, 4).take(2)).subscribe(ts);
ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
ts.assertTerminated();
// System.out.println(ts.getOnNextEvents());
ts.assertValues(1, 2, 3, 4, 5, 5, 6, 7, 8, 9);
assertEquals(9, count.get());
}
Aggregations