use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.
the class SingleTimeoutTest method successTimeoutRace.
@Test
public void successTimeoutRace() {
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
final SingleSubject<Integer> subj = SingleSubject.create();
SingleSubject<Integer> fallback = SingleSubject.create();
final TestScheduler sch = new TestScheduler();
TestObserver<Integer> to = subj.timeout(1, TimeUnit.MILLISECONDS, sch, fallback).test();
Runnable r1 = new Runnable() {
@Override
public void run() {
subj.onSuccess(1);
}
};
Runnable r2 = new Runnable() {
@Override
public void run() {
sch.advanceTimeBy(1, TimeUnit.MILLISECONDS);
}
};
TestHelper.race(r1, r2);
if (!fallback.hasObservers()) {
to.assertResult(1);
} else {
to.assertEmpty();
}
}
}
use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.
the class SingleTimeoutTest method shouldUnsubscribeFromUnderlyingSubscriptionOnDispose.
@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() {
final PublishSubject<String> subject = PublishSubject.create();
final TestScheduler scheduler = new TestScheduler();
final TestObserver<String> observer = subject.single("").timeout(100, TimeUnit.MILLISECONDS, scheduler).test();
assertTrue(subject.hasObservers());
observer.dispose();
assertFalse(subject.hasObservers());
}
use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method workerDisposed.
@Test
public void workerDisposed() {
TestScheduler scheduler = new TestScheduler();
Worker w = scheduler.createWorker();
w.dispose();
assertTrue(w.isDisposed());
}
use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method nestedSchedule.
@Test
public final void nestedSchedule() {
final TestScheduler scheduler = new TestScheduler();
final Scheduler.Worker inner = scheduler.createWorker();
try {
final Runnable calledOp = mock(Runnable.class);
Flowable<Object> poller;
poller = Flowable.unsafeCreate(new Publisher<Object>() {
@Override
public void subscribe(final Subscriber<? super Object> aSubscriber) {
final BooleanSubscription bs = new BooleanSubscription();
aSubscriber.onSubscribe(bs);
inner.schedule(new Runnable() {
@Override
public void run() {
if (!bs.isCancelled()) {
calledOp.run();
inner.schedule(this, 5, TimeUnit.SECONDS);
}
}
});
}
});
InOrder inOrder = Mockito.inOrder(calledOp);
Disposable sub;
sub = poller.subscribe();
scheduler.advanceTimeTo(6, TimeUnit.SECONDS);
inOrder.verify(calledOp, times(2)).run();
sub.dispose();
scheduler.advanceTimeTo(11, TimeUnit.SECONDS);
inOrder.verify(calledOp, never()).run();
sub = poller.subscribe();
scheduler.advanceTimeTo(12, TimeUnit.SECONDS);
inOrder.verify(calledOp, times(1)).run();
} finally {
inner.dispose();
}
}
use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method withOnScheduleHook.
@Test
public void withOnScheduleHook() {
AtomicInteger run = new AtomicInteger();
AtomicInteger counter = new AtomicInteger();
RxJavaPlugins.setScheduleHandler(r -> {
counter.getAndIncrement();
return r;
});
try {
Runnable r = () -> run.getAndIncrement();
TestScheduler ts = new TestScheduler(true);
ts.createWorker().schedule(r);
ts.createWorker().schedule(r, 1, TimeUnit.SECONDS);
ts.advanceTimeBy(1, TimeUnit.SECONDS);
assertEquals(2, run.get());
assertEquals(2, counter.get());
ts = new TestScheduler();
ts.createWorker().schedule(r);
ts.createWorker().schedule(r, 1, TimeUnit.SECONDS);
ts.advanceTimeBy(1, TimeUnit.SECONDS);
assertEquals(4, run.get());
assertEquals(2, counter.get());
} finally {
RxJavaPlugins.setScheduleHandler(null);
}
}
Aggregations