Search in sources :

Example 41 with Worker

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

the class HandlerSchedulerTest method workerSchedulePeriodicallyReschedulesItself.

@Test
@Ignore("Implementation delegated to default RxJava implementation")
public void workerSchedulePeriodicallyReschedulesItself() {
    Worker worker = scheduler.createWorker();
    CountingRunnable counter = new CountingRunnable();
    worker.schedulePeriodically(counter, 1, 1, MINUTES);
    runUiThreadTasks();
    assertEquals(0, counter.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(1, counter.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(2, counter.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(3, counter.get());
}
Also used : CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Worker(io.reactivex.Scheduler.Worker) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 42 with Worker

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

the class FlowableReplayTest method testIssue2191_SchedulerUnsubscribe.

/**
     * Specifically test interaction with a Scheduler with subscribeOn.
     *
     * @throws Exception functional interfaces declare throws Exception
     */
@SuppressWarnings("unchecked")
@Test
public void testIssue2191_SchedulerUnsubscribe() throws Exception {
    // setup mocks
    Consumer<Integer> sourceNext = mock(Consumer.class);
    Action sourceCompleted = mock(Action.class);
    Action sourceUnsubscribed = mock(Action.class);
    final Scheduler mockScheduler = mock(Scheduler.class);
    final Disposable mockSubscription = mock(Disposable.class);
    Worker spiedWorker = workerSpy(mockSubscription);
    Subscriber<Integer> mockObserverBeforeConnect = TestHelper.mockSubscriber();
    Subscriber<Integer> mockObserverAfterConnect = TestHelper.mockSubscriber();
    when(mockScheduler.createWorker()).thenReturn(spiedWorker);
    // Flowable under test
    ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).subscribeOn(mockScheduler).replay();
    replay.subscribe(mockObserverBeforeConnect);
    replay.subscribe(mockObserverBeforeConnect);
    replay.connect();
    replay.subscribe(mockObserverAfterConnect);
    replay.subscribe(mockObserverAfterConnect);
    verify(mockObserverBeforeConnect, times(2)).onSubscribe((Subscription) any());
    verify(mockObserverAfterConnect, times(2)).onSubscribe((Subscription) any());
    // verify interactions
    verify(sourceNext, times(1)).accept(1);
    verify(sourceNext, times(1)).accept(2);
    verify(sourceNext, times(1)).accept(3);
    verify(sourceCompleted, times(1)).run();
    verify(mockScheduler, times(1)).createWorker();
    verify(spiedWorker, times(1)).schedule((Runnable) notNull());
    verifyObserverMock(mockObserverBeforeConnect, 2, 6);
    verifyObserverMock(mockObserverAfterConnect, 2, 6);
    // FIXME not supported
    //        verify(spiedWorker, times(1)).isUnsubscribed();
    // FIXME publish calls cancel too
    verify(spiedWorker, times(1)).dispose();
    verify(sourceUnsubscribed, never()).run();
    verifyNoMoreInteractions(sourceNext);
    verifyNoMoreInteractions(sourceCompleted);
    verifyNoMoreInteractions(sourceUnsubscribed);
    verifyNoMoreInteractions(spiedWorker);
    verifyNoMoreInteractions(mockSubscription);
    verifyNoMoreInteractions(mockScheduler);
    verifyNoMoreInteractions(mockObserverBeforeConnect);
    verifyNoMoreInteractions(mockObserverAfterConnect);
}
Also used : Disposable(io.reactivex.disposables.Disposable) Worker(io.reactivex.Scheduler.Worker)

Example 43 with Worker

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

the class FlowableSubscribeOnTest method deferredRequestRace.

@Test
public void deferredRequestRace() {
    for (int i = 0; i < 500; i++) {
        final TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
        Worker w = Schedulers.computation().createWorker();
        final SubscribeOnSubscriber<Integer> so = new SubscribeOnSubscriber<Integer>(ts, w, Flowable.<Integer>never(), true);
        ts.onSubscribe(so);
        final BooleanSubscription bs = new BooleanSubscription();
        try {
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    so.onSubscribe(bs);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    so.request(1);
                }
            };
            TestHelper.race(r1, r2);
        } finally {
            w.dispose();
        }
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) SubscribeOnSubscriber(io.reactivex.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber) Worker(io.reactivex.Scheduler.Worker)

Example 44 with Worker

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

the class ImmediateThinSchedulerTest method schedule.

@Test
public void schedule() {
    final int[] count = { 0 };
    Worker w = ImmediateThinScheduler.INSTANCE.createWorker();
    assertFalse(w.isDisposed());
    w.schedule(new Runnable() {

        @Override
        public void run() {
            count[0]++;
        }
    });
    assertEquals(1, count[0]);
}
Also used : Worker(io.reactivex.Scheduler.Worker) Test(org.junit.Test)

Example 45 with Worker

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

the class TrampolineSchedulerInternalTest method reentrantScheduleDispose.

@Test
public void reentrantScheduleDispose() {
    final Worker w = Schedulers.trampoline().createWorker();
    try {
        final int[] calls = { 0, 0 };
        w.schedule(new Runnable() {

            @Override
            public void run() {
                calls[0]++;
                w.schedule(new Runnable() {

                    @Override
                    public void run() {
                        calls[1]++;
                    }
                }).dispose();
            }
        });
        assertEquals(1, calls[0]);
        assertEquals(0, calls[1]);
    } finally {
        w.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