Search in sources :

Example 31 with TestSubscriber

use of io.reactivex.subscribers.TestSubscriber in project todo-mvp-rxjava by albertizzy.

the class TasksRepositoryTest method getTask_requestsSingleTaskFromLocalDataSource.

@Test
public void getTask_requestsSingleTaskFromLocalDataSource() {
    // Given a stub completed task with title and description in the local repository
    Task task = new Task(TASK_TITLE, "Some Task Description", true);
    Optional<Task> taskOptional = Optional.of(task);
    setTaskAvailable(mTasksLocalDataSource, taskOptional);
    // And the task not available in the remote repository
    setTaskNotAvailable(mTasksRemoteDataSource, taskOptional.get().getId());
    // When a task is requested from the tasks repository
    TestSubscriber<Optional<Task>> testSubscriber = new TestSubscriber<>();
    mTasksRepository.getTask(task.getId()).subscribe(testSubscriber);
    // Then the task is loaded from the database
    verify(mTasksLocalDataSource).getTask(eq(task.getId()));
    testSubscriber.assertValue(taskOptional);
}
Also used : Task(com.example.android.architecture.blueprints.todoapp.data.Task) Optional(com.google.common.base.Optional) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 32 with TestSubscriber

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

the class FlowableDoOnUnsubscribeTest method testDoOnUnsubscribe.

@Test
public void testDoOnUnsubscribe() throws Exception {
    int subCount = 3;
    final CountDownLatch upperLatch = new CountDownLatch(subCount);
    final CountDownLatch lowerLatch = new CountDownLatch(subCount);
    final CountDownLatch onNextLatch = new CountDownLatch(subCount);
    final AtomicInteger upperCount = new AtomicInteger();
    final AtomicInteger lowerCount = new AtomicInteger();
    Flowable<Long> longs = Flowable.interval(50, TimeUnit.MILLISECONDS).doOnCancel(new Action() {

        @Override
        public void run() {
            // Test that upper stream will be notified for un-subscription
            // from a child subscriber
            upperLatch.countDown();
            upperCount.incrementAndGet();
        }
    }).doOnNext(new Consumer<Long>() {

        @Override
        public void accept(Long aLong) {
            // Ensure there is at least some onNext events before un-subscription happens
            onNextLatch.countDown();
        }
    }).doOnCancel(new Action() {

        @Override
        public void run() {
            // Test that lower stream will be notified for a direct un-subscription
            lowerLatch.countDown();
            lowerCount.incrementAndGet();
        }
    });
    List<Disposable> subscriptions = new ArrayList<Disposable>();
    List<TestSubscriber<Long>> subscribers = new ArrayList<TestSubscriber<Long>>();
    for (int i = 0; i < subCount; ++i) {
        TestSubscriber<Long> subscriber = new TestSubscriber<Long>();
        subscriptions.add(subscriber);
        longs.subscribe(subscriber);
        subscribers.add(subscriber);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    // FIXME no longer valid
    //            subscribers.get(i).assertUnsubscribed();
    }
    upperLatch.await();
    lowerLatch.await();
    assertEquals(String.format("There should exactly %d un-subscription events for upper stream", subCount), subCount, upperCount.get());
    assertEquals(String.format("There should exactly %d un-subscription events for lower stream", subCount), subCount, lowerCount.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 33 with TestSubscriber

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

the class FlowableDoOnUnsubscribeTest method testDoOnUnSubscribeWorksWithRefCount.

@Test
public void testDoOnUnSubscribeWorksWithRefCount() throws Exception {
    int subCount = 3;
    final CountDownLatch upperLatch = new CountDownLatch(1);
    final CountDownLatch lowerLatch = new CountDownLatch(1);
    final CountDownLatch onNextLatch = new CountDownLatch(subCount);
    final AtomicInteger upperCount = new AtomicInteger();
    final AtomicInteger lowerCount = new AtomicInteger();
    Flowable<Long> longs = Flowable.interval(50, TimeUnit.MILLISECONDS).doOnCancel(new Action() {

        @Override
        public void run() {
            // Test that upper stream will be notified for un-subscription
            upperLatch.countDown();
            upperCount.incrementAndGet();
        }
    }).doOnNext(new Consumer<Long>() {

        @Override
        public void accept(Long aLong) {
            // Ensure there is at least some onNext events before un-subscription happens
            onNextLatch.countDown();
        }
    }).doOnCancel(new Action() {

        @Override
        public void run() {
            // Test that lower stream will be notified for un-subscription
            lowerLatch.countDown();
            lowerCount.incrementAndGet();
        }
    }).publish().refCount();
    List<Disposable> subscriptions = new ArrayList<Disposable>();
    List<TestSubscriber<Long>> subscribers = new ArrayList<TestSubscriber<Long>>();
    for (int i = 0; i < subCount; ++i) {
        TestSubscriber<Long> subscriber = new TestSubscriber<Long>();
        longs.subscribe(subscriber);
        subscriptions.add(subscriber);
        subscribers.add(subscriber);
    }
    onNextLatch.await();
    for (int i = 0; i < subCount; ++i) {
        subscriptions.get(i).dispose();
    // Test that unsubscribe() method is not affected in any way
    // FIXME no longer valid
    //            subscribers.get(i).assertUnsubscribed();
    }
    upperLatch.await();
    lowerLatch.await();
    assertEquals("There should exactly 1 un-subscription events for upper stream", 1, upperCount.get());
    assertEquals("There should exactly 1 un-subscription events for lower stream", 1, lowerCount.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 34 with TestSubscriber

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

the class FlowableBlockingTest method blockinsSubscribeCancelAsync.

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

            @Override
            public void run() {
                ts.cancel();
            }
        };
        final Runnable r2 = new Runnable() {

            @Override
            public void run() {
                pp.onNext(1);
            }
        };
        final AtomicInteger c = new AtomicInteger(2);
        Schedulers.computation().scheduleDirect(new Runnable() {

            @Override
            public void run() {
                c.decrementAndGet();
                while (c.get() != 0 && !pp.hasSubscribers()) {
                }
                TestHelper.race(r1, r2);
            }
        });
        c.decrementAndGet();
        while (c.get() != 0) {
        }
        pp.blockingSubscribe(ts);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 35 with TestSubscriber

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

the class FlowableDebounceTest method debounceWithTimeBackpressure.

@Test
public void debounceWithTimeBackpressure() throws InterruptedException {
    TestScheduler scheduler = new TestScheduler();
    TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>();
    Flowable.merge(Flowable.just(1), Flowable.just(2).delay(10, TimeUnit.MILLISECONDS, scheduler)).debounce(20, TimeUnit.MILLISECONDS, scheduler).take(1).subscribe(subscriber);
    scheduler.advanceTimeBy(30, TimeUnit.MILLISECONDS);
    subscriber.assertValue(2);
    subscriber.assertTerminated();
    subscriber.assertNoErrors();
}
Also used : TestSubscriber(io.reactivex.subscribers.TestSubscriber) 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