use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class FlowableSubscribeOnTest method deferredRequestRace.
@Test
public void deferredRequestRace() {
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
Worker w = Schedulers.computation().createWorker();
final SubscribeOnSubscriber<Integer> so = new SubscribeOnSubscriber<>(ts, w, Flowable.<Integer>never(), true);
ts.onSubscribe(so);
final BooleanSubscription bs = new BooleanSubscription();
try {
Runnable r1 = new Runnable() {
@Override
public void run() {
so.onSubscribe(bs);
}
};
Runnable r2 = new Runnable() {
@Override
public void run() {
so.request(1);
}
};
TestHelper.race(r1, r2);
} finally {
w.dispose();
}
}
}
use of io.reactivex.rxjava3.core.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.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class CachedThreadSchedulerTest method shutdownRejects.
@Test
@SuppressUndeliverable
public void shutdownRejects() {
final int[] calls = { 0 };
Runnable r = new Runnable() {
@Override
public void run() {
calls[0]++;
}
};
IoScheduler s = new IoScheduler();
s.shutdown();
s.shutdown();
s.scheduleDirect(r);
s.scheduleDirect(r, 1, TimeUnit.SECONDS);
s.schedulePeriodicallyDirect(r, 1, 1, TimeUnit.SECONDS);
Worker w = s.createWorker();
w.dispose();
assertEquals(Disposable.disposed(), w.schedule(r));
assertEquals(Disposable.disposed(), w.schedule(r, 1, TimeUnit.SECONDS));
assertEquals(Disposable.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS));
assertEquals(0, calls[0]);
}
use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class AbstractSchedulerConcurrencyTests method unsubscribeRecursiveScheduleFromOutside.
@Test
public void unsubscribeRecursiveScheduleFromOutside() 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() {
int i;
@Override
public void run() {
System.out.println("Run: " + i++);
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);
}
});
}
});
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();
}
}
use of io.reactivex.rxjava3.core.Scheduler.Worker in project RxJava by ReactiveX.
the class Scheduler method schedulePeriodicallyDirect.
/**
* Schedules a periodic execution of the given task with the given initial time delay and repeat period.
*
* <p>
* This method is safe to be called from multiple threads but there are no
* ordering guarantees between tasks.
*
* <p>
* The periodic execution is at a fixed rate, that is, the first execution will be after the
* {@code initialDelay}, the second after {@code initialDelay + period}, the third after
* {@code initialDelay + 2 * period}, and so on.
*
* @param run the task to schedule
* @param initialDelay the initial delay amount, non-positive values indicate non-delayed scheduling
* @param period the period at which the task should be re-executed
* @param unit the unit of measure of the delay amount
* @return the Disposable that let's one cancel this particular delayed task.
* @throws NullPointerException if {@code run} or {@code unit} is {@code null}
* @since 2.0
*/
@NonNull
public Disposable schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) {
final Worker w = createWorker();
final Runnable decoratedRun = RxJavaPlugins.onSchedule(run);
PeriodicDirectTask periodicTask = new PeriodicDirectTask(decoratedRun, w);
Disposable d = w.schedulePeriodically(periodicTask, initialDelay, period, unit);
if (d == EmptyDisposable.INSTANCE) {
return d;
}
return periodicTask;
}
Aggregations