use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.
the class ObservableFromSupplierTest method shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission.
@SuppressWarnings("unchecked")
@Test
public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Throwable {
Supplier<String> func = mock(Supplier.class);
final CountDownLatch funcLatch = new CountDownLatch(1);
final CountDownLatch observerLatch = new CountDownLatch(1);
when(func.get()).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
observerLatch.countDown();
try {
funcLatch.await();
} catch (InterruptedException e) {
// It's okay, unsubscription causes Thread interruption
// Restoring interruption status of the Thread
Thread.currentThread().interrupt();
}
return "should_not_be_delivered";
}
});
Observable<String> fromSupplierObservable = Observable.fromSupplier(func);
Observer<Object> observer = TestHelper.mockObserver();
TestObserver<String> outer = new TestObserver<>(observer);
fromSupplierObservable.subscribeOn(Schedulers.computation()).subscribe(outer);
// Wait until func will be invoked
observerLatch.await();
// Unsubscribing before emission
outer.dispose();
// Emitting result
funcLatch.countDown();
// func must be invoked
verify(func).get();
// Observer must not be notified at all
verify(observer).onSubscribe(any(Disposable.class));
verifyNoMoreInteractions(observer);
}
use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.
the class ObservableBufferTest method openCloseBadClose.
@Test
public void openCloseBadClose() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
Observable.never().buffer(Observable.just(1).concatWith(Observable.<Integer>never()), Functions.justFunction(new Observable<Object>() {
@Override
protected void subscribeActual(Observer<? super Object> observer) {
assertFalse(((Disposable) observer).isDisposed());
Disposable bs1 = Disposable.empty();
Disposable bs2 = Disposable.empty();
observer.onSubscribe(bs1);
assertFalse(bs1.isDisposed());
assertFalse(bs2.isDisposed());
observer.onSubscribe(bs2);
assertFalse(bs1.isDisposed());
assertTrue(bs2.isDisposed());
observer.onError(new IOException());
assertTrue(((Disposable) observer).isDisposed());
observer.onComplete();
observer.onNext(1);
observer.onError(new TestException());
}
})).test().assertFailure(IOException.class);
TestHelper.assertError(errors, 0, ProtocolViolationException.class);
TestHelper.assertUndeliverable(errors, 1, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.
the class ObservableCacheTest method cache.
@Test
public void cache() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Observable<String> o = Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> observer) {
observer.onSubscribe(Disposable.empty());
new Thread(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
System.out.println("published Observable being executed");
observer.onNext("one");
observer.onComplete();
}
}).start();
}
}).cache();
// we then expect the following 2 subscriptions to get that same value
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
System.out.println("v: " + v);
latch.countDown();
}
});
// subscribe again
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
System.out.println("v: " + v);
latch.countDown();
}
});
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
}
use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.
the class BlockingObservableMostRecentTest method singleSourceManyIterators.
@Test
public void singleSourceManyIterators() {
TestScheduler scheduler = new TestScheduler();
Observable<Long> source = Observable.interval(1, TimeUnit.SECONDS, scheduler).take(10);
Iterable<Long> iter = source.blockingMostRecent(-1L);
for (int j = 0; j < 3; j++) {
Iterator<Long> it = iter.iterator();
Assert.assertEquals(Long.valueOf(-1), it.next());
for (int i = 0; i < 9; i++) {
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
Assert.assertTrue(it.hasNext());
Assert.assertEquals(Long.valueOf(i), it.next());
}
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
Assert.assertFalse(it.hasNext());
}
}
use of io.reactivex.rxjava3.core.Observable 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 };
TestObserverEx<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(Disposable.empty());
observer.onError(new TestException("First"));
}
}), new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer a, Integer b) throws Exception {
return a + b;
}
}).to(TestHelper.<Integer>testConsumer());
o[0].onError(new TestException("Second"));
to.assertFailureAndMessage(TestException.class, "First");
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
} finally {
RxJavaPlugins.reset();
}
}
Aggregations