Search in sources :

Example 6 with Scheduler

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();
}
Also used : Action0(rx.functions.Action0) TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Override(java.lang.Override) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 7 with Scheduler

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);
}
Also used : Scheduler(rx.Scheduler) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) ContentResolver(android.content.ContentResolver) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 8 with 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();
        }
    });
}
Also used : Scheduler(rx.Scheduler)

Example 9 with Scheduler

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);
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.client.sdk.models.organisationunit.OrganisationUnit) Scheduler(rx.Scheduler) ArrayList(java.util.ArrayList) Event(org.hisp.dhis.client.sdk.models.event.Event) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) EventFilters(org.hisp.dhis.client.sdk.core.event.EventFilters)

Example 10 with Scheduler

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();
}
Also used : Action0(rx.functions.Action0) TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Override(java.lang.Override) Test(org.junit.Test)

Aggregations

Scheduler (rx.Scheduler)10 Test (org.junit.Test)6 Action0 (rx.functions.Action0)3 Override (java.lang.Override)2 TestSubscriber (rx.observers.TestSubscriber)2 TestScheduler (rx.schedulers.TestScheduler)2 ContentResolver (android.content.ContentResolver)1 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)1 MapMaker (com.google.common.collect.MapMaker)1 StorIOContentResolver (com.pushtorefresh.storio.contentresolver.StorIOContentResolver)1 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)1 ArrayList (java.util.ArrayList)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1