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);
}
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());
}
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());
}
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);
}
}
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();
}
Aggregations