Search in sources :

Example 71 with Observable

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());
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest)

Example 72 with Observable

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());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 73 with Observable

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();
        }
    });
}
Also used : java.util(java.util) Observer(io.reactivex.rxjava3.core.Observer) io.reactivex.rxjava3.subjects(io.reactivex.rxjava3.subjects) TestException(io.reactivex.rxjava3.exceptions.TestException) io.reactivex.rxjava3.functions(io.reactivex.rxjava3.functions) java.util.concurrent.atomic(java.util.concurrent.atomic) IOException(java.io.IOException) io.reactivex.rxjava3.observers(io.reactivex.rxjava3.observers) TimeUnit(java.util.concurrent.TimeUnit) Functions(io.reactivex.rxjava3.internal.functions.Functions) Observable(io.reactivex.rxjava3.core.Observable) Disposable(io.reactivex.rxjava3.disposables.Disposable) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) org.junit(org.junit) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) io.reactivex.rxjava3.testsupport(io.reactivex.rxjava3.testsupport) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) TestException(io.reactivex.rxjava3.exceptions.TestException) Observer(io.reactivex.rxjava3.core.Observer) Observable(io.reactivex.rxjava3.core.Observable)

Example 74 with Observable

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();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Observable(io.reactivex.rxjava3.core.Observable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 75 with Observable

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());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)132 TestException (io.reactivex.rxjava3.exceptions.TestException)109 Observable (io.reactivex.rxjava3.core.Observable)83 InOrder (org.mockito.InOrder)60 TestObserver (io.reactivex.rxjava3.observers.TestObserver)38 Disposable (io.reactivex.rxjava3.disposables.Disposable)35 IOException (java.io.IOException)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)22 Observer (io.reactivex.rxjava3.core.Observer)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 Function (io.reactivex.rxjava3.functions.Function)10 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 Collections (java.util.Collections)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Assert (org.junit.Assert)6