Search in sources :

Example 51 with Worker

use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.

the class TrampolineSchedulerInternalTest method reentrantScheduleInterrupt.

@Test
@SuppressUndeliverable
public void reentrantScheduleInterrupt() {
    final Worker w = Schedulers.trampoline().createWorker();
    try {
        final int[] calls = { 0 };
        Thread.currentThread().interrupt();
        w.schedule(new Runnable() {

            @Override
            public void run() {
                calls[0]++;
            }
        }, 1, TimeUnit.DAYS);
        assertTrue(Thread.interrupted());
        assertEquals(0, calls[0]);
    } finally {
        w.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 52 with Worker

use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.

the class DeferredScalarSubscriberTest method emissionRequestRace.

@Test
public void emissionRequestRace() {
    Worker w = Schedulers.computation().createWorker();
    try {
        for (int i = 0; i < 10000; i++) {
            final TestSubscriber<Integer> ts = TestSubscriber.create(0L);
            TestingDeferredScalarSubscriber ds = new TestingDeferredScalarSubscriber(ts);
            ds.setupDownstream();
            ds.onNext(1);
            final AtomicInteger ready = new AtomicInteger(2);
            w.schedule(new Runnable() {

                @Override
                public void run() {
                    ready.decrementAndGet();
                    while (ready.get() != 0) {
                    }
                    ts.request(1);
                }
            });
            ready.decrementAndGet();
            while (ready.get() != 0) {
            }
            ds.onComplete();
            ts.awaitDone(5, TimeUnit.SECONDS);
            ts.assertValues(1);
            ts.assertNoErrors();
            ts.assertComplete();
        }
    } finally {
        w.dispose();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 53 with Worker

use of io.reactivex.rxjava3.core.Scheduler.Worker 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.rxjava3.core.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 54 with Worker

use of io.reactivex.rxjava3.core.Scheduler.Worker 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.rxjava3.core.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 55 with Worker

use of io.reactivex.rxjava3.core.Scheduler.Worker 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.rxjava3.core.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Worker (io.reactivex.rxjava3.core.Scheduler.Worker)91 Test (org.junit.Test)87 Disposable (io.reactivex.rxjava3.disposables.Disposable)28 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)22 Scheduler (io.reactivex.rxjava3.core.Scheduler)18 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Ignore (org.junit.Ignore)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)4 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)4 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)4 IoScheduler (io.reactivex.rxjava3.internal.schedulers.IoScheduler)3 WorkerCallback (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ThrowingRunnable (org.junit.function.ThrowingRunnable)3 Message (android.os.Message)2 CompositeDisposable (io.reactivex.rxjava3.disposables.CompositeDisposable)2 TestException (io.reactivex.rxjava3.exceptions.TestException)2 NewThreadWorker (io.reactivex.rxjava3.internal.schedulers.NewThreadWorker)2