Search in sources :

Example 11 with Scheduler

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

the class BehaviorRelayTest method testEmissionSubscriptionRace.

@Test
@Ignore("OOMs")
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 BehaviorRelay<Object> rs = BehaviorRelay.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.accept(1);
                }
            });
            final AtomicReference<Object> o = new AtomicReference<Object>();
            rs.subscribeOn(s).observeOn(Schedulers.io()).subscribe(new DefaultObserver<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.hasObservers());
                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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with Scheduler

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

the class ReplayRelayConcurrencyTest method testReplayRelayEmissionSubscriptionRace.

@Test
public void testReplayRelayEmissionSubscriptionRace() 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.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.accept(1);
                }
            });
            final AtomicReference<Object> o = new AtomicReference<Object>();
            rs.subscribeOn(s).observeOn(Schedulers.io()).subscribe(new DefaultObserver<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.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)

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