Search in sources :

Example 16 with Worker

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

the class TrampolineSchedulerInternalTest method dispose.

@Test
public void dispose() {
    Worker w = Schedulers.trampoline().createWorker();
    assertFalse(w.isDisposed());
    w.dispose();
    assertTrue(w.isDisposed());
    assertEquals(EmptyDisposable.INSTANCE, w.schedule(Functions.EMPTY_RUNNABLE));
}
Also used : Worker(io.reactivex.Scheduler.Worker) Test(org.junit.Test)

Example 17 with Worker

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

the class RxJavaPluginsTest method verifyThread.

private static void verifyThread(Scheduler scheduler, String expectedThreadName) throws AssertionError {
    assertNotNull(scheduler);
    Worker w = scheduler.createWorker();
    try {
        final AtomicReference<Thread> value = new AtomicReference<Thread>();
        final CountDownLatch cdl = new CountDownLatch(1);
        w.schedule(new Runnable() {

            @Override
            public void run() {
                value.set(Thread.currentThread());
                cdl.countDown();
            }
        });
        cdl.await();
        Thread t = value.get();
        assertNotNull(t);
        assertTrue(expectedThreadName.equals(t.getName()));
    } catch (Exception e) {
        fail();
    } finally {
        w.dispose();
    }
}
Also used : Worker(io.reactivex.Scheduler.Worker)

Example 18 with Worker

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

the class AbstractSchedulerConcurrencyTests method recursionFromOuterActionAndUnsubscribeInside.

@Test
public void recursionFromOuterActionAndUnsubscribeInside() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final Worker inner = getScheduler().createWorker();
    try {
        inner.schedule(new Runnable() {

            int i;

            @Override
            public void run() {
                i++;
                if (i % 100000 == 0) {
                    System.out.println(i + "  Total Memory: " + Runtime.getRuntime().totalMemory() + "  Free: " + Runtime.getRuntime().freeMemory());
                }
                if (i < 1000000L) {
                    inner.schedule(this);
                } else {
                    latch.countDown();
                }
            }
        });
        latch.await();
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.Scheduler.Worker) Test(org.junit.Test)

Example 19 with Worker

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

the class AbstractSchedulerConcurrencyTests method testRecursionAndOuterUnsubscribe.

@Test
public void testRecursionAndOuterUnsubscribe() throws InterruptedException {
    // use latches instead of Thread.sleep
    final CountDownLatch latch = new CountDownLatch(10);
    final CountDownLatch completionLatch = new CountDownLatch(1);
    final Worker inner = getScheduler().createWorker();
    try {
        Flowable<Integer> obs = Flowable.unsafeCreate(new Publisher<Integer>() {

            @Override
            public void subscribe(final Subscriber<? super Integer> observer) {
                inner.schedule(new Runnable() {

                    @Override
                    public void run() {
                        observer.onNext(42);
                        latch.countDown();
                        // this will recursively schedule this task for execution again
                        inner.schedule(this);
                    }
                });
                observer.onSubscribe(new Subscription() {

                    @Override
                    public void cancel() {
                        inner.dispose();
                        observer.onComplete();
                        completionLatch.countDown();
                    }

                    @Override
                    public void request(long n) {
                    }
                });
            }
        });
        final AtomicInteger count = new AtomicInteger();
        final AtomicBoolean completed = new AtomicBoolean(false);
        ResourceSubscriber<Integer> s = new ResourceSubscriber<Integer>() {

            @Override
            public void onComplete() {
                System.out.println("Completed");
                completed.set(true);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("Error");
            }

            @Override
            public void onNext(Integer args) {
                count.incrementAndGet();
                System.out.println(args);
            }
        };
        obs.subscribe(s);
        if (!latch.await(5000, TimeUnit.MILLISECONDS)) {
            fail("Timed out waiting on onNext latch");
        }
        // now unsubscribe and ensure it stops the recursive loop
        s.dispose();
        System.out.println("unsubscribe");
        if (!completionLatch.await(5000, TimeUnit.MILLISECONDS)) {
            fail("Timed out waiting on completion latch");
        }
        // the count can be 10 or higher due to thread scheduling of the unsubscribe vs the scheduler looping to emit the count
        assertTrue(count.get() >= 10);
        assertTrue(completed.get());
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.Scheduler.Worker) Test(org.junit.Test)

Example 20 with Worker

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

the class AbstractSchedulerConcurrencyTests method testRecursion.

@Test
public void testRecursion() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final Worker inner = getScheduler().createWorker();
    try {
        inner.schedule(new Runnable() {

            private long i;

            @Override
            public void run() {
                i++;
                if (i % 100000 == 0) {
                    System.out.println(i + "  Total Memory: " + Runtime.getRuntime().totalMemory() + "  Free: " + Runtime.getRuntime().freeMemory());
                }
                if (i < 1000000L) {
                    inner.schedule(this);
                } else {
                    latch.countDown();
                }
            }
        });
        latch.await();
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.Scheduler.Worker) Test(org.junit.Test)

Aggregations

Worker (io.reactivex.Scheduler.Worker)61 Test (org.junit.Test)40 CountingRunnable (io.reactivex.android.testutil.CountingRunnable)20 Disposable (io.reactivex.disposables.Disposable)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Ignore (org.junit.Ignore)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)3 EmptyDisposable (io.reactivex.internal.disposables.EmptyDisposable)2 Scheduler (io.reactivex.Scheduler)1 ConditionalSubscriber (io.reactivex.internal.fuseable.ConditionalSubscriber)1 SubscribeOnSubscriber (io.reactivex.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber)1 SpscArrayQueue (io.reactivex.internal.queue.SpscArrayQueue)1 ComputationScheduler (io.reactivex.internal.schedulers.ComputationScheduler)1 IoScheduler (io.reactivex.internal.schedulers.IoScheduler)1 NewThreadWorker (io.reactivex.internal.schedulers.NewThreadWorker)1 ScheduledWorker (io.reactivex.internal.schedulers.SingleScheduler.ScheduledWorker)1 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)1 TestScheduler (io.reactivex.schedulers.TestScheduler)1 TestSubscriber (io.reactivex.subscribers.TestSubscriber)1