Search in sources :

Example 16 with Function

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());
    }
}
Also used : TrampolineScheduler(io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler) Test(org.junit.Test)

Example 17 with Function

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();
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 18 with Function

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());
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Observable(io.reactivex.rxjava3.core.Observable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 19 with Function

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.rxjava3.core.Observable)

Example 20 with Function

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()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.rxjava3.core.Observable) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Aggregations

Test (org.junit.Test)97 TestException (io.reactivex.rxjava3.exceptions.TestException)78 Observable (io.reactivex.rxjava3.core.Observable)41 InOrder (org.mockito.InOrder)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Function (io.reactivex.rxjava3.functions.Function)20 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 IOException (java.io.IOException)13 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)9 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)7 Observer (io.reactivex.rxjava3.core.Observer)7 Function (org.apache.cassandra.cql3.functions.Function)7 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)6 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)6 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)5