Search in sources :

Example 1 with Timed

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

Example 2 with Timed

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());
}
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 3 with Timed

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

Example 4 with Timed

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

Example 5 with Timed

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

Aggregations

Test (org.junit.Test)5 Observable (io.reactivex.rxjava3.core.Observable)2 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)2 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)2 NonNull (androidx.annotation.NonNull)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 AmplifyException (com.amplifyframework.AmplifyException)1 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)1 SubscriptionType (com.amplifyframework.api.graphql.SubscriptionType)1 Action (com.amplifyframework.core.Action)1 Amplify (com.amplifyframework.core.Amplify)1 Consumer (com.amplifyframework.core.Consumer)1 Cancelable (com.amplifyframework.core.async.Cancelable)1 Model (com.amplifyframework.core.model.Model)1 ModelProvider (com.amplifyframework.core.model.ModelProvider)1 ModelSchema (com.amplifyframework.core.model.ModelSchema)1 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)1 SerializedModel (com.amplifyframework.core.model.SerializedModel)1 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)1 AmplifyDisposables (com.amplifyframework.datastore.AmplifyDisposables)1