Search in sources :

Example 16 with TestSubscriber

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

the class FlowableTimeoutWithSelectorTest method testTimeoutSelectorWithTimeoutAndOnNextRaceCondition.

@Test
public void testTimeoutSelectorWithTimeoutAndOnNextRaceCondition() 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, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {

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

                    @Override
                    public void subscribe(Subscriber<? super Integer> subscriber) {
                        subscriber.onSubscribe(new BooleanSubscription());
                        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.
                            }
                        }
                        subscriber.onNext(1);
                        timeoutEmittedOne.countDown();
                    }
                }).subscribeOn(Schedulers.newThread());
            } else {
                return PublishProcessor.create();
            }
        }
    };
    final Subscriber<Integer> o = TestHelper.mockSubscriber();
    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 TestSubscriber<Integer> ts = new TestSubscriber<Integer>(o);
    new Thread(new Runnable() {

        @Override
        public void run() {
            PublishProcessor<Integer> source = PublishProcessor.create();
            source.timeout(timeoutFunc, Flowable.just(3)).subscribe(ts);
            // 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((Subscription) 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 : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.functions.Function) TestSubscriber(io.reactivex.subscribers.TestSubscriber) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 17 with TestSubscriber

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

the class FlowableSkipTest method testBackpressureMultipleSmallAsyncRequests.

@Test
public void testBackpressureMultipleSmallAsyncRequests() throws InterruptedException {
    final AtomicLong requests = new AtomicLong(0);
    TestSubscriber<Long> ts = new TestSubscriber<Long>(0L);
    Flowable.interval(100, TimeUnit.MILLISECONDS).doOnRequest(new LongConsumer() {

        @Override
        public void accept(long n) {
            requests.addAndGet(n);
        }
    }).skip(4).subscribe(ts);
    Thread.sleep(100);
    ts.request(1);
    ts.request(1);
    Thread.sleep(100);
    ts.dispose();
    // FIXME not assertable anymore
    //        ts.assertUnsubscribed();
    ts.assertNoErrors();
    assertEquals(6, requests.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) LongConsumer(io.reactivex.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 18 with TestSubscriber

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

the class FlowableWithLatestFromTest method withMainError.

@Test
public void withMainError() {
    TestSubscriber<String> ts = new TestSubscriber<String>(0);
    Flowable.error(new TestException()).withLatestFrom(new Flowable<?>[] { Flowable.just(1), Flowable.just(1) }, toArray).subscribe(ts);
    ts.assertNoValues();
    ts.assertError(TestException.class);
    ts.assertNotComplete();
}
Also used : TestException(io.reactivex.exceptions.TestException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 19 with TestSubscriber

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

the class HalfSerializerSubscriberTest method reentrantOnNextOnError.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void reentrantOnNextOnError() {
    final AtomicInteger wip = new AtomicInteger();
    final AtomicThrowable error = new AtomicThrowable();
    final Subscriber[] a = { null };
    final TestSubscriber ts = new TestSubscriber();
    FlowableSubscriber s = new FlowableSubscriber() {

        @Override
        public void onSubscribe(Subscription s) {
            ts.onSubscribe(s);
        }

        @Override
        public void onNext(Object t) {
            if (t.equals(1)) {
                HalfSerializer.onError(a[0], new TestException(), wip, error);
            }
            ts.onNext(t);
        }

        @Override
        public void onError(Throwable t) {
            ts.onError(t);
        }

        @Override
        public void onComplete() {
            ts.onComplete();
        }
    };
    a[0] = s;
    s.onSubscribe(new BooleanSubscription());
    HalfSerializer.onNext(s, 1, wip, error);
    ts.assertFailure(TestException.class, 1);
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 20 with TestSubscriber

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

the class QueueDrainHelperTest method postCompleteCancelledAfterOne.

@Test
public void postCompleteCancelledAfterOne() {
    final TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            cancel();
        }
    };
    ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
    AtomicLong state = new AtomicLong();
    BooleanSupplier isCancelled = new BooleanSupplier() {

        @Override
        public boolean getAsBoolean() throws Exception {
            return ts.isCancelled();
        }
    };
    ts.onSubscribe(new BooleanSubscription());
    queue.offer(1);
    state.getAndIncrement();
    QueueDrainHelper.postComplete(ts, queue, state, isCancelled);
    ts.assertValue(1).assertNoErrors().assertNotComplete();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BooleanSupplier(io.reactivex.functions.BooleanSupplier) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Aggregations

TestSubscriber (io.reactivex.subscribers.TestSubscriber)63 Test (org.junit.Test)41 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 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 Function (io.reactivex.functions.Function)1 LongConsumer (io.reactivex.functions.LongConsumer)1