Search in sources :

Example 36 with Observable

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);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) CountDownLatch(java.util.concurrent.CountDownLatch) TestObserver(io.reactivex.rxjava3.observers.TestObserver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 37 with Observable

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();
    }
}
Also used : Observer(io.reactivex.rxjava3.core.Observer) BufferExactObserver(io.reactivex.rxjava3.internal.operators.observable.ObservableBuffer.BufferExactObserver) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable)

Example 38 with Observable

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observer(io.reactivex.rxjava3.core.Observer) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 39 with Observable

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

Example 40 with Observable

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();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

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