use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.
the class ExecutorSchedulerInterruptibleTest method interruptibleWorkerTaskScheduledExecutor.
@Test
public void interruptibleWorkerTaskScheduledExecutor() throws Exception {
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
try {
Scheduler scheduler = Schedulers.from(exec, true);
Worker worker = scheduler.createWorker();
try {
final AtomicInteger sync = new AtomicInteger(2);
final AtomicBoolean isInterrupted = new AtomicBoolean();
Disposable d = worker.schedule(new Runnable() {
@Override
public void run() {
if (sync.decrementAndGet() != 0) {
while (sync.get() != 0) {
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
isInterrupted.set(true);
}
}
});
if (sync.decrementAndGet() != 0) {
while (sync.get() != 0) {
}
}
Thread.sleep(500);
d.dispose();
int i = 20;
while (i-- > 0 && !isInterrupted.get()) {
Thread.sleep(50);
}
assertTrue("Interruption did not propagate", isInterrupted.get());
} finally {
worker.dispose();
}
} finally {
exec.shutdown();
}
}
use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.
the class ExecutorSchedulerInterruptibleTest method runnableDisposedAsync.
@Test
public void runnableDisposedAsync() throws Exception {
final Scheduler s = Schedulers.from(new Executor() {
@Override
public void execute(Runnable r) {
new Thread(r).start();
}
}, true);
Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE);
while (!d.isDisposed()) {
Thread.sleep(1);
}
}
use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.
the class RxJavaPluginsTest method clearIsPassthrough.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void clearIsPassthrough() {
try {
RxJavaPlugins.reset();
assertNull(RxJavaPlugins.onAssembly((Observable) null));
assertNull(RxJavaPlugins.onAssembly((ConnectableObservable) null));
assertNull(RxJavaPlugins.onAssembly((Flowable) null));
assertNull(RxJavaPlugins.onAssembly((ConnectableFlowable) null));
Observable oos = new Observable() {
@Override
public void subscribeActual(Observer t) {
}
};
Flowable fos = new Flowable() {
@Override
public void subscribeActual(Subscriber t) {
}
};
assertSame(oos, RxJavaPlugins.onAssembly(oos));
assertSame(fos, RxJavaPlugins.onAssembly(fos));
assertNull(RxJavaPlugins.onAssembly((Single) null));
Single sos = new Single() {
@Override
public void subscribeActual(SingleObserver t) {
}
};
assertSame(sos, RxJavaPlugins.onAssembly(sos));
assertNull(RxJavaPlugins.onAssembly((Completable) null));
Completable cos = new Completable() {
@Override
public void subscribeActual(CompletableObserver t) {
}
};
assertSame(cos, RxJavaPlugins.onAssembly(cos));
assertNull(RxJavaPlugins.onAssembly((Maybe) null));
Maybe myb = new Maybe() {
@Override
public void subscribeActual(MaybeObserver t) {
}
};
assertSame(myb, RxJavaPlugins.onAssembly(myb));
Runnable action = Functions.EMPTY_RUNNABLE;
assertSame(action, RxJavaPlugins.onSchedule(action));
class AllSubscriber implements Subscriber, Observer, SingleObserver, CompletableObserver, MaybeObserver {
@Override
public void onSuccess(Object value) {
}
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(Object t) {
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
}
AllSubscriber all = new AllSubscriber();
Subscriber[] allArray = { all };
assertNull(RxJavaPlugins.onSubscribe(Observable.never(), null));
assertSame(all, RxJavaPlugins.onSubscribe(Observable.never(), all));
assertNull(RxJavaPlugins.onSubscribe(Flowable.never(), null));
assertSame(all, RxJavaPlugins.onSubscribe(Flowable.never(), all));
assertNull(RxJavaPlugins.onSubscribe(Single.just(1), null));
assertSame(all, RxJavaPlugins.onSubscribe(Single.just(1), all));
assertNull(RxJavaPlugins.onSubscribe(Completable.never(), null));
assertSame(all, RxJavaPlugins.onSubscribe(Completable.never(), all));
assertNull(RxJavaPlugins.onSubscribe(Maybe.never(), null));
assertSame(all, RxJavaPlugins.onSubscribe(Maybe.never(), all));
assertNull(RxJavaPlugins.onSubscribe(Flowable.never().parallel(), null));
assertSame(allArray, RxJavaPlugins.onSubscribe(Flowable.never().parallel(), allArray));
final Scheduler s = ImmediateThinScheduler.INSTANCE;
Supplier<Scheduler> c = new Supplier<Scheduler>() {
@Override
public Scheduler get() throws Exception {
return s;
}
};
assertSame(s, RxJavaPlugins.onComputationScheduler(s));
assertSame(s, RxJavaPlugins.onIoScheduler(s));
assertSame(s, RxJavaPlugins.onNewThreadScheduler(s));
assertSame(s, RxJavaPlugins.onSingleScheduler(s));
assertSame(s, RxJavaPlugins.initComputationScheduler(c));
assertSame(s, RxJavaPlugins.initIoScheduler(c));
assertSame(s, RxJavaPlugins.initNewThreadScheduler(c));
assertSame(s, RxJavaPlugins.initSingleScheduler(c));
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.
the class ObservableDebounceTest method debounceWithTimeBackpressure.
@Test
public void debounceWithTimeBackpressure() throws InterruptedException {
TestScheduler scheduler = new TestScheduler();
TestObserverEx<Integer> observer = new TestObserverEx<>();
Observable.merge(Observable.just(1), Observable.just(2).delay(10, TimeUnit.MILLISECONDS, scheduler)).debounce(20, TimeUnit.MILLISECONDS, scheduler).take(1).subscribe(observer);
scheduler.advanceTimeBy(30, TimeUnit.MILLISECONDS);
observer.assertValue(2);
observer.assertTerminated();
observer.assertNoErrors();
}
use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.
the class ObservableConcatMapSchedulerTest method issue2890NoStackoverflow.
@Test
public void issue2890NoStackoverflow() throws InterruptedException, TimeoutException {
final ExecutorService executor = Executors.newFixedThreadPool(2);
final Scheduler sch = Schedulers.from(executor);
Function<Integer, Observable<Integer>> func = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t) {
Observable<Integer> flowable = Observable.just(t).subscribeOn(sch);
Subject<Integer> processor = UnicastSubject.create();
flowable.subscribe(processor);
return processor;
}
};
int n = 5000;
final AtomicInteger counter = new AtomicInteger();
Observable.range(1, n).concatMap(func, 2, ImmediateThinScheduler.INSTANCE).subscribe(new DefaultObserver<Integer>() {
@Override
public void onNext(Integer t) {
// Consume after sleep for 1 ms
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// ignored
}
if (counter.getAndIncrement() % 100 == 0) {
System.out.print("testIssue2890NoStackoverflow -> ");
System.out.println(counter.get());
}
;
}
@Override
public void onComplete() {
executor.shutdown();
}
@Override
public void onError(Throwable e) {
executor.shutdown();
}
});
long awaitTerminationTimeout = 100_000;
if (!executor.awaitTermination(awaitTerminationTimeout, TimeUnit.MILLISECONDS)) {
throw new TimeoutException("Completed " + counter.get() + "/" + n + " before timed out after " + awaitTerminationTimeout + " milliseconds.");
}
assertEquals(n, counter.get());
}
Aggregations