Search in sources :

Example 21 with BooleanSubscription

use of io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowableRetryTest method singleSubscriptionOnFirst.

@Test
public void singleSubscriptionOnFirst() throws Exception {
    final AtomicInteger inc = new AtomicInteger(0);
    Publisher<Integer> onSubscribe = new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            final int emit = inc.incrementAndGet();
            subscriber.onNext(emit);
            subscriber.onComplete();
        }
    };
    int first = Flowable.unsafeCreate(onSubscribe).retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {

        @Override
        public Flowable<Object> apply(Flowable<? extends Throwable> attempt) {
            return attempt.zipWith(Flowable.just(1), new BiFunction<Throwable, Integer, Object>() {

                @Override
                public Object apply(Throwable o, Integer integer) {
                    return 0;
                }
            });
        }
    }).blockingFirst();
    assertEquals("Observer did not receive the expected output", 1, first);
    assertEquals("Subscribe was not called once", 1, inc.get());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.rxjava3.flowables.GroupedFlowable) Test(org.junit.Test)

Example 22 with BooleanSubscription

use of io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowablePublishMulticastTest method dontDropItemsWhenNoReadyConsumers.

@Test
public void dontDropItemsWhenNoReadyConsumers() {
    final MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true);
    mp.onSubscribe(new BooleanSubscription());
    mp.onNext(1);
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    final MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(ts, mp);
    ts.onSubscribe(ms1);
    assertTrue(mp.add(ms1));
    ms1.set(Long.MIN_VALUE);
    mp.drain();
    assertFalse(mp.queue.isEmpty());
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 23 with BooleanSubscription

use of io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowablePublishMulticastTest method cancelledWhileFindingRequests.

@Test
public void cancelledWhileFindingRequests() {
    final MulticastProcessor<Integer> mp = new MulticastProcessor<>(128, true);
    final MulticastSubscription<Integer> ms1 = new MulticastSubscription<>(null, mp);
    assertTrue(mp.add(ms1));
    mp.onSubscribe(new BooleanSubscription());
    ms1.set(Long.MIN_VALUE);
    mp.drain();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 24 with BooleanSubscription

use of io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription 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)

Example 25 with BooleanSubscription

use of io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowableMapTest method sourceIgnoresCancel.

@Test
public void sourceIgnoresCancel() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.fromPublisher(new Publisher<Integer>() {

            @Override
            public void subscribe(Subscriber<? super Integer> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onNext(1);
                s.onNext(2);
                s.onError(new IOException());
                s.onComplete();
            }
        }).map(new Function<Integer, Object>() {

            @Override
            public Object apply(Integer v) throws Exception {
                throw new TestException();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) IOException(java.io.IOException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException)

Aggregations

BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)227 Test (org.junit.Test)152 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)81 TestException (io.reactivex.rxjava3.exceptions.TestException)40 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)32 IOException (java.io.IOException)26 InOrder (org.mockito.InOrder)19 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)12 Subscriber (org.reactivestreams.Subscriber)11 io.reactivex.rxjava3.testsupport (io.reactivex.rxjava3.testsupport)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 Assert (org.junit.Assert)10 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 io.reactivex.rxjava3.functions (io.reactivex.rxjava3.functions)8 Functions (io.reactivex.rxjava3.internal.functions.Functions)8 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)8 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)8 io.reactivex.rxjava3.processors (io.reactivex.rxjava3.processors)8 java.util (java.util)8