Search in sources :

Example 6 with Scheduler

use of io.reactivex.Scheduler in project RxRelay by JakeWharton.

the class ReplayRelayBoundedConcurrencyTest method testReplaySubjectEmissionSubscriptionRace.

@Test
public void testReplaySubjectEmissionSubscriptionRace() 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 ReplayRelay<Object> rs = ReplayRelay.createWithSize(2);
            final CountDownLatch finish = new CountDownLatch(1);
            final CountDownLatch start = new CountDownLatch(1);
            //                int j = i;
            worker.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        start.await();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    //                        System.out.println("> " + j);
                    rs.accept(1);
                }
            });
            final AtomicReference<Object> o = new AtomicReference<Object>();
            rs.subscribeOn(s).observeOn(Schedulers.io()).subscribe(new DefaultObserver<Object>() {

                @Override
                protected void onStart() {
                    super.onStart();
                }

                @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.hasObservers());
                Assert.fail("Timeout @ " + i);
                break;
            } else {
                Assert.assertEquals(1, o.get());
            }
        }
    } finally {
        worker.dispose();
    }
}
Also used : Scheduler(io.reactivex.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with Scheduler

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

the class RxAndroidPluginsTest method resetClearsMainThreadHandler.

@Test
public void resetClearsMainThreadHandler() {
    RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {

        @Override
        public Scheduler apply(Scheduler scheduler) {
            throw new AssertionError();
        }
    });
    RxAndroidPlugins.reset();
    Scheduler scheduler = new EmptyScheduler();
    Scheduler actual = RxAndroidPlugins.onMainThreadScheduler(scheduler);
    assertSame(scheduler, actual);
}
Also used : EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) Scheduler(io.reactivex.Scheduler) Test(org.junit.Test)

Example 8 with Scheduler

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

the class AndroidSchedulersTest method mainThreadCallsThroughToHook.

@Test
public void mainThreadCallsThroughToHook() {
    final AtomicInteger called = new AtomicInteger();
    final Scheduler newScheduler = new EmptyScheduler();
    RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {

        @Override
        public Scheduler apply(Scheduler scheduler) {
            called.getAndIncrement();
            return newScheduler;
        }
    });
    assertSame(newScheduler, AndroidSchedulers.mainThread());
    assertEquals(1, called.get());
    assertSame(newScheduler, AndroidSchedulers.mainThread());
    assertEquals(2, called.get());
}
Also used : EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmptyScheduler(io.reactivex.android.testutil.EmptyScheduler) Scheduler(io.reactivex.Scheduler) Test(org.junit.Test)

Example 9 with Scheduler

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

the class SchedulerWhenTest method testRaceConditions.

@Test(timeout = 1000)
public void testRaceConditions() {
    Scheduler comp = Schedulers.computation();
    Scheduler limited = comp.when(new Function<Flowable<Flowable<Completable>>, Completable>() {

        @Override
        public Completable apply(Flowable<Flowable<Completable>> t) {
            return Completable.merge(Flowable.merge(t, 10));
        }
    });
    merge(just(just(1).subscribeOn(limited).observeOn(comp)).repeat(1000)).blockingSubscribe();
}
Also used : Completable(io.reactivex.Completable) Scheduler(io.reactivex.Scheduler) TestScheduler(io.reactivex.schedulers.TestScheduler) Flowable(io.reactivex.Flowable) Test(org.junit.Test)

Example 10 with Scheduler

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

the class NewThreadSchedulerTest method shutdownRejects.

// FIXME no longer testable due to internal changes
//    @Test(timeout = 3000)
//    public void testNoSelfInterrupt() throws InterruptedException {
//        Scheduler.Worker worker = Schedulers.newThread().createWorker();
//        try {
//            final CountDownLatch run = new CountDownLatch(1);
//            final CountDownLatch done = new CountDownLatch(1);
//            final AtomicReference<Throwable> exception = new AtomicReference<T>();
//            final AtomicBoolean interruptFlag = new AtomicBoolean();
//
//            ScheduledRunnable sa = (ScheduledRunnable)worker.schedule(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        run.await();
//                    } catch (InterruptedException ex) {
//                        exception.set(ex);
//                    }
//                }
//            });
//
//            sa.add(new Disposable() {
//                @Override
//                public void dispose() {
//                    interruptFlag.set(Thread.currentThread().isInterrupted());
//                    done.countDown();
//                }
//            });
//
//            run.countDown();
//
//            done.await();
//
//            Assert.assertEquals(null, exception.get());
//            Assert.assertFalse("Interrupted?!", interruptFlag.get());
//        } finally {
//            worker.dispose();
//        }
//    }
@Test
public void shutdownRejects() {
    final int[] calls = { 0 };
    Runnable r = new Runnable() {

        @Override
        public void run() {
            calls[0]++;
        }
    };
    Scheduler s = getScheduler();
    Worker w = s.createWorker();
    w.dispose();
    assertTrue(w.isDisposed());
    assertEquals(Disposables.disposed(), w.schedule(r));
    assertEquals(Disposables.disposed(), w.schedule(r, 1, TimeUnit.SECONDS));
    assertEquals(Disposables.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS));
    NewThreadWorker actual = (NewThreadWorker) w;
    CompositeDisposable cd = new CompositeDisposable();
    actual.scheduleActual(r, 1, TimeUnit.SECONDS, cd);
    assertEquals(0, cd.size());
    assertEquals(0, calls[0]);
}
Also used : Scheduler(io.reactivex.Scheduler) Worker(io.reactivex.Scheduler.Worker) NewThreadWorker(io.reactivex.internal.schedulers.NewThreadWorker) 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