本文整理匯總了Java中io.reactivex.subscribers.TestSubscriber類的典型用法代碼示例。如果您正苦於以下問題:Java TestSubscriber類的具體用法?Java TestSubscriber怎麽用?Java TestSubscriber使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TestSubscriber類屬於io.reactivex.subscribers包,在下文中一共展示了TestSubscriber類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: completeTask_retrievedTaskIsComplete
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void completeTask_retrievedTaskIsComplete() {
// Given a new task in the persistent repository
final Task newTask = new Task(TITLE, "");
mLocalDataSource.saveTask(newTask);
// When completed in the persistent repository
mLocalDataSource.completeTask(newTask);
// Then the task can be retrieved from the persistent repository and is complete
TestSubscriber<Optional<Task>> testSubscriber = new TestSubscriber<>();
mLocalDataSource.getTask(newTask.getId()).subscribe(testSubscriber);
testSubscriber.assertValueCount(1);
Task result = testSubscriber.values().get(0).get();
assertThat(result.isCompleted(), is(true));
}
示例2: clearCompletedTask_taskNotRetrievable
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void clearCompletedTask_taskNotRetrievable() {
// Given 2 new completed tasks and 1 active task in the persistent repository
final Task newTask1 = new Task(TITLE, "");
mLocalDataSource.saveTask(newTask1);
mLocalDataSource.completeTask(newTask1);
final Task newTask2 = new Task(TITLE2, "");
mLocalDataSource.saveTask(newTask2);
mLocalDataSource.completeTask(newTask2);
final Task newTask3 = new Task(TITLE3, "");
mLocalDataSource.saveTask(newTask3);
// When completed tasks are cleared in the repository
mLocalDataSource.clearCompletedTasks();
// Then the completed tasks cannot be retrieved and the active one can
TestSubscriber<List<Task>> testSubscriber = new TestSubscriber<>();
mLocalDataSource.getTasks().subscribe(testSubscriber);
List<Task> result = testSubscriber.values().get(0);
assertThat(result, not(hasItems(newTask1, newTask2)));
}
示例3: testInsertWithError
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void testInsertWithError(){
models.clear();
testStore.shouldThrowError(true); // enable error
TestModel model = new TestModel(99);
TestSubscriber<Optional<TestModel>> observer = new TestSubscriber<>();
disposables.add(testStore.insert(model)
.subscribeOn(Schedulers.io())
.subscribeWith(observer));
observer.awaitTerminalEvent(2, SECONDS);
observer.assertError(Throwable.class);
observer.assertErrorMessage("insertSingle.error");
testStore.shouldThrowError(false); // disable error
Assert.assertEquals(0, models.size());
}
示例4: testATestScheduler
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void testATestScheduler() throws Exception {
TestSubscriber<Long> testSubscriber = new TestSubscriber<>();
TestScheduler testScheduler = new TestScheduler();
Flowable.interval(5, TimeUnit.MILLISECONDS, testScheduler)
.map(x -> x + 1)
.filter(x -> x % 2 == 0)
.subscribe(testSubscriber);
testScheduler.advanceTimeBy(10, TimeUnit.MILLISECONDS);
testSubscriber.assertNoErrors();
testSubscriber.assertValues(2L);
testScheduler.advanceTimeBy(10, TimeUnit.MILLISECONDS);
testSubscriber.assertNoErrors();
testSubscriber.assertValues(2L, 4L);
}
示例5: getTasks_repositoryCachesAfterFirstSubscription_whenTasksAvailableInLocalStorage
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void getTasks_repositoryCachesAfterFirstSubscription_whenTasksAvailableInLocalStorage() {
// Given that the local data source has data available
setTasksAvailable(mTasksLocalDataSource, TASKS);
// And the remote data source does not have any data available
setTasksNotAvailable(mTasksRemoteDataSource);
// When two subscriptions are set
TestSubscriber<List<Task>> testSubscriber1 = new TestSubscriber<>();
mTasksRepository.getTasks().subscribe(testSubscriber1);
TestSubscriber<List<Task>> testSubscriber2 = new TestSubscriber<>();
mTasksRepository.getTasks().subscribe(testSubscriber2);
// Then tasks were only requested once from remote and local sources
verify(mTasksRemoteDataSource).getTasks();
verify(mTasksLocalDataSource).getTasks();
//
assertFalse(mTasksRepository.mCacheIsDirty);
testSubscriber1.assertValue(TASKS);
testSubscriber2.assertValue(TASKS);
}
示例6: getTasks_repositoryCachesAfterFirstSubscription_whenTasksAvailableInRemoteStorage
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void getTasks_repositoryCachesAfterFirstSubscription_whenTasksAvailableInRemoteStorage() {
// Given that the local data source has data available
setTasksAvailable(mTasksRemoteDataSource, TASKS);
// And the remote data source does not have any data available
setTasksNotAvailable(mTasksLocalDataSource);
// When two subscriptions are set
TestSubscriber<List<Task>> testSubscriber1 = new TestSubscriber<>();
mTasksRepository.getTasks().subscribe(testSubscriber1);
TestSubscriber<List<Task>> testSubscriber2 = new TestSubscriber<>();
mTasksRepository.getTasks().subscribe(testSubscriber2);
// Then tasks were only requested once from remote and local sources
verify(mTasksRemoteDataSource).getTasks();
verify(mTasksLocalDataSource).getTasks();
assertFalse(mTasksRepository.mCacheIsDirty);
testSubscriber1.assertValue(TASKS);
testSubscriber2.assertValue(TASKS);
}
示例7: getTask_requestsSingleTaskFromLocalDataSource
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@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);
}
示例8: shouldHandleDuplicateCommands
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void shouldHandleDuplicateCommands() {
// given:
final Processor<ORSet.ORSetCommand<String>, ORSet.ORSetCommand<String>> inputStream = ReplayProcessor.create();
final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
final ORSet<String> set = new ORSet<>("ID_1");
set.subscribeTo(inputStream);
set.subscribe(subscriber);
final ORSet.AddCommand<String> command = new ORSet.AddCommand<>(set.getCrdtId(), new ORSet.Element<>("1", UUID.randomUUID()));
// when:
inputStream.onNext(command);
inputStream.onNext(command);
// then:
assertThat(set, hasSize(1));
assertThat(subscriber.valueCount(), is(1));
subscriber.assertNotComplete();
subscriber.assertNoErrors();
}
示例9: shouldHandleAddCommands
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void shouldHandleAddCommands() {
// given:
final Processor<TwoPSet.TwoPSetCommand<String>, TwoPSet.TwoPSetCommand<String>> inputStream = ReplayProcessor.create();
final TestSubscriber<CrdtCommand> subscriber = TestSubscriber.create();
final TwoPSet<String> set = new TwoPSet<>("ID_1");
set.subscribeTo(inputStream);
set.subscribe(subscriber);
final TwoPSet.AddCommand<String> command1 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");
final TwoPSet.AddCommand<String> command2 = new TwoPSet.AddCommand<>(set.getCrdtId(), "2");
final TwoPSet.AddCommand<String> command3 = new TwoPSet.AddCommand<>(set.getCrdtId(), "1");
// when:
inputStream.onNext(command1);
inputStream.onNext(command2);
inputStream.onNext(command3);
// then:
assertThat(set, hasSize(2));
assertThat(subscriber.valueCount(), is(2));
subscriber.assertNotComplete();
subscriber.assertNoErrors();
}
示例10: testOneGist
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void testOneGist() {
ServiceLocator.put(OkHttpClient.class, OkHttpClientUtil.getOkHttpClient(null, MockBehavior.MOCK));
Flowable<Gist> flowable = ServiceInjector.resolve(RxEndpoints.class).getGist("3d7cbc2f66cf5d61b8014d957a270c7c");
TestSubscriber<Gist> testSubscriber = new TestSubscriber<>();
flowable.subscribe(testSubscriber);
testSubscriber.assertComplete();
List<Gist> gistList = testSubscriber.values();
Gist gist = gistList.get(0);
assertEquals("Bootstrap Customizer Config", gist.getDescription());
GistFile file = gist.getFile(gist.getFilenames().iterator().next());
assertEquals(file.getContent().length(), file.getSize());
assertEquals("config.json", file.getFilename());
flowable = ServiceInjector.resolve(RxEndpoints.class).getGist("not actually an ID");
testSubscriber = new TestSubscriber<>();
flowable.subscribe(testSubscriber);
testSubscriber.assertNotComplete();
testSubscriber.assertNoValues();
List<Throwable> errorList = testSubscriber.errors();
assertEquals(errorList.size(), 1);
assertEquals("Not Found", errorList.get(0).getMessage());
}
示例11: clientCanCancelServerStreamImplicitly
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void clientCanCancelServerStreamImplicitly() throws InterruptedException {
RxNumbersGrpc.RxNumbersStub stub = RxNumbersGrpc.newRxStub(channel);
TestSubscriber<NumberProto.Number> subscription = stub
.responsePressure(Single.just(Empty.getDefaultInstance()))
.doOnNext(number -> System.out.println(number.getNumber(0)))
.doOnError(throwable -> System.out.println(throwable.getMessage()))
.doOnComplete(() -> System.out.println("Completed"))
.doOnCancel(() -> System.out.println("Client canceled"))
.take(10)
.test();
// Consume some work
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
subscription.dispose();
subscription.awaitTerminalEvent(3, TimeUnit.SECONDS);
subscription.assertValueCount(10);
subscription.assertTerminated();
assertThat(svc.wasCanceled()).isTrue();
}
示例12: testBasicTransform
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void testBasicTransform()
{
TestSubscriber<Update<Integer>> testSubscriber = new TestSubscriber<>();
SimpleFlowableList<Integer> list = new SimpleFlowableList<>(Arrays.asList(1, 2, 3));
FlowableList<Integer> transformedList = list.map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer + 12;
}
});
transformedList.updates().subscribe(testSubscriber);
testSubscriber.assertValueCount(1);
List<Update<Integer>> onNextEvents = testSubscriber.values();
assertEquals(Arrays.asList(Change.reloaded()), onNextEvents.get(0).changes);
assertEquals(Arrays.asList(13, 14, 15), onNextEvents.get(0).list);
}
示例13: serverToClientBackpressure
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void serverToClientBackpressure() throws InterruptedException {
RxNumbersGrpc.RxNumbersStub stub = RxNumbersGrpc.newRxStub(channel);
Single<Empty> rxRequest = Single.just(Empty.getDefaultInstance());
TestSubscriber<NumberProto.Number> rxResponse = stub.responsePressure(rxRequest)
.doOnNext(n -> System.out.println(n.getNumber(0) + " <--"))
.doOnNext(n -> waitIfValuesAreEqual(n.getNumber(0), 3))
.test();
rxResponse.awaitTerminalEvent(5, TimeUnit.SECONDS);
rxResponse.assertComplete()
.assertValueCount(NUMBER_OF_STREAM_ELEMENTS);
assertThat(numberOfWaits.get()).isEqualTo(1);
}
示例14: bidiRequestBackpressure
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void bidiRequestBackpressure() throws InterruptedException {
RxNumbersGrpc.RxNumbersStub stub = RxNumbersGrpc.newRxStub(channel);
Flowable<NumberProto.Number> rxRequest = Flowable
.fromIterable(IntStream.range(0, NUMBER_OF_STREAM_ELEMENTS)::iterator)
.doOnNext(i -> System.out.println(i + " --> "))
.doOnNext(i -> updateNumberOfWaits(lastValueTime, numberOfWaits))
.map(BackpressureIntegrationTest::protoNum);
TestSubscriber<NumberProto.Number> rxResponse = stub.twoWayRequestPressure(rxRequest).test();
rxResponse.awaitTerminalEvent(5, TimeUnit.SECONDS);
rxResponse.assertComplete()
.assertValue(v -> v.getNumber(0) == NUMBER_OF_STREAM_ELEMENTS - 1);
assertThat(numberOfWaits.get()).isEqualTo(1);
}
示例15: createGist
import io.reactivex.subscribers.TestSubscriber; //導入依賴的package包/類
@Test
public void createGist() throws IOException {
ServiceLocator.put(OkHttpClient.class, OkHttpClientUtil.getOkHttpClient(null, MockBehavior.MOCK_ONLY));
Gist gist = new GistImpl();
gist.setDescription(CREATE_DESCRIPTION);
gist.addFile(CREATE_FILE_NAME, readFromAsset("mocks/javaclass"));
Flowable<Gist> flowable = ServiceInjector.resolve(RxEndpoints.class).createGist(gist);
TestSubscriber<Gist> testSubscriber = new TestSubscriber<>();
flowable.subscribe(testSubscriber);
testSubscriber.assertComplete();
List<Gist> gistList = testSubscriber.values();
Gist resultGist = gistList.get(0);
Flowable<Gist> gistFlowable = ServiceInjector.resolve(RxEndpoints.class).getGist(resultGist.getId());
TestSubscriber<Gist> gistTestSubscriber = new TestSubscriber<>();
gistFlowable.subscribe(gistTestSubscriber);
Gist detailGist = gistTestSubscriber.values().get(0);
assertEquals(detailGist.getDescription(), CREATE_DESCRIPTION);
}