Search in sources :

Example 21 with Observable

use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.

the class ObservableConcatMapSchedulerTest method concatArray.

@SuppressWarnings("unchecked")
@Test
public void concatArray() throws Exception {
    for (int i = 2; i < 10; i++) {
        Observable<Integer>[] obs = new Observable[i];
        Arrays.fill(obs, Observable.just(1));
        Integer[] expected = new Integer[i];
        Arrays.fill(expected, 1);
        Method m = Observable.class.getMethod("concatArray", ObservableSource[].class);
        TestObserver<Integer> to = TestObserver.create();
        ((Observable<Integer>) m.invoke(null, new Object[] { obs })).subscribe(to);
        to.assertValues(expected);
        to.assertNoErrors();
        to.assertComplete();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Method(java.lang.reflect.Method) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 22 with Observable

use of io.reactivex.rxjava3.core.Observable 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 23 with Observable

use of io.reactivex.rxjava3.core.Observable 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 24 with Observable

use of io.reactivex.rxjava3.core.Observable 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)

Example 25 with Observable

use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.

the class ObservableDebounceTest method debounceWithError.

@Test
public void debounceWithError() {
    Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() {

        @Override
        public void subscribe(Observer<? super String> observer) {
            observer.onSubscribe(Disposable.empty());
            Exception error = new TestException();
            // Should be published since "two" will arrive after the timeout expires.
            publishNext(observer, 100, "one");
            // Should be skipped since onError will arrive before the timeout expires.
            publishNext(observer, 600, "two");
            // Should be published as soon as the timeout expires.
            publishError(observer, 700, error);
        }
    });
    Observable<String> sampled = source.debounce(400, TimeUnit.MILLISECONDS, scheduler);
    sampled.subscribe(observer);
    scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    // 100 + 400 means it triggers at 500
    scheduler.advanceTimeTo(500, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onNext("one");
    scheduler.advanceTimeTo(701, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException)

Aggregations

Test (org.junit.Test)132 TestException (io.reactivex.rxjava3.exceptions.TestException)109 Observable (io.reactivex.rxjava3.core.Observable)83 InOrder (org.mockito.InOrder)60 TestObserver (io.reactivex.rxjava3.observers.TestObserver)38 Disposable (io.reactivex.rxjava3.disposables.Disposable)35 IOException (java.io.IOException)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)22 Observer (io.reactivex.rxjava3.core.Observer)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 Function (io.reactivex.rxjava3.functions.Function)10 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 Collections (java.util.Collections)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Assert (org.junit.Assert)6