use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class TrampolineSchedulerTest method trampolineWorkerHandlesConcurrentScheduling.
/**
* 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 trampolineWorkerHandlesConcurrentScheduling() {
final Worker trampolineWorker = Schedulers.trampoline().createWorker();
final Subscriber<Object> subscriber = TestHelper.mockSubscriber();
final TestSubscriber<Disposable> ts = new TestSubscriber<>(subscriber);
// 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.awaitDone(5, TimeUnit.SECONDS);
ts.assertNoErrors();
}
use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class ExecutorSchedulerTest method rejectingExecutorWorker.
@Test
public void rejectingExecutorWorker() {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.shutdown();
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
Worker s = Schedulers.from(exec).createWorker();
assertSame(EmptyDisposable.INSTANCE, s.schedule(Functions.EMPTY_RUNNABLE));
s = Schedulers.from(exec).createWorker();
assertSame(EmptyDisposable.INSTANCE, s.schedule(Functions.EMPTY_RUNNABLE, 10, TimeUnit.MILLISECONDS));
s = Schedulers.from(exec).createWorker();
assertSame(EmptyDisposable.INSTANCE, s.schedulePeriodically(Functions.EMPTY_RUNNABLE, 10, 10, TimeUnit.MILLISECONDS));
TestHelper.assertUndeliverable(errors, 0, RejectedExecutionException.class);
TestHelper.assertUndeliverable(errors, 1, RejectedExecutionException.class);
TestHelper.assertUndeliverable(errors, 2, RejectedExecutionException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class NewThreadSchedulerTest method npeRegression.
/**
* Regression test to ensure there is no NPE when the worker has been disposed.
* @throws Exception on error
*/
@Test
@SuppressUndeliverable
public void npeRegression() throws Exception {
Scheduler s = getScheduler();
NewThreadWorker w = (NewThreadWorker) s.createWorker();
w.dispose();
// This method used to throw a NPE when the worker has been disposed and the parent is null
w.scheduleActual(new Runnable() {
@Override
public void run() {
}
}, 0, TimeUnit.MILLISECONDS, null);
}
use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class ExecutorSchedulerFairTest method cancelledWorkerDoesntRunTasks.
@Test
public void cancelledWorkerDoesntRunTasks() {
final AtomicInteger calls = new AtomicInteger();
Runnable task = new Runnable() {
@Override
public void run() {
calls.getAndIncrement();
}
};
TestExecutor exec = new TestExecutor();
Scheduler custom = Schedulers.from(exec, false, true);
Worker w = custom.createWorker();
try {
w.schedule(task);
w.schedule(task);
w.schedule(task);
} finally {
w.dispose();
}
exec.executeAll();
assertEquals(0, calls.get());
}
use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class ExecutorSchedulerTest method cancelledWorkerDoesntRunTasks.
@Test
public void cancelledWorkerDoesntRunTasks() {
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 {
w.schedule(task);
w.schedule(task);
w.schedule(task);
} finally {
w.dispose();
}
exec.executeAll();
assertEquals(0, calls.get());
}
Aggregations