Search in sources :

Example 66 with Scheduler

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

the class ObservableDebounceTest method before.

@Before
public void before() {
    scheduler = new TestScheduler();
    observer = TestHelper.mockObserver();
    innerScheduler = scheduler.createWorker();
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 67 with Scheduler

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

the class ObservableDebounceTest method debounceWithError.

@Test
public void debounceWithError() {
    Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() {

        @Override
        public void subscribe(Observer<? super String> observer) {
            observer.onSubscribe(Disposable.empty());
            Exception error = new TestException();
            // Should be published since "two" will arrive after the timeout expires.
            publishNext(observer, 100, "one");
            // Should be skipped since onError will arrive before the timeout expires.
            publishNext(observer, 600, "two");
            // Should be published as soon as the timeout expires.
            publishError(observer, 700, error);
        }
    });
    Observable<String> sampled = source.debounce(400, TimeUnit.MILLISECONDS, scheduler);
    sampled.subscribe(observer);
    scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    // 100 + 400 means it triggers at 500
    scheduler.advanceTimeTo(500, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onNext("one");
    scheduler.advanceTimeTo(701, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 68 with Scheduler

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

the class ObservableDelayTest method onErrorFinal.

@Test
public void onErrorFinal() {
    TestScheduler scheduler = new TestScheduler();
    Observable.error(new TestException()).delay(1, TimeUnit.MILLISECONDS, scheduler).subscribe(new DisposableObserver<Object>() {

        @Override
        public void onNext(Object value) {
        }

        @Override
        public void onError(Throwable e) {
            throw new TestException();
        }

        @Override
        public void onComplete() {
        }
    });
    try {
        scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
        fail("Should have thrown");
    } catch (TestException ex) {
    // expected
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 69 with Scheduler

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

the class ObservableDelayTest method delayWithObservableAsTimed.

@Test
public void delayWithObservableAsTimed() {
    Observable<Long> source = Observable.interval(1L, TimeUnit.SECONDS, scheduler).take(3);
    final Observable<Long> delayer = Observable.timer(500L, TimeUnit.MILLISECONDS, scheduler);
    Function<Long, Observable<Long>> delayFunc = new Function<Long, Observable<Long>>() {

        @Override
        public Observable<Long> apply(Long t1) {
            return delayer;
        }
    };
    Observable<Long> delayed = source.delay(delayFunc);
    delayed.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(1499L, TimeUnit.MILLISECONDS);
    verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(1500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(0L);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(2400L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(2500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(1L);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(3400L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(3500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(2L);
    verify(observer, times(1)).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 70 with Scheduler

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

the class BlockingObservableMostRecentTest method singleSourceManyIterators.

@Test
public void singleSourceManyIterators() {
    TestScheduler scheduler = new TestScheduler();
    Observable<Long> source = Observable.interval(1, TimeUnit.SECONDS, scheduler).take(10);
    Iterable<Long> iter = source.blockingMostRecent(-1L);
    for (int j = 0; j < 3; j++) {
        Iterator<Long> it = iter.iterator();
        Assert.assertEquals(Long.valueOf(-1), it.next());
        for (int i = 0; i < 9; i++) {
            scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
            Assert.assertTrue(it.hasNext());
            Assert.assertEquals(Long.valueOf(i), it.next());
        }
        scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
        Assert.assertFalse(it.hasNext());
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest)

Aggregations

Test (org.junit.Test)169 Disposable (io.reactivex.rxjava3.disposables.Disposable)69 Scheduler (io.reactivex.rxjava3.core.Scheduler)61 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)54 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)50 TestException (io.reactivex.rxjava3.exceptions.TestException)34 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)32 InOrder (org.mockito.InOrder)32 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)20 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)8 Observable (io.reactivex.rxjava3.core.Observable)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 Action (io.reactivex.rxjava3.functions.Action)5 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5