use of rx.Scheduler in project AnDevCon-RxPatterns by colintheshots.
the class Example16Test method test_anomalous_network_event.
@Test
public void test_anomalous_network_event() {
// TestScheduler lets you advance time by hand
TestScheduler scheduler = Schedulers.test();
TestSubscriber<NetworkResponse> subscriber = new TestSubscriber<>();
// Scheduler.Worker lets you schedule events in time
Scheduler.Worker worker = scheduler.createWorker();
// Subjects allow both input and output, so they can be swapped in for
// Retrofit calls to unit test your code.
final PublishSubject<NetworkResponse> networkSubject = PublishSubject.create();
// schedule a first observable event to occur at 1000 ms
worker.schedule(new Action0() {
@Override
public void call() {
networkSubject.onError(new TimeoutException());
}
}, 10000, TimeUnit.MILLISECONDS);
// subscribing so events appear
networkSubject.subscribeOn(scheduler).subscribe(subscriber);
scheduler.advanceTimeBy(20000, TimeUnit.MILLISECONDS);
subscriber.awaitTerminalEvent();
// we use the class-based assertError method, since it's easier to match
subscriber.assertError(TimeoutException.class);
subscriber.assertValueCount(0);
subscriber.assertUnsubscribed();
}
use of rx.Scheduler in project storio by pushtorefresh.
the class DefaultStorIOContentResolverTest method defaultSchedulerReturnsSpecifiedScheduler.
@Test
public void defaultSchedulerReturnsSpecifiedScheduler() {
Scheduler scheduler = mock(Scheduler.class);
StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(mock(ContentResolver.class)).defaultScheduler(scheduler).build();
assertThat(storIOContentResolver.defaultScheduler()).isSameAs(scheduler);
}
use of rx.Scheduler in project goro by stanfy.
the class TestActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoroService.setup(this, Goro.create());
Scheduler scheduler = new RxGoro(goro).scheduler("test-queue");
Observable.just("ok").subscribeOn(scheduler).subscribe(new Action1<String>() {
@Override
public void call(String s) {
result = "ok";
resultSync.countDown();
}
});
Observable.error(new RuntimeException("test error")).subscribeOn(scheduler).subscribe(Actions.empty(), new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
error = throwable;
errorSync.countDown();
}
});
}
use of rx.Scheduler in project pictureapp by EyeSeeTea.
the class PullDhisSDKDataSource method pullData.
public void pullData(PullFilters pullFilters, List<OrganisationUnit> organisationUnits, IDataSourceCallback<List<Event>> callback) {
boolean isNetworkAvailable = isNetworkAvailable();
if (!isNetworkAvailable) {
callback.onError(new NetworkException());
} else {
try {
List<Event> events = new ArrayList<>();
for (OrganisationUnit organisationUnit : organisationUnits) {
Scheduler pullEventsThread = Schedulers.newThread();
EventFilters eventFilters = new EventFilters();
eventFilters.setStartDate(pullFilters.getStartDate());
eventFilters.setEndDate(pullFilters.getEndDate());
eventFilters.setMaxEvents(pullFilters.getMaxEvents());
eventFilters.setOrganisationUnitUId(organisationUnit.getUId());
mPullDhisSDKDataSourceStrategy.setEventFilters(eventFilters);
List<Event> eventsByOrgUnit = D2.events().pull(eventFilters).subscribeOn(pullEventsThread).observeOn(pullEventsThread).toBlocking().single();
events.addAll(eventsByOrgUnit);
}
callback.onSuccess(events);
} catch (Exception e) {
callback.onError(e);
}
}
}
use of rx.Scheduler in project AnDevCon-RxPatterns by colintheshots.
the class Example16Test method using_testscheduler_to_simulate_network_events.
@Test
public void using_testscheduler_to_simulate_network_events() {
// TestScheduler lets you advance time by hand
TestScheduler scheduler = Schedulers.test();
TestSubscriber<NetworkResponse> subscriber = new TestSubscriber<>();
// Scheduler.Worker lets you schedule events in time
Scheduler.Worker worker = scheduler.createWorker();
// Subjects allow both input and output, so they can be swapped in for
// Retrofit calls to unit test your code.
final PublishSubject<NetworkResponse> networkSubject = PublishSubject.create();
// schedule a first observable event to occur at 1000 ms
worker.schedule(new Action0() {
@Override
public void call() {
// explicitly calling onNext in a worker allows one to
// create a very specific test of timed events
networkSubject.onNext(new NetworkResponse(401));
}
}, 1000, TimeUnit.MILLISECONDS);
// schedule a second observable event to occur at 2000 ms
worker.schedule(new Action0() {
@Override
public void call() {
networkSubject.onNext(new NetworkResponse(200));
networkSubject.onCompleted();
}
}, 2000, TimeUnit.MILLISECONDS);
// we must subscribe before anticipating results
networkSubject.subscribeOn(scheduler).subscribe(subscriber);
// we can manually advance time using the scheduler and check assertions
scheduler.advanceTimeBy(1500, TimeUnit.MILLISECONDS);
subscriber.assertReceivedOnNext(Arrays.asList(new NetworkResponse(401)));
// awaitTerminalEvent will wait forever if we don't advance time enough
scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
subscriber.awaitTerminalEvent();
subscriber.assertReceivedOnNext(Arrays.asList(new NetworkResponse(401), new NetworkResponse(200)));
// TestSubscriber provides many useful methods
subscriber.assertNoErrors();
subscriber.assertValueCount(2);
subscriber.assertUnsubscribed();
}
Aggregations