Search in sources :

Example 6 with TestSubscriber

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

the class FlowablePublishTest method subscribeDisconnectRace.

@Test
public void subscribeDisconnectRace() {
    for (int i = 0; i < 500; i++) {
        final PublishProcessor<Integer> ps = PublishProcessor.create();
        final ConnectableFlowable<Integer> co = ps.publish();
        final Disposable d = co.connect();
        final TestSubscriber<Integer> to = new TestSubscriber<Integer>();
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                d.dispose();
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                co.subscribe(to);
            }
        };
        TestHelper.race(r1, r2);
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 7 with TestSubscriber

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

the class FlowablePublishTest method testSubscribeAfterDisconnectThenConnect.

@Test
public void testSubscribeAfterDisconnectThenConnect() {
    ConnectableFlowable<Integer> source = Flowable.just(1).publish();
    TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>();
    source.subscribe(ts1);
    Disposable s = source.connect();
    ts1.assertValue(1);
    ts1.assertNoErrors();
    ts1.assertTerminated();
    TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>();
    source.subscribe(ts2);
    Disposable s2 = source.connect();
    ts2.assertValue(1);
    ts2.assertNoErrors();
    ts2.assertTerminated();
    System.out.println(s);
    System.out.println(s2);
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 8 with TestSubscriber

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

the class FlowablePublishTest method syncFusedObserveOn2.

@Test
public void syncFusedObserveOn2() {
    ConnectableFlowable<Integer> co = Flowable.range(0, 1000).publish();
    Flowable<Integer> obs = co.observeOn(ImmediateThinScheduler.INSTANCE);
    for (int i = 0; i < 1000; i++) {
        for (int j = 1; j < 6; j++) {
            List<TestSubscriber<Integer>> tss = new ArrayList<TestSubscriber<Integer>>();
            for (int k = 1; k < j; k++) {
                TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
                tss.add(ts);
                obs.subscribe(ts);
            }
            Disposable s = co.connect();
            for (TestSubscriber<Integer> ts : tss) {
                ts.awaitDone(5, TimeUnit.SECONDS).assertSubscribed().assertValueCount(1000).assertNoErrors().assertComplete();
            }
            s.dispose();
        }
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 9 with TestSubscriber

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

the class FlowableFlatMapTest method testFlatMapSelectorMaxConcurrent.

@Test
public void testFlatMapSelectorMaxConcurrent() {
    final int m = 4;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Flowable<Integer> source = Flowable.range(1, 10).flatMap(new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            return composer(Flowable.range(t1 * 10, 2), subscriptionCount, m).subscribeOn(Schedulers.computation());
        }
    }, new BiFunction<Integer, Integer, Integer>() {

        @Override
        public Integer apply(Integer t1, Integer t2) {
            return t1 * 1000 + t2;
        }
    }, m);
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    source.subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    Set<Integer> expected = new HashSet<Integer>(Arrays.asList(1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, 6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101));
    Assert.assertEquals(expected.size(), ts.valueCount());
    System.out.println("--> testFlatMapSelectorMaxConcurrent: " + ts.values());
    Assert.assertTrue(expected.containsAll(ts.values()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 10 with TestSubscriber

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

the class FlowableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyErrored.

@Test
@Ignore("s should be considered cancelled upon executing onError and not expect downstream to call cancel")
public void shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyErrored() throws InterruptedException {
    // From https://github.com/ReactiveX/RxJava/pull/951
    final Subscription s = mock(Subscription.class);
    Flowable<String> immediatelyError = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(s);
            subscriber.onError(new IOException("Error"));
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Flowable<String> observableWithTimeout = immediatelyError.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(IOException.class));
    inOrder.verifyNoMoreInteractions();
    verify(s, times(1)).cancel();
}
Also used : InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.subscribers.TestSubscriber) IOException(java.io.IOException) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestScheduler(io.reactivex.schedulers.TestScheduler)

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