use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableDoOnUnsubscribeTest method testDoOnUnSubscribeWorksWithRefCount.
@Test
public void testDoOnUnSubscribeWorksWithRefCount() throws Exception {
int subCount = 3;
final CountDownLatch upperLatch = new CountDownLatch(1);
final CountDownLatch lowerLatch = new CountDownLatch(1);
final CountDownLatch onNextLatch = new CountDownLatch(subCount);
final AtomicInteger upperCount = new AtomicInteger();
final AtomicInteger lowerCount = new AtomicInteger();
Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(new Action() {
@Override
public void run() {
// Test that upper stream will be notified for un-subscription
upperLatch.countDown();
upperCount.incrementAndGet();
}
}).doOnNext(new Consumer<Long>() {
@Override
public void accept(Long aLong) {
// Ensure there is at least some onNext events before un-subscription happens
onNextLatch.countDown();
}
}).doOnDispose(new Action() {
@Override
public void run() {
// Test that lower stream will be notified for un-subscription
lowerLatch.countDown();
lowerCount.incrementAndGet();
}
}).publish().refCount();
List<Disposable> subscriptions = new ArrayList<Disposable>();
List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
for (int i = 0; i < subCount; ++i) {
TestObserver<Long> observer = new TestObserver<Long>();
longs.subscribe(observer);
subscriptions.add(observer);
subscribers.add(observer);
}
onNextLatch.await();
for (int i = 0; i < subCount; ++i) {
subscriptions.get(i).dispose();
// Test that unsubscribe() method is not affected in any way
// FIXME no longer valid
// subscribers.get(i).assertUnsubscribed();
}
upperLatch.await();
lowerLatch.await();
assertEquals("There should exactly 1 un-subscription events for upper stream", 1, upperCount.get());
assertEquals("There should exactly 1 un-subscription events for lower stream", 1, lowerCount.get());
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableDoOnUnsubscribeTest method testDoOnUnsubscribe.
@Test
public void testDoOnUnsubscribe() throws Exception {
int subCount = 3;
final CountDownLatch upperLatch = new CountDownLatch(subCount);
final CountDownLatch lowerLatch = new CountDownLatch(subCount);
final CountDownLatch onNextLatch = new CountDownLatch(subCount);
final AtomicInteger upperCount = new AtomicInteger();
final AtomicInteger lowerCount = new AtomicInteger();
Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(new Action() {
@Override
public void run() {
// Test that upper stream will be notified for un-subscription
// from a child Observer
upperLatch.countDown();
upperCount.incrementAndGet();
}
}).doOnNext(new Consumer<Long>() {
@Override
public void accept(Long aLong) {
// Ensure there is at least some onNext events before un-subscription happens
onNextLatch.countDown();
}
}).doOnDispose(new Action() {
@Override
public void run() {
// Test that lower stream will be notified for a direct un-subscription
lowerLatch.countDown();
lowerCount.incrementAndGet();
}
});
List<Disposable> subscriptions = new ArrayList<Disposable>();
List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
for (int i = 0; i < subCount; ++i) {
TestObserver<Long> observer = new TestObserver<Long>();
subscriptions.add(observer);
longs.subscribe(observer);
subscribers.add(observer);
}
onNextLatch.await();
for (int i = 0; i < subCount; ++i) {
subscriptions.get(i).dispose();
// Test that unsubscribe() method is not affected in any way
// FIXME no longer valid
// subscribers.get(i).assertUnsubscribed();
}
upperLatch.await();
lowerLatch.await();
assertEquals(String.format("There should exactly %d un-subscription events for upper stream", subCount), subCount, upperCount.get());
assertEquals(String.format("There should exactly %d un-subscription events for lower stream", subCount), subCount, lowerCount.get());
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableIgnoreElementsTest method testErrorReceived.
@Test
public void testErrorReceived() {
TestObserver<Object> ts = new TestObserver<Object>();
TestException ex = new TestException("boo");
Observable.error(ex).ignoreElements().subscribe(ts);
ts.assertNoValues();
ts.assertTerminated();
// FIXME no longer testable
// ts.assertUnsubscribed();
ts.assertError(TestException.class);
ts.assertErrorMessage("boo");
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableJoinTest method badEndSource.
@Test
public void badEndSource() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
@SuppressWarnings("rawtypes") final Observer[] o = { null };
TestObserver<Integer> to = Observable.just(1).join(Observable.just(2), Functions.justFunction(Observable.never()), Functions.justFunction(new Observable<Integer>() {
@Override
protected void subscribeActual(Observer<? super Integer> observer) {
o[0] = observer;
observer.onSubscribe(Disposables.empty());
observer.onError(new TestException("First"));
}
}), new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer a, Integer b) throws Exception {
return a + b;
}
}).test();
o[0].onError(new TestException("Second"));
to.assertFailureAndMessage(TestException.class, "First");
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.observers.TestObserver in project RxJava by ReactiveX.
the class ObservableFlatMapTest method testFlatMapMaxConcurrent.
@Test
public void testFlatMapMaxConcurrent() {
final int m = 4;
final AtomicInteger subscriptionCount = new AtomicInteger();
Observable<Integer> source = Observable.range(1, 10).flatMap(new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return composer(Observable.range(t1 * 10, 2), subscriptionCount, m).subscribeOn(Schedulers.computation());
}
}, m);
TestObserver<Integer> ts = new TestObserver<Integer>();
source.subscribe(ts);
ts.awaitTerminalEvent();
ts.assertNoErrors();
Set<Integer> expected = new HashSet<Integer>(Arrays.asList(10, 11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100, 101));
Assert.assertEquals(expected.size(), ts.valueCount());
Assert.assertTrue(expected.containsAll(ts.values()));
}
Aggregations