Search in sources :

Example 66 with Action

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

the class SingleDoOnTerminateTest method doOnTerminateSuccess.

@Test
public void doOnTerminateSuccess() {
    final AtomicBoolean atomicBoolean = new AtomicBoolean();
    Single.just(1).doOnTerminate(new Action() {

        @Override
        public void run() throws Exception {
            atomicBoolean.set(true);
        }
    }).test().assertResult(1);
    assertTrue(atomicBoolean.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action(io.reactivex.rxjava3.functions.Action) Test(org.junit.Test)

Example 67 with Action

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

the class SingleFlatMapTest method flatMapPublisherCancelDuringSingle.

@Test
public void flatMapPublisherCancelDuringSingle() {
    final AtomicBoolean disposed = new AtomicBoolean();
    TestSubscriberEx<Integer> ts = Single.<Integer>never().doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            disposed.set(true);
        }
    }).flatMapPublisher(new Function<Integer, Publisher<Integer>>() {

        @Override
        public Publisher<Integer> apply(Integer v) throws Exception {
            return Flowable.range(v, 5);
        }
    }).to(TestHelper.<Integer>testConsumer()).assertNoValues().assertNotTerminated();
    assertFalse(disposed.get());
    ts.cancel();
    assertTrue(disposed.get());
    ts.assertNotTerminated();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 68 with Action

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

the class SingleFlatMapTest method flatMapPublisherCancelDuringFlowable.

@Test
public void flatMapPublisherCancelDuringFlowable() {
    final AtomicBoolean disposed = new AtomicBoolean();
    TestSubscriberEx<Integer> ts = Single.just(1).flatMapPublisher(new Function<Integer, Publisher<Integer>>() {

        @Override
        public Publisher<Integer> apply(Integer v) throws Exception {
            return Flowable.<Integer>never().doOnCancel(new Action() {

                @Override
                public void run() throws Exception {
                    disposed.set(true);
                }
            });
        }
    }).to(TestHelper.<Integer>testConsumer()).assertNoValues().assertNotTerminated();
    assertFalse(disposed.get());
    ts.cancel();
    assertTrue(disposed.get());
    ts.assertNotTerminated();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 69 with Action

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

the class BoundedSubscriberTest method badSourceOnSubscribe.

@Test
public void badSourceOnSubscribe() {
    Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> s) {
            BooleanSubscription s1 = new BooleanSubscription();
            s.onSubscribe(s1);
            BooleanSubscription s2 = new BooleanSubscription();
            s.onSubscribe(s2);
            assertFalse(s1.isCancelled());
            assertTrue(s2.isCancelled());
            s.onNext(1);
            s.onComplete();
        }
    });
    final List<Object> received = new ArrayList<>();
    BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {

        @Override
        public void accept(Object v) throws Exception {
            received.add(v);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            received.add(e);
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            received.add(100);
        }
    }, new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) throws Exception {
            s.request(128);
        }
    }, 128);
    source.subscribe(subscriber);
    assertEquals(Arrays.asList(1, 100), received);
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 70 with Action

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

the class LambdaSubscriberTest method badSourceOnSubscribe.

@Test
public void badSourceOnSubscribe() {
    Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> s) {
            BooleanSubscription s1 = new BooleanSubscription();
            s.onSubscribe(s1);
            BooleanSubscription s2 = new BooleanSubscription();
            s.onSubscribe(s2);
            assertFalse(s1.isCancelled());
            assertTrue(s2.isCancelled());
            s.onNext(1);
            s.onComplete();
        }
    });
    final List<Object> received = new ArrayList<>();
    LambdaSubscriber<Object> subscriber = new LambdaSubscriber<>(new Consumer<Object>() {

        @Override
        public void accept(Object v) throws Exception {
            received.add(v);
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            received.add(e);
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            received.add(100);
        }
    }, new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) throws Exception {
            s.request(Long.MAX_VALUE);
        }
    });
    source.subscribe(subscriber);
    assertEquals(Arrays.asList(1, 100), received);
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)109 TestException (io.reactivex.rxjava3.exceptions.TestException)64 Action (io.reactivex.rxjava3.functions.Action)49 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)13 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 TestObserver (io.reactivex.rxjava3.observers.TestObserver)10 Disposable (io.reactivex.rxjava3.disposables.Disposable)9 IOException (java.io.IOException)9 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 Observable (io.reactivex.rxjava3.core.Observable)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)3 Flowable (io.reactivex.rxjava3.core.Flowable)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3