Search in sources :

Example 76 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class ObservableTimeoutWithSelectorTest method timeoutSelectorWithFirstTimeoutFirstAndNoOtherObservable.

@Test
public void timeoutSelectorWithFirstTimeoutFirstAndNoOtherObservable() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return PublishSubject.create();
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    source.timeout(timeout, timeoutFunc).subscribe(o);
    timeout.onNext(1);
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onError(isA(TimeoutException.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 77 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class ObservableTimeoutWithSelectorTest method timeoutSelectorWithTimeoutAndOnNextRaceCondition.

@Test
public void timeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
    // Thread 1                                    Thread 2
    // 
    // observer.onNext(1)
    // start timeout
    // unsubscribe timeout in thread 2          start to do some long-time work in "unsubscribe"
    // observer.onNext(2)
    // timeout.onNext(1)
    // "unsubscribe" done
    // 
    // 
    // In the above case, the timeout operator should ignore "timeout.onNext(1)"
    // since "observer" has already seen 2.
    final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
    final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
    final CountDownLatch observerCompleted = new CountDownLatch(1);
    final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
    final AtomicBoolean latchTimeout = new AtomicBoolean(false);
    final Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            if (t1 == 1) {
                // Force "unsubscribe" run on another thread
                return Observable.unsafeCreate(new ObservableSource<Integer>() {

                    @Override
                    public void subscribe(Observer<? super Integer> observer) {
                        observer.onSubscribe(Disposable.empty());
                        enteredTimeoutOne.countDown();
                        // force the timeout message be sent after observer.onNext(2)
                        while (true) {
                            try {
                                if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
                                    // CountDownLatch timeout
                                    // There should be something wrong
                                    latchTimeout.set(true);
                                }
                                break;
                            } catch (InterruptedException e) {
                            // Since we just want to emulate a busy method,
                            // we ignore the interrupt signal from Scheduler.
                            }
                        }
                        observer.onNext(1);
                        timeoutEmittedOne.countDown();
                    }
                }).subscribeOn(Schedulers.newThread());
            } else {
                return PublishSubject.create();
            }
        }
    };
    final Observer<Integer> o = TestHelper.mockObserver();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerReceivedTwo.countDown();
            return null;
        }
    }).when(o).onNext(2);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerCompleted.countDown();
            return null;
        }
    }).when(o).onComplete();
    final TestObserver<Integer> to = new TestObserver<>(o);
    new Thread(new Runnable() {

        @Override
        public void run() {
            PublishSubject<Integer> source = PublishSubject.create();
            source.timeout(timeoutFunc, Observable.just(3)).subscribe(to);
            // start timeout
            source.onNext(1);
            try {
                if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // disable timeout
            source.onNext(2);
            try {
                if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            source.onComplete();
        }
    }).start();
    if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
        latchTimeout.set(true);
    }
    assertFalse("CoundDownLatch timeout", latchTimeout.get());
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onSubscribe((Disposable) notNull());
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o, never()).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) TestObserver(io.reactivex.rxjava3.observers.TestObserver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Observer(io.reactivex.rxjava3.core.Observer) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 78 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class ObservableTimeoutWithSelectorTest method timeoutSelectorSubsequentThrows.

@Test
public void timeoutSelectorSubsequentThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            throw new TestException();
        }
    };
    Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    source.onNext(1);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 79 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class ObservableUsingTest method performTestUsing.

private void performTestUsing(boolean disposeEagerly) {
    final Resource resource = mock(Resource.class);
    when(resource.getTextFromWeb()).thenReturn("Hello world!");
    Supplier<Resource> resourceFactory = new Supplier<Resource>() {

        @Override
        public Resource get() {
            return resource;
        }
    };
    Function<Resource, Observable<String>> observableFactory = new Function<Resource, Observable<String>>() {

        @Override
        public Observable<String> apply(Resource res) {
            return Observable.fromArray(res.getTextFromWeb().split(" "));
        }
    };
    Observer<String> observer = TestHelper.mockObserver();
    Observable<String> o = Observable.using(resourceFactory, observableFactory, new DisposeAction(), disposeEagerly);
    o.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("Hello");
    inOrder.verify(observer, times(1)).onNext("world!");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    // The resouce should be closed
    verify(resource, times(1)).dispose();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 80 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class ObservableUsingTest method performTestUsingWithSubscribingTwice.

private void performTestUsingWithSubscribingTwice(boolean disposeEagerly) {
    // When subscribe is called, a new resource should be created.
    Supplier<Resource> resourceFactory = new Supplier<Resource>() {

        @Override
        public Resource get() {
            return new Resource() {

                boolean first = true;

                @Override
                public String getTextFromWeb() {
                    if (first) {
                        first = false;
                        return "Hello world!";
                    }
                    return "Nothing";
                }

                @Override
                public void dispose() {
                // do nothing
                }
            };
        }
    };
    Function<Resource, Observable<String>> observableFactory = new Function<Resource, Observable<String>>() {

        @Override
        public Observable<String> apply(Resource res) {
            return Observable.fromArray(res.getTextFromWeb().split(" "));
        }
    };
    Observer<String> observer = TestHelper.mockObserver();
    Observable<String> o = Observable.using(resourceFactory, observableFactory, new DisposeAction(), disposeEagerly);
    o.subscribe(observer);
    o.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("Hello");
    inOrder.verify(observer, times(1)).onNext("world!");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verify(observer, times(1)).onNext("Hello");
    inOrder.verify(observer, times(1)).onNext("world!");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Aggregations

Test (org.junit.Test)97 TestException (io.reactivex.rxjava3.exceptions.TestException)78 Observable (io.reactivex.rxjava3.core.Observable)41 InOrder (org.mockito.InOrder)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Function (io.reactivex.rxjava3.functions.Function)20 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 IOException (java.io.IOException)13 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)9 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)7 Observer (io.reactivex.rxjava3.core.Observer)7 Function (org.apache.cassandra.cql3.functions.Function)7 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)6 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)6 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)5