use of io.reactivex.rxjava3.functions.Function 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.functions.Function 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.functions.Function 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.functions.Function in project RxJava by ReactiveX.
the class ObservableFlatMapTest method flatMapTransformsMaxConcurrentNormal.
@Test
public void flatMapTransformsMaxConcurrentNormal() {
final int m = 2;
final AtomicInteger subscriptionCount = new AtomicInteger();
Observable<Integer> onNext = composer(Observable.fromIterable(Arrays.asList(1, 2, 3)).observeOn(Schedulers.computation()), subscriptionCount, m).subscribeOn(Schedulers.computation());
Observable<Integer> onComplete = composer(Observable.fromIterable(Arrays.asList(4)), subscriptionCount, m).subscribeOn(Schedulers.computation());
Observable<Integer> onError = Observable.fromIterable(Arrays.asList(5));
Observable<Integer> source = Observable.fromIterable(Arrays.asList(10, 20, 30));
Observer<Object> o = TestHelper.mockObserver();
TestObserverEx<Object> to = new TestObserverEx<>(o);
Function<Throwable, Observable<Integer>> just = just(onError);
source.flatMap(just(onNext), just, just0(onComplete), m).subscribe(to);
to.awaitDone(1, TimeUnit.SECONDS);
to.assertNoErrors();
to.assertTerminated();
verify(o, times(3)).onNext(1);
verify(o, times(3)).onNext(2);
verify(o, times(3)).onNext(3);
verify(o).onNext(4);
verify(o).onComplete();
verify(o, never()).onNext(5);
verify(o, never()).onError(any(Throwable.class));
}
use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.
the class ObservableFlatMapTest method flatMapSelectorMaxConcurrent.
@Test
public void flatMapSelectorMaxConcurrent() {
final int m = 4;
final AtomicInteger subscriptionCount = new AtomicInteger();
Observable<Integer> source = Observable.range(1, 10).flatMap(new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return composer(Observable.range(t1 * 10, 2), subscriptionCount, m).subscribeOn(Schedulers.computation());
}
}, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 * 1000 + t2;
}
}, m);
TestObserver<Integer> to = new TestObserver<>();
source.subscribe(to);
to.awaitDone(5, TimeUnit.SECONDS);
to.assertNoErrors();
Set<Integer> expected = new HashSet<>(Arrays.asList(1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, 6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101));
Assert.assertEquals(expected.size(), to.values().size());
System.out.println("--> testFlatMapSelectorMaxConcurrent: " + to.values());
Assert.assertTrue(expected.containsAll(to.values()));
}
Aggregations