Search in sources :

Example 71 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class SchedulerWorkerTest method testCurrentTimeDriftBackwards.

@Test
public void testCurrentTimeDriftBackwards() throws Exception {
    CustomDriftScheduler s = new CustomDriftScheduler();
    Scheduler.Worker w = s.createWorker();
    try {
        final List<Long> times = new ArrayList<Long>();
        Disposable d = w.schedulePeriodically(new Runnable() {

            @Override
            public void run() {
                times.add(System.currentTimeMillis());
            }
        }, 100, 100, TimeUnit.MILLISECONDS);
        Thread.sleep(150);
        s.drift = -TimeUnit.SECONDS.toNanos(1) - Scheduler.clockDriftTolerance();
        Thread.sleep(400);
        d.dispose();
        Thread.sleep(150);
        System.out.println("Runs: " + times.size());
        for (int i = 0; i < times.size() - 1; i++) {
            long diff = times.get(i + 1) - times.get(i);
            System.out.println("Diff #" + i + ": " + diff);
            assertTrue("" + i + ":" + diff, diff < 150 && diff > 50);
        }
        assertTrue("Too few invocations: " + times.size(), times.size() > 2);
    } finally {
        w.dispose();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) Scheduler(io.reactivex.Scheduler) Test(org.junit.Test)

Example 72 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class TestSchedulerTest method testImmediateUnsubscribes2.

@Test
public final void testImmediateUnsubscribes2() {
    TestScheduler s = new TestScheduler();
    final Scheduler.Worker inner = s.createWorker();
    try {
        final AtomicInteger counter = new AtomicInteger(0);
        final Disposable subscription = inner.schedule(new Runnable() {

            @Override
            public void run() {
                counter.incrementAndGet();
                System.out.println("counter: " + counter.get());
                inner.schedule(this);
            }
        });
        subscription.dispose();
        assertEquals(0, counter.get());
    } finally {
        inner.dispose();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) Worker(io.reactivex.Scheduler.Worker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestScheduler(io.reactivex.schedulers.TestScheduler) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 73 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class TrampolineSchedulerTest method testTrampolineWorkerHandlesConcurrentScheduling.

/**
     * This is a regression test for #1702. Concurrent work scheduling that is improperly synchronized can cause an
     * action to be added or removed onto the priority queue during a poll, which can result in NPEs during queue
     * sifting. While it is difficult to isolate the issue directly, we can easily trigger the behavior by spamming the
     * trampoline with enqueue requests from multiple threads concurrently.
     */
@Test
public void testTrampolineWorkerHandlesConcurrentScheduling() {
    final Worker trampolineWorker = Schedulers.trampoline().createWorker();
    final Subscriber<Object> observer = TestHelper.mockSubscriber();
    final TestSubscriber<Disposable> ts = new TestSubscriber<Disposable>(observer);
    // Spam the trampoline with actions.
    Flowable.range(0, 50).flatMap(new Function<Integer, Publisher<Disposable>>() {

        @Override
        public Publisher<Disposable> apply(Integer count) {
            return Flowable.interval(1, TimeUnit.MICROSECONDS).map(new Function<Long, Disposable>() {

                @Override
                public Disposable apply(Long ount1) {
                    return trampolineWorker.schedule(Functions.EMPTY_RUNNABLE);
                }
            }).take(100);
        }
    }).subscribeOn(Schedulers.computation()).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Worker(io.reactivex.Scheduler.Worker) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test)

Example 74 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class ExecutorSchedulerTest method testCancelledTasksDontRun.

@Test
public void testCancelledTasksDontRun() {
    final AtomicInteger calls = new AtomicInteger();
    Runnable task = new Runnable() {

        @Override
        public void run() {
            calls.getAndIncrement();
        }
    };
    TestExecutor exec = new TestExecutor();
    Scheduler custom = Schedulers.from(exec);
    Worker w = custom.createWorker();
    try {
        Disposable s1 = w.schedule(task);
        Disposable s2 = w.schedule(task);
        Disposable s3 = w.schedule(task);
        s1.dispose();
        s2.dispose();
        s3.dispose();
        exec.executeAll();
        assertEquals(0, calls.get());
    } finally {
        w.dispose();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) EmptyDisposable(io.reactivex.internal.disposables.EmptyDisposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Worker(io.reactivex.Scheduler.Worker)

Example 75 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class ExecutorSchedulerTest method reuseScheduledExecutor.

@Test
public void reuseScheduledExecutor() throws Exception {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    try {
        Scheduler s = Schedulers.from(exec);
        final CountDownLatch cdl = new CountDownLatch(8);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                cdl.countDown();
            }
        };
        s.scheduleDirect(r);
        s.scheduleDirect(r, 10, TimeUnit.MILLISECONDS);
        Disposable d = s.schedulePeriodicallyDirect(r, 10, 10, TimeUnit.MILLISECONDS);
        try {
            assertTrue(cdl.await(5, TimeUnit.SECONDS));
        } finally {
            d.dispose();
        }
    } finally {
        exec.shutdown();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) EmptyDisposable(io.reactivex.internal.disposables.EmptyDisposable)

Aggregations

Disposable (io.reactivex.disposables.Disposable)224 Test (org.junit.Test)95 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)31 Logger (chat.rocket.android.helper.Logger)19 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 Worker (io.reactivex.Scheduler.Worker)15 List (java.util.List)14 Single (io.reactivex.Single)13 Function (io.reactivex.functions.Function)13 NonNull (io.reactivex.annotations.NonNull)12 Nullable (android.support.annotation.Nullable)11 Bundle (android.os.Bundle)10 CountingRunnable (io.reactivex.android.testutil.CountingRunnable)10 EmptyDisposable (io.reactivex.internal.disposables.EmptyDisposable)10 TestSubscriber (io.reactivex.subscribers.TestSubscriber)10 Optional (com.fernandocejas.arrow.optional.Optional)9 Schedulers (io.reactivex.schedulers.Schedulers)9 TimeUnit (java.util.concurrent.TimeUnit)8 NonNull (android.support.annotation.NonNull)7