Search in sources :

Example 1 with Scheduler

use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.

the class RxAndroidPluginsTest method initMainThreadHandlerCalled.

@Test
public void initMainThreadHandlerCalled() {
    final AtomicReference<Callable<Scheduler>> schedulerRef = new AtomicReference<>();
    final Scheduler newScheduler = new EmptyScheduler();
    RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() {

        @Override
        public Scheduler apply(Callable<Scheduler> scheduler) {
            schedulerRef.set(scheduler);
            return newScheduler;
        }
    });
    Callable<Scheduler> scheduler = new Callable<Scheduler>() {

        @Override
        public Scheduler call() throws Exception {
            throw new AssertionError();
        }
    };
    Scheduler actual = RxAndroidPlugins.initMainThreadScheduler(scheduler);
    assertSame(newScheduler, actual);
    assertSame(scheduler, schedulerRef.get());
}
Also used : EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) Scheduler(io.reactivex.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 2 with Scheduler

use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.

the class RxAndroidPluginsTest method mainThreadHandlerCalled.

@Test
public void mainThreadHandlerCalled() {
    final AtomicReference<Scheduler> schedulerRef = new AtomicReference<>();
    final Scheduler newScheduler = new EmptyScheduler();
    RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {

        @Override
        public Scheduler apply(Scheduler scheduler) {
            schedulerRef.set(scheduler);
            return newScheduler;
        }
    });
    Scheduler scheduler = new EmptyScheduler();
    Scheduler actual = RxAndroidPlugins.onMainThreadScheduler(scheduler);
    assertSame(newScheduler, actual);
    assertSame(scheduler, schedulerRef.get());
}
Also used : EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) Scheduler(io.reactivex.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with Scheduler

use of io.reactivex.Scheduler in project RxAndroid by ReactiveX.

the class RxAndroidPluginsTest method resetClearsInitMainThreadHandler.

@Test
public void resetClearsInitMainThreadHandler() throws Exception {
    RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() {

        @Override
        public Scheduler apply(Callable<Scheduler> scheduler) {
            throw new AssertionError();
        }
    });
    final Scheduler scheduler = new EmptyScheduler();
    Callable<Scheduler> schedulerCallable = new Callable<Scheduler>() {

        @Override
        public Scheduler call() throws Exception {
            return scheduler;
        }
    };
    RxAndroidPlugins.reset();
    Scheduler actual = RxAndroidPlugins.initMainThreadScheduler(schedulerCallable);
    assertSame(schedulerCallable.call(), actual);
}
Also used : EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) Scheduler(io.reactivex.Scheduler) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 4 with Scheduler

use of io.reactivex.Scheduler in project RxJava by ReactiveX.

the class BehaviorProcessorTest method testEmissionSubscriptionRace.

// FIXME RS subscribers are not allowed to throw
//    @Test
//    public void testOnErrorThrowsDoesntPreventDelivery() {
//        BehaviorSubject<String> ps = BehaviorSubject.create();
//
//        ps.subscribe();
//        TestSubscriber<String> ts = new TestSubscriber<String>();
//        ps.subscribe(ts);
//
//        try {
//            ps.onError(new RuntimeException("an exception"));
//            fail("expect OnErrorNotImplementedException");
//        } catch (OnErrorNotImplementedException e) {
//            // ignore
//        }
//        // even though the onError above throws we should still receive it on the other subscriber
//        assertEquals(1, ts.getOnErrorEvents().size());
//    }
// FIXME RS subscribers are not allowed to throw
//    /**
//     * This one has multiple failures so should get a CompositeException
//     */
//    @Test
//    public void testOnErrorThrowsDoesntPreventDelivery2() {
//        BehaviorSubject<String> ps = BehaviorSubject.create();
//
//        ps.subscribe();
//        ps.subscribe();
//        TestSubscriber<String> ts = new TestSubscriber<String>();
//        ps.subscribe(ts);
//        ps.subscribe();
//        ps.subscribe();
//        ps.subscribe();
//
//        try {
//            ps.onError(new RuntimeException("an exception"));
//            fail("expect OnErrorNotImplementedException");
//        } catch (CompositeException e) {
//            // we should have 5 of them
//            assertEquals(5, e.getExceptions().size());
//        }
//        // even though the onError above throws we should still receive it on the other subscriber
//        assertEquals(1, ts.getOnErrorEvents().size());
//    }
@Test
public void testEmissionSubscriptionRace() throws Exception {
    Scheduler s = Schedulers.io();
    Scheduler.Worker worker = Schedulers.io().createWorker();
    try {
        for (int i = 0; i < 50000; i++) {
            if (i % 1000 == 0) {
                System.out.println(i);
            }
            final BehaviorProcessor<Object> rs = BehaviorProcessor.create();
            final CountDownLatch finish = new CountDownLatch(1);
            final CountDownLatch start = new CountDownLatch(1);
            worker.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        start.await();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    rs.onNext(1);
                }
            });
            final AtomicReference<Object> o = new AtomicReference<Object>();
            rs.subscribeOn(s).observeOn(Schedulers.io()).subscribe(new DefaultSubscriber<Object>() {

                @Override
                public void onComplete() {
                    o.set(-1);
                    finish.countDown();
                }

                @Override
                public void onError(Throwable e) {
                    o.set(e);
                    finish.countDown();
                }

                @Override
                public void onNext(Object t) {
                    o.set(t);
                    finish.countDown();
                }
            });
            start.countDown();
            if (!finish.await(5, TimeUnit.SECONDS)) {
                System.out.println(o.get());
                System.out.println(rs.hasSubscribers());
                rs.onComplete();
                Assert.fail("Timeout @ " + i);
                break;
            } else {
                Assert.assertEquals(1, o.get());
                worker.schedule(new Runnable() {

                    @Override
                    public void run() {
                        rs.onComplete();
                    }
                });
            }
        }
    } finally {
        worker.dispose();
    }
}
Also used : Scheduler(io.reactivex.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestException(io.reactivex.exceptions.TestException) MissingBackpressureException(io.reactivex.exceptions.MissingBackpressureException) Test(org.junit.Test)

Example 5 with Scheduler

use of io.reactivex.Scheduler in project RxJava by ReactiveX.

the class NewThreadSchedulerTest method npeRegression.

/**
     * Regression test to ensure there is no NPE when the worker has been disposed.
     * @throws Exception on error
     */
@Test
public void npeRegression() throws Exception {
    Scheduler s = getScheduler();
    NewThreadWorker w = (NewThreadWorker) s.createWorker();
    w.dispose();
    //This method used to throw a NPE when the worker has been disposed and the parent is null
    w.scheduleActual(new Runnable() {

        @Override
        public void run() {
        }
    }, 0, TimeUnit.MILLISECONDS, null);
}
Also used : Scheduler(io.reactivex.Scheduler) NewThreadWorker(io.reactivex.internal.schedulers.NewThreadWorker)

Aggregations

Scheduler (io.reactivex.Scheduler)12 Test (org.junit.Test)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 EmptyScheduler (io.reactivex.android.testutil.EmptyScheduler)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 NewThreadWorker (io.reactivex.internal.schedulers.NewThreadWorker)2 Callable (java.util.concurrent.Callable)2 Completable (io.reactivex.Completable)1 Flowable (io.reactivex.Flowable)1 Worker (io.reactivex.Scheduler.Worker)1 MissingBackpressureException (io.reactivex.exceptions.MissingBackpressureException)1 TestException (io.reactivex.exceptions.TestException)1 TestScheduler (io.reactivex.schedulers.TestScheduler)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Ignore (org.junit.Ignore)1