Search in sources :

Example 46 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableToFutureTest method testCancellationDuringFutureGet.

@Test
public void testCancellationDuringFutureGet() throws Exception {
    Future<Object> future = new Future<Object>() {

        private AtomicBoolean isCancelled = new AtomicBoolean(false);

        private AtomicBoolean isDone = new AtomicBoolean(false);

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            isCancelled.compareAndSet(false, true);
            return true;
        }

        @Override
        public boolean isCancelled() {
            return isCancelled.get();
        }

        @Override
        public boolean isDone() {
            return isCancelled() || isDone.get();
        }

        @Override
        public Object get() throws InterruptedException, ExecutionException {
            Thread.sleep(500);
            isDone.compareAndSet(false, true);
            return "foo";
        }

        @Override
        public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return get();
        }
    };
    Subscriber<Object> o = TestHelper.mockSubscriber();
    TestSubscriber<Object> ts = new TestSubscriber<Object>(o);
    Flowable<Object> futureObservable = Flowable.fromFuture(future);
    futureObservable.subscribeOn(Schedulers.computation()).subscribe(ts);
    Thread.sleep(100);
    ts.dispose();
    ts.assertNoErrors();
    ts.assertNoValues();
    ts.assertNotComplete();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 47 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableStrictTest method badRequestOnNextRace.

@Test
public void badRequestOnNextRace() {
    for (int i = 0; i < 500; i++) {
        TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
        final PublishProcessor<Integer> pp = PublishProcessor.create();
        final StrictSubscriber<Integer> s = new StrictSubscriber<Integer>(ts);
        s.onSubscribe(new BooleanSubscription());
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                pp.onNext(1);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                s.request(0);
            }
        };
        TestHelper.race(r1, r2);
        if (ts.valueCount() == 0) {
            ts.assertFailure(IllegalArgumentException.class);
        } else {
            ts.assertValue(1).assertNoErrors().assertNotComplete();
        }
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) StrictSubscriber(io.reactivex.internal.subscribers.StrictSubscriber) Test(org.junit.Test)

Example 48 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout.

@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout() throws InterruptedException {
    // From https://github.com/ReactiveX/RxJava/pull/951
    final Subscription s = mock(Subscription.class);
    Flowable<String> never = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(s);
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Flowable<String> observableWithTimeout = never.timeout(1000, TimeUnit.MILLISECONDS, testScheduler);
    Subscriber<String> observer = TestHelper.mockSubscriber();
    TestSubscriber<String> ts = new TestSubscriber<String>(observer);
    observableWithTimeout.subscribe(ts);
    testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer).onError(isA(TimeoutException.class));
    inOrder.verifyNoMoreInteractions();
    verify(s, times(1)).cancel();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 49 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableTimeoutTests method shouldSwitchToOtherIfOnNextNotWithinTimeout.

@Test
public void shouldSwitchToOtherIfOnNextNotWithinTimeout() {
    Flowable<String> other = Flowable.just("a", "b", "c");
    Flowable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
    Subscriber<String> observer = TestHelper.mockSubscriber();
    TestSubscriber<String> ts = new TestSubscriber<String>(observer);
    source.subscribe(ts);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onNext("Two");
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("One");
    inOrder.verify(observer, times(1)).onNext("a");
    inOrder.verify(observer, times(1)).onNext("b");
    inOrder.verify(observer, times(1)).onNext("c");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    ts.dispose();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 50 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project RxJava by ReactiveX.

the class FlowableTakeUntilPredicateTest method testErrorIncludesLastValueAsCause.

@Test
public void testErrorIncludesLastValueAsCause() {
    TestSubscriber<String> ts = new TestSubscriber<String>();
    final TestException e = new TestException("Forced failure");
    Predicate<String> predicate = new Predicate<String>() {

        @Override
        public boolean test(String t) {
            throw e;
        }
    };
    Flowable.just("abc").takeUntil(predicate).subscribe(ts);
    ts.assertTerminated();
    ts.assertNotComplete();
    ts.assertError(TestException.class);
// FIXME last cause value is not saved
//        assertTrue(ts.errors().get(0).getCause().getMessage().contains("abc"));
}
Also used : TestException(io.reactivex.exceptions.TestException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Aggregations

TestSubscriber (io.reactivex.subscribers.TestSubscriber)65 Test (org.junit.Test)43 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)20 TestException (io.reactivex.exceptions.TestException)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 InOrder (org.mockito.InOrder)11 Disposable (io.reactivex.disposables.Disposable)9 TestScheduler (io.reactivex.schedulers.TestScheduler)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 BooleanSupplier (io.reactivex.functions.BooleanSupplier)5 ArrayDeque (java.util.ArrayDeque)5 Task (com.example.android.architecture.blueprints.todoapp.data.Task)2 Optional (com.google.common.base.Optional)2 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Subscriber (org.reactivestreams.Subscriber)2 Flowable (io.reactivex.Flowable)1 Worker (io.reactivex.Scheduler.Worker)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Action (io.reactivex.functions.Action)1