use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class TrampolineSchedulerInternalTest method dispose.
@Test
public void dispose() {
Worker w = Schedulers.trampoline().createWorker();
assertFalse(w.isDisposed());
w.dispose();
assertTrue(w.isDisposed());
assertEquals(EmptyDisposable.INSTANCE, w.schedule(Functions.EMPTY_RUNNABLE));
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class RxJavaPluginsTest method verifyThread.
private static void verifyThread(Scheduler scheduler, String expectedThreadName) throws AssertionError {
assertNotNull(scheduler);
Worker w = scheduler.createWorker();
try {
final AtomicReference<Thread> value = new AtomicReference<Thread>();
final CountDownLatch cdl = new CountDownLatch(1);
w.schedule(new Runnable() {
@Override
public void run() {
value.set(Thread.currentThread());
cdl.countDown();
}
});
cdl.await();
Thread t = value.get();
assertNotNull(t);
assertTrue(expectedThreadName.equals(t.getName()));
} catch (Exception e) {
fail();
} finally {
w.dispose();
}
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class AbstractSchedulerConcurrencyTests method recursionFromOuterActionAndUnsubscribeInside.
@Test
public void recursionFromOuterActionAndUnsubscribeInside() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Worker inner = getScheduler().createWorker();
try {
inner.schedule(new Runnable() {
int i;
@Override
public void run() {
i++;
if (i % 100000 == 0) {
System.out.println(i + " Total Memory: " + Runtime.getRuntime().totalMemory() + " Free: " + Runtime.getRuntime().freeMemory());
}
if (i < 1000000L) {
inner.schedule(this);
} else {
latch.countDown();
}
}
});
latch.await();
} finally {
inner.dispose();
}
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class AbstractSchedulerConcurrencyTests method testRecursionAndOuterUnsubscribe.
@Test
public void testRecursionAndOuterUnsubscribe() throws InterruptedException {
// use latches instead of Thread.sleep
final CountDownLatch latch = new CountDownLatch(10);
final CountDownLatch completionLatch = new CountDownLatch(1);
final Worker inner = getScheduler().createWorker();
try {
Flowable<Integer> obs = Flowable.unsafeCreate(new Publisher<Integer>() {
@Override
public void subscribe(final Subscriber<? super Integer> observer) {
inner.schedule(new Runnable() {
@Override
public void run() {
observer.onNext(42);
latch.countDown();
// this will recursively schedule this task for execution again
inner.schedule(this);
}
});
observer.onSubscribe(new Subscription() {
@Override
public void cancel() {
inner.dispose();
observer.onComplete();
completionLatch.countDown();
}
@Override
public void request(long n) {
}
});
}
});
final AtomicInteger count = new AtomicInteger();
final AtomicBoolean completed = new AtomicBoolean(false);
ResourceSubscriber<Integer> s = new ResourceSubscriber<Integer>() {
@Override
public void onComplete() {
System.out.println("Completed");
completed.set(true);
}
@Override
public void onError(Throwable e) {
System.out.println("Error");
}
@Override
public void onNext(Integer args) {
count.incrementAndGet();
System.out.println(args);
}
};
obs.subscribe(s);
if (!latch.await(5000, TimeUnit.MILLISECONDS)) {
fail("Timed out waiting on onNext latch");
}
// now unsubscribe and ensure it stops the recursive loop
s.dispose();
System.out.println("unsubscribe");
if (!completionLatch.await(5000, TimeUnit.MILLISECONDS)) {
fail("Timed out waiting on completion latch");
}
// the count can be 10 or higher due to thread scheduling of the unsubscribe vs the scheduler looping to emit the count
assertTrue(count.get() >= 10);
assertTrue(completed.get());
} finally {
inner.dispose();
}
}
use of io.reactivex.Scheduler.Worker in project RxJava by ReactiveX.
the class AbstractSchedulerConcurrencyTests method testRecursion.
@Test
public void testRecursion() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final Worker inner = getScheduler().createWorker();
try {
inner.schedule(new Runnable() {
private long i;
@Override
public void run() {
i++;
if (i % 100000 == 0) {
System.out.println(i + " Total Memory: " + Runtime.getRuntime().totalMemory() + " Free: " + Runtime.getRuntime().freeMemory());
}
if (i < 1000000L) {
inner.schedule(this);
} else {
latch.countDown();
}
}
});
latch.await();
} finally {
inner.dispose();
}
}
Aggregations