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();
}
}
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();
}
}
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();
}
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();
}
}
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();
}
}
Aggregations