Search in sources :

Example 21 with Consumer

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

the class ObservableCacheTest method cache.

@Test
public void cache() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    Observable<String> o = Observable.unsafeCreate(new ObservableSource<String>() {

        @Override
        public void subscribe(final Observer<? super String> observer) {
            observer.onSubscribe(Disposable.empty());
            new Thread(new Runnable() {

                @Override
                public void run() {
                    counter.incrementAndGet();
                    System.out.println("published Observable being executed");
                    observer.onNext("one");
                    observer.onComplete();
                }
            }).start();
        }
    }).cache();
    // we then expect the following 2 subscriptions to get that same value
    final CountDownLatch latch = new CountDownLatch(2);
    // subscribe once
    o.subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            assertEquals("one", v);
            System.out.println("v: " + v);
            latch.countDown();
        }
    });
    // subscribe again
    o.subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            assertEquals("one", v);
            System.out.println("v: " + v);
            latch.countDown();
        }
    });
    if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
        fail("subscriptions did not receive values");
    }
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observer(io.reactivex.rxjava3.core.Observer) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 22 with Consumer

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

the class ObservableAmbTest method noWinnerErrorDispose.

@Test
public void noWinnerErrorDispose() throws Exception {
    final TestException ex = new TestException();
    for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
        final AtomicBoolean interrupted = new AtomicBoolean();
        final CountDownLatch cdl = new CountDownLatch(1);
        Observable.ambArray(Observable.error(ex).subscribeOn(Schedulers.single()).observeOn(Schedulers.computation()), Observable.never()).subscribe(Functions.emptyConsumer(), new Consumer<Throwable>() {

            @Override
            public void accept(Throwable e) throws Exception {
                interrupted.set(Thread.currentThread().isInterrupted());
                cdl.countDown();
            }
        });
        assertTrue(cdl.await(500, TimeUnit.SECONDS));
        assertFalse("Interrupted!", interrupted.get());
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException)

Example 23 with Consumer

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

the class FlowableReplayEagerTruncateTest method issue2191_SchedulerUnsubscribeOnError.

/**
 * Specifically test interaction with a Scheduler with subscribeOn.
 *
 * @throws Throwable functional interfaces declare throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void issue2191_SchedulerUnsubscribeOnError() throws Throwable {
    // setup mocks
    Consumer<Integer> sourceNext = mock(Consumer.class);
    Action sourceCompleted = mock(Action.class);
    Consumer<Throwable> sourceError = mock(Consumer.class);
    Action sourceUnsubscribed = mock(Action.class);
    final Scheduler mockScheduler = mock(Scheduler.class);
    final Disposable mockSubscription = mock(Disposable.class);
    Worker spiedWorker = workerSpy(mockSubscription);
    Subscriber<Integer> mockObserverBeforeConnect = TestHelper.mockSubscriber();
    Subscriber<Integer> mockObserverAfterConnect = TestHelper.mockSubscriber();
    when(mockScheduler.createWorker()).thenReturn(spiedWorker);
    // Flowable under test
    Function<Integer, Integer> mockFunc = mock(Function.class);
    IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
    when(mockFunc.apply(1)).thenReturn(1);
    when(mockFunc.apply(2)).thenThrow(illegalArgumentException);
    ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).map(mockFunc).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).doOnError(sourceError).subscribeOn(mockScheduler).replay();
    replay.subscribe(mockObserverBeforeConnect);
    replay.subscribe(mockObserverBeforeConnect);
    replay.connect();
    replay.subscribe(mockObserverAfterConnect);
    replay.subscribe(mockObserverAfterConnect);
    verify(mockObserverBeforeConnect, times(2)).onSubscribe((Subscription) any());
    verify(mockObserverAfterConnect, times(2)).onSubscribe((Subscription) any());
    // verify interactions
    verify(mockScheduler, times(1)).createWorker();
    verify(spiedWorker, times(1)).schedule((Runnable) notNull());
    verify(sourceNext, times(1)).accept(1);
    verify(sourceError, times(1)).accept(illegalArgumentException);
    verifyObserver(mockObserverBeforeConnect, 2, 2, illegalArgumentException);
    verifyObserver(mockObserverAfterConnect, 2, 2, illegalArgumentException);
    // FIXME publish also calls cancel
    verify(spiedWorker, times(1)).dispose();
    verify(sourceUnsubscribed, never()).run();
    verifyNoMoreInteractions(sourceNext);
    verifyNoMoreInteractions(sourceCompleted);
    verifyNoMoreInteractions(sourceError);
    verifyNoMoreInteractions(sourceUnsubscribed);
    verifyNoMoreInteractions(spiedWorker);
    verifyNoMoreInteractions(mockSubscription);
    verifyNoMoreInteractions(mockScheduler);
    verifyNoMoreInteractions(mockObserverBeforeConnect);
    verifyNoMoreInteractions(mockObserverAfterConnect);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker)

Example 24 with Consumer

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

the class FlowableRetryTest method unsubscribeFromRetry.

@Test
public void unsubscribeFromRetry() {
    PublishProcessor<Integer> processor = PublishProcessor.create();
    final AtomicInteger count = new AtomicInteger(0);
    Disposable sub = processor.retry().subscribe(new Consumer<Integer>() {

        @Override
        public void accept(Integer n) {
            count.incrementAndGet();
        }
    });
    processor.onNext(1);
    sub.dispose();
    processor.onNext(2);
    assertEquals(1, count.get());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 25 with Consumer

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

the class FlowableGroupByTest method firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete.

@Test
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException {
    // there are two groups to first complete
    final CountDownLatch first = new CountDownLatch(2);
    final ArrayList<String> results = new ArrayList<>();
    Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> sub) {
            sub.onSubscribe(new BooleanSubscription());
            sub.onNext(1);
            sub.onNext(2);
            sub.onNext(1);
            sub.onNext(2);
            try {
                first.await();
            } catch (InterruptedException e) {
                sub.onError(e);
                return;
            }
            sub.onNext(3);
            sub.onNext(3);
            sub.onComplete();
        }
    }).groupBy(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer t) {
            return t;
        }
    }).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() {

        @Override
        public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) {
            if (group.getKey() < 3) {
                return group.map(new Function<Integer, String>() {

                    @Override
                    public String apply(Integer t1) {
                        return "first groups: " + t1;
                    }
                }).take(2).doOnComplete(new Action() {

                    @Override
                    public void run() {
                        first.countDown();
                    }
                });
            } else {
                return group.map(new Function<Integer, String>() {

                    @Override
                    public String apply(Integer t1) {
                        return "last group: " + t1;
                    }
                });
            }
        }
    }).blockingForEach(new Consumer<String>() {

        @Override
        public void accept(String s) {
            results.add(s);
        }
    });
    System.out.println("Results: " + results);
    assertEquals(6, results.size());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)98 TestException (io.reactivex.rxjava3.exceptions.TestException)57 Disposable (io.reactivex.rxjava3.disposables.Disposable)39 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)22 IOException (java.io.IOException)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 InOrder (org.mockito.InOrder)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Observable (io.reactivex.rxjava3.core.Observable)5 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5 Consumer (io.reactivex.rxjava3.functions.Consumer)5 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Observer (io.reactivex.rxjava3.core.Observer)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ArgsToString (io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString)3