Search in sources :

Example 91 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class FlowableCollectWithCollectorTest method collectorAccumulatorDropSignalsToFlowable.

@Test
public void collectorAccumulatorDropSignalsToFlowable() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        Flowable<Integer> source = new Flowable<Integer>() {

            @Override
            protected void subscribeActual(Subscriber<? super Integer> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onNext(1);
                s.onNext(2);
                s.onError(new IOException());
                s.onComplete();
            }
        };
        source.collect(new Collector<Integer, Integer, Integer>() {

            @Override
            public Supplier<Integer> supplier() {
                return () -> 1;
            }

            @Override
            public BiConsumer<Integer, Integer> accumulator() {
                return (a, b) -> {
                    throw new TestException();
                };
            }

            @Override
            public BinaryOperator<Integer> combiner() {
                return (a, b) -> a + b;
            }

            @Override
            public Function<Integer, Integer> finisher() {
                return a -> a;
            }

            @Override
            public Set<Characteristics> characteristics() {
                return Collections.emptySet();
            }
        }).toFlowable().test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    });
}
Also used : java.util(java.util) TestHelper(io.reactivex.rxjava3.testsupport.TestHelper) TestException(io.reactivex.rxjava3.exceptions.TestException) Assert.assertFalse(org.junit.Assert.assertFalse) java.util.stream(java.util.stream) IOException(java.io.IOException) Test(org.junit.Test) java.util.function(java.util.function) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Subscriber(org.reactivestreams.Subscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) io.reactivex.rxjava3.processors(io.reactivex.rxjava3.processors) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) Subscriber(org.reactivestreams.Subscriber) IOException(java.io.IOException) Test(org.junit.Test)

Example 92 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class FlowableCollectWithCollectorTest method collectorAccumulatorCrashToFlowable.

@Test
public void collectorAccumulatorCrashToFlowable() {
    BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1);
    source.collect(new Collector<Integer, Integer, Integer>() {

        @Override
        public Supplier<Integer> supplier() {
            return () -> 1;
        }

        @Override
        public BiConsumer<Integer, Integer> accumulator() {
            return (a, b) -> {
                throw new TestException();
            };
        }

        @Override
        public BinaryOperator<Integer> combiner() {
            return (a, b) -> a + b;
        }

        @Override
        public Function<Integer, Integer> finisher() {
            return a -> a;
        }

        @Override
        public Set<Characteristics> characteristics() {
            return Collections.emptySet();
        }
    }).toFlowable().test().assertFailure(TestException.class);
    assertFalse(source.hasSubscribers());
}
Also used : java.util(java.util) TestHelper(io.reactivex.rxjava3.testsupport.TestHelper) TestException(io.reactivex.rxjava3.exceptions.TestException) Assert.assertFalse(org.junit.Assert.assertFalse) java.util.stream(java.util.stream) IOException(java.io.IOException) Test(org.junit.Test) java.util.function(java.util.function) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Subscriber(org.reactivestreams.Subscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) io.reactivex.rxjava3.processors(io.reactivex.rxjava3.processors) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 93 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class FlowableTimeoutWithSelectorTest method timeoutSelectorWithTimeoutAndOnNextRaceCondition.

@Test
public void timeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
    // Thread 1                                    Thread 2
    // 
    // observer.onNext(1)
    // start timeout
    // unsubscribe timeout in thread 2          start to do some long-time work in "unsubscribe"
    // observer.onNext(2)
    // timeout.onNext(1)
    // "unsubscribe" done
    // 
    // 
    // In the above case, the timeout operator should ignore "timeout.onNext(1)"
    // since "observer" has already seen 2.
    final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
    final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
    final CountDownLatch observerCompleted = new CountDownLatch(1);
    final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
    final AtomicBoolean latchTimeout = new AtomicBoolean(false);
    final Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            if (t1 == 1) {
                // Force "unsubscribe" run on another thread
                return Flowable.unsafeCreate(new Publisher<Integer>() {

                    @Override
                    public void subscribe(Subscriber<? super Integer> subscriber) {
                        subscriber.onSubscribe(new BooleanSubscription());
                        enteredTimeoutOne.countDown();
                        // force the timeout message be sent after observer.onNext(2)
                        while (true) {
                            try {
                                if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
                                    // CountDownLatch timeout
                                    // There should be something wrong
                                    latchTimeout.set(true);
                                }
                                break;
                            } catch (InterruptedException e) {
                            // Since we just want to emulate a busy method,
                            // we ignore the interrupt signal from Scheduler.
                            }
                        }
                        subscriber.onNext(1);
                        timeoutEmittedOne.countDown();
                    }
                }).subscribeOn(Schedulers.newThread());
            } else {
                return PublishProcessor.create();
            }
        }
    };
    final Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerReceivedTwo.countDown();
            return null;
        }
    }).when(subscriber).onNext(2);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerCompleted.countDown();
            return null;
        }
    }).when(subscriber).onComplete();
    final TestSubscriber<Integer> ts = new TestSubscriber<>(subscriber);
    new Thread(new Runnable() {

        @Override
        public void run() {
            PublishProcessor<Integer> source = PublishProcessor.create();
            source.timeout(timeoutFunc, Flowable.just(3)).subscribe(ts);
            // start timeout
            source.onNext(1);
            try {
                if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // disable timeout
            source.onNext(2);
            try {
                if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            source.onComplete();
        }
    }).start();
    if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
        latchTimeout.set(true);
    }
    assertFalse("CoundDownLatch timeout", latchTimeout.get());
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber).onSubscribe((Subscription) notNull());
    inOrder.verify(subscriber).onNext(1);
    inOrder.verify(subscriber).onNext(2);
    inOrder.verify(subscriber, never()).onNext(3);
    inOrder.verify(subscriber).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 94 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class FlowableRepeatTest method shouldDisposeInnerObservable.

@Test
public void shouldDisposeInnerObservable() {
    final PublishProcessor<Object> processor = PublishProcessor.create();
    final Disposable disposable = Flowable.just("Leak").repeatWhen(new Function<Flowable<Object>, Flowable<Object>>() {

        @Override
        public Flowable<Object> apply(Flowable<Object> completions) throws Exception {
            return completions.switchMap(new Function<Object, Flowable<Object>>() {

                @Override
                public Flowable<Object> apply(Object ignore) throws Exception {
                    return processor;
                }
            });
        }
    }).subscribe();
    assertTrue(processor.hasSubscribers());
    disposable.dispose();
    assertFalse(processor.hasSubscribers());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) Flowable(io.reactivex.rxjava3.core.Flowable) Test(org.junit.Test)

Example 95 with Function

use of io.reactivex.rxjava3.functions.Function in project RxJava by ReactiveX.

the class SingleTimerTest method timerInterruptible.

@Test
public void timerInterruptible() throws Exception {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    try {
        for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
            final AtomicBoolean interrupted = new AtomicBoolean();
            TestObserver<Long> to = Single.timer(1, TimeUnit.MILLISECONDS, s).map(new Function<Long, Long>() {

                @Override
                public Long apply(Long v) throws Exception {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                        interrupted.set(true);
                    }
                    return v;
                }
            }).test();
            Thread.sleep(500);
            to.dispose();
            Thread.sleep(500);
            assertTrue(s.getClass().getSimpleName(), interrupted.get());
        }
    } finally {
        exec.shutdown();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.rxjava3.functions.Function) Test(org.junit.Test)

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