use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class TrampolineSchedulerInternalTest method reentrantScheduleShutdown.
@Test
public void reentrantScheduleShutdown() {
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]++;
}
}, 1, TimeUnit.MILLISECONDS);
w.dispose();
}
});
assertEquals(1, calls[0]);
assertEquals(0, calls[1]);
} finally {
w.dispose();
}
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class TrampolineSchedulerInternalTest method reentrantScheduleInterrupt.
@Test(timeout = 5000)
public void reentrantScheduleInterrupt() {
final Worker w = Schedulers.trampoline().createWorker();
try {
final int[] calls = { 0 };
Thread.currentThread().interrupt();
w.schedule(new Runnable() {
@Override
public void run() {
calls[0]++;
}
}, 1, TimeUnit.DAYS);
assertTrue(Thread.interrupted());
assertEquals(0, calls[0]);
} finally {
w.dispose();
}
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class DeferredScalarSubscriberTest method emissionRequestRace.
@Test
public void emissionRequestRace() {
Worker w = Schedulers.computation().createWorker();
try {
for (int i = 0; i < 10000; i++) {
final TestSubscriber<Integer> ts = TestSubscriber.create(0L);
TestingDeferredScalarSubscriber ds = new TestingDeferredScalarSubscriber(ts);
ds.setupDownstream();
ds.onNext(1);
final AtomicInteger ready = new AtomicInteger(2);
w.schedule(new Runnable() {
@Override
public void run() {
ready.decrementAndGet();
while (ready.get() != 0) {
}
ts.request(1);
}
});
ready.decrementAndGet();
while (ready.get() != 0) {
}
ds.onComplete();
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
ts.assertValues(1);
ts.assertNoErrors();
ts.assertComplete();
}
} finally {
w.dispose();
}
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class DeferredScalarSubscriberTest method emissionRequestRace2.
@Test
public void emissionRequestRace2() {
Worker w = Schedulers.io().createWorker();
Worker w2 = Schedulers.io().createWorker();
int m = 10000;
if (Runtime.getRuntime().availableProcessors() < 3) {
m = 1000;
}
try {
for (int i = 0; i < m; i++) {
final TestSubscriber<Integer> ts = TestSubscriber.create(0L);
TestingDeferredScalarSubscriber ds = new TestingDeferredScalarSubscriber(ts);
ds.setupDownstream();
ds.onNext(1);
final AtomicInteger ready = new AtomicInteger(3);
w.schedule(new Runnable() {
@Override
public void run() {
ready.decrementAndGet();
while (ready.get() != 0) {
}
ts.request(1);
}
});
w2.schedule(new Runnable() {
@Override
public void run() {
ready.decrementAndGet();
while (ready.get() != 0) {
}
ts.request(1);
}
});
ready.decrementAndGet();
while (ready.get() != 0) {
}
ds.onComplete();
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
ts.assertValues(1);
ts.assertNoErrors();
ts.assertComplete();
}
} finally {
w.dispose();
w2.dispose();
}
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class AbstractSchedulerConcurrencyTests method testUnsubscribeRecursiveScheduleWithDelay.
@Test
public void testUnsubscribeRecursiveScheduleWithDelay() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch unsubscribeLatch = new CountDownLatch(1);
final AtomicInteger counter = new AtomicInteger();
final Worker inner = getScheduler().createWorker();
try {
inner.schedule(new Runnable() {
@Override
public void run() {
inner.schedule(new Runnable() {
long i = 1L;
@Override
public void run() {
if (i++ == 10) {
latch.countDown();
try {
// wait for unsubscribe to finish so we are not racing it
unsubscribeLatch.await();
} catch (InterruptedException e) {
// we expect the countDown if unsubscribe is not working
// or to be interrupted if unsubscribe is successful since
// the unsubscribe will interrupt it as it is calling Future.cancel(true)
// so we will ignore the stacktrace
}
}
counter.incrementAndGet();
inner.schedule(this, 10, TimeUnit.MILLISECONDS);
}
}, 10, TimeUnit.MILLISECONDS);
}
});
latch.await();
inner.dispose();
unsubscribeLatch.countDown();
// let time pass to see if the scheduler is still doing work
Thread.sleep(200);
assertEquals(10, counter.get());
} finally {
inner.dispose();
}
}
Aggregations