use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.
the class BlockingObservableLatestTest method sameSourceMultipleIterators.
@Test
public void sameSourceMultipleIterators() {
TestScheduler scheduler = new TestScheduler();
Observable<Long> source = Observable.interval(1, TimeUnit.SECONDS, scheduler).take(10);
Iterable<Long> iter = source.blockingLatest();
for (int j = 0; j < 3; j++) {
Iterator<Long> it = iter.iterator();
// which onComplete will overwrite the previous value
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 FlowableCacheTest method cache.
@Test
public void cache() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Flowable<String> f = Flowable.unsafeCreate(new Publisher<String>() {
@Override
public void subscribe(final Subscriber<? super String> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
new Thread(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
System.out.println("published observable being executed");
subscriber.onNext("one");
subscriber.onComplete();
}
}).start();
}
}).cache();
// we then expect the following 2 subscriptions to get that same value
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
f.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
System.out.println("v: " + v);
latch.countDown();
}
});
// subscribe again
f.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 ObservableWindowWithStartEndObservableTest method openError.
@Test
public void openError() throws Throwable {
TestHelper.withErrorTracking(errors -> {
TestException ex1 = new TestException();
TestException ex2 = new TestException();
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
AtomicReference<Observer<? super Integer>> ref1 = new AtomicReference<>();
AtomicReference<Observer<? super Integer>> ref2 = new AtomicReference<>();
Observable<Integer> o1 = Observable.<Integer>unsafeCreate(ref1::set);
Observable<Integer> o2 = Observable.<Integer>unsafeCreate(ref2::set);
TestObserver<Observable<Integer>> to = BehaviorSubject.createDefault(1).window(o1, v -> o2).doOnNext(w -> w.test()).test();
ref1.get().onSubscribe(Disposable.empty());
ref1.get().onNext(1);
ref2.get().onSubscribe(Disposable.empty());
TestHelper.race(() -> ref1.get().onError(ex1), () -> ref2.get().onError(ex2));
to.assertError(RuntimeException.class);
if (!errors.isEmpty()) {
TestHelper.assertUndeliverable(errors, 0, TestException.class);
}
errors.clear();
}
});
}
use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.
the class ObservableWithLatestFromTest method withError.
@Test
public void withError() {
TestObserver<String> to = new TestObserver<>();
Observable.range(1, 3).withLatestFrom(new Observable<?>[] { Observable.just(1), Observable.error(new TestException()) }, toArray).subscribe(to);
to.assertNoValues();
to.assertError(TestException.class);
to.assertNotComplete();
}
use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.
the class ObservableWithLatestFromTest method sourceThrows.
@Test
public void sourceThrows() {
PublishSubject<Integer> source = PublishSubject.create();
PublishSubject<Integer> other = PublishSubject.create();
Observable<Integer> result = source.withLatestFrom(other, COMBINER);
TestObserverEx<Integer> to = new TestObserverEx<>();
result.subscribe(to);
assertTrue(source.hasObservers());
assertTrue(other.hasObservers());
other.onNext(1);
source.onNext(1);
source.onError(new TestException());
to.assertTerminated();
to.assertValue((1 << 8) + 1);
to.assertError(TestException.class);
to.assertNotComplete();
assertFalse(source.hasObservers());
assertFalse(other.hasObservers());
}
Aggregations