use of io.reactivex.rxjava3.schedulers.Timed in project RxJava by ReactiveX.
the class AbstractSchedulerTests method subscribeOnNestedConcurrency.
@Test
public final void subscribeOnNestedConcurrency() throws InterruptedException {
final Scheduler scheduler = getScheduler();
Flowable<String> f = Flowable.fromArray("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten").flatMap(new Function<String, Flowable<String>>() {
@Override
public Flowable<String> apply(final String v) {
return Flowable.unsafeCreate(new Publisher<String>() {
@Override
public void subscribe(Subscriber<? super String> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
subscriber.onNext("value_after_map-" + v);
subscriber.onComplete();
}
}).subscribeOn(scheduler);
}
});
ConcurrentObserverValidator<String> observer = new ConcurrentObserverValidator<>();
f.subscribe(observer);
if (!observer.completed.await(3000, TimeUnit.MILLISECONDS)) {
fail("timed out");
}
if (observer.error.get() != null) {
observer.error.get().printStackTrace();
fail("Error: " + observer.error.get().getMessage());
}
}
use of io.reactivex.rxjava3.schedulers.Timed 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());
}
use of io.reactivex.rxjava3.schedulers.Timed in project RxJava by ReactiveX.
the class AbstractSchedulerConcurrencyTests method recursionAndOuterUnsubscribe.
@Test
public void recursionAndOuterUnsubscribe() 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> subscriber) {
inner.schedule(new Runnable() {
@Override
public void run() {
subscriber.onNext(42);
latch.countDown();
// this will recursively schedule this task for execution again
inner.schedule(this);
}
});
subscriber.onSubscribe(new Subscription() {
@Override
public void cancel() {
inner.dispose();
subscriber.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.rxjava3.schedulers.Timed in project RxJava by ReactiveX.
the class FlowableConcatMapSchedulerTest method issue2890NoStackoverflow.
@Test
public void issue2890NoStackoverflow() throws InterruptedException, TimeoutException {
final ExecutorService executor = Executors.newFixedThreadPool(2);
final Scheduler sch = Schedulers.from(executor);
Function<Integer, Flowable<Integer>> func = new Function<Integer, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Integer t) {
Flowable<Integer> flowable = Flowable.just(t).subscribeOn(sch);
FlowableProcessor<Integer> processor = UnicastProcessor.create();
flowable.subscribe(processor);
return processor;
}
};
int n = 5000;
final AtomicInteger counter = new AtomicInteger();
Flowable.range(1, n).concatMap(func, 2, ImmediateThinScheduler.INSTANCE).subscribe(new DefaultSubscriber<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 awaitTerminationTimeoutMillis = 100_000;
if (!executor.awaitTermination(awaitTerminationTimeoutMillis, TimeUnit.MILLISECONDS)) {
throw new TimeoutException("Completed " + counter.get() + "/" + n + " before timed out after " + awaitTerminationTimeoutMillis + " milliseconds.");
}
assertEquals(n, counter.get());
}
use of io.reactivex.rxjava3.schedulers.Timed in project RxJava by ReactiveX.
the class AbstractSchedulerTests method observeOn.
@Test
public final void observeOn() throws InterruptedException {
final Scheduler scheduler = getScheduler();
Flowable<String> f = Flowable.fromArray("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
ConcurrentObserverValidator<String> observer = new ConcurrentObserverValidator<>();
f.observeOn(scheduler).subscribe(observer);
if (!observer.completed.await(3000, TimeUnit.MILLISECONDS)) {
fail("timed out");
}
if (observer.error.get() != null) {
observer.error.get().printStackTrace();
fail("Error: " + observer.error.get().getMessage());
}
}
Aggregations