Search in sources :

Example 21 with BooleanSubscription

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

the class FlowableGroupByTest method testFirstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenObservesOnAndDelaysAndThenCompletes.

@Test
public void testFirstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenObservesOnAndDelaysAndThenCompletes() throws InterruptedException {
    // there are two groups to first complete
    final CountDownLatch first = new CountDownLatch(2);
    final ArrayList<String> results = new ArrayList<String>();
    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.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).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.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.flowables.GroupedFlowable) Test(org.junit.Test)

Example 22 with BooleanSubscription

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

the class FlowableGroupByTest method testGroupsWithNestedObserveOn.

@Test
public void testGroupsWithNestedObserveOn() throws InterruptedException {
    final ArrayList<String> results = new ArrayList<String>();
    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);
            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) {
            return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() {

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

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

Example 23 with BooleanSubscription

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

the class FlowableGroupByTest method testFirstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete.

@Test
public void testFirstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException {
    // there are two groups to first complete
    final CountDownLatch first = new CountDownLatch(2);
    final ArrayList<String> results = new ArrayList<String>();
    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.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.flowables.GroupedFlowable) Test(org.junit.Test)

Example 24 with BooleanSubscription

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

the class FlowableObserveOnTest method asycFusedPollThrows.

@Test
public void asycFusedPollThrows() {
    new Flowable<Integer>() {

        @Override
        protected void subscribeActual(Subscriber<? super Integer> observer) {
            observer.onSubscribe(new BooleanSubscription());
            @SuppressWarnings("unchecked") BaseObserveOnSubscriber<Integer> oo = (BaseObserveOnSubscriber<Integer>) observer;
            oo.sourceMode = QueueFuseable.ASYNC;
            oo.requested.lazySet(1);
            oo.queue = new SimpleQueue<Integer>() {

                @Override
                public boolean offer(Integer value) {
                    return false;
                }

                @Override
                public boolean offer(Integer v1, Integer v2) {
                    return false;
                }

                @Nullable
                @Override
                public Integer poll() throws Exception {
                    throw new TestException();
                }

                @Override
                public boolean isEmpty() {
                    return false;
                }

                @Override
                public void clear() {
                }
            };
            oo.clear();
            oo.trySchedule();
        }
    }.observeOn(Schedulers.single()).test(0L).awaitDone(5, TimeUnit.SECONDS).assertFailure(TestException.class);
}
Also used : BaseObserveOnSubscriber(io.reactivex.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) BaseObserveOnSubscriber(io.reactivex.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber) Nullable(io.reactivex.annotations.Nullable) Test(org.junit.Test)

Example 25 with BooleanSubscription

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

the class FlowableObserveOnTest method nonFusedPollThrows.

@Test
public void nonFusedPollThrows() {
    new Flowable<Integer>() {

        @Override
        protected void subscribeActual(Subscriber<? super Integer> observer) {
            observer.onSubscribe(new BooleanSubscription());
            @SuppressWarnings("unchecked") BaseObserveOnSubscriber<Integer> oo = (BaseObserveOnSubscriber<Integer>) observer;
            oo.sourceMode = QueueFuseable.SYNC;
            oo.requested.lazySet(1);
            oo.queue = new SimpleQueue<Integer>() {

                @Override
                public boolean offer(Integer value) {
                    return false;
                }

                @Override
                public boolean offer(Integer v1, Integer v2) {
                    return false;
                }

                @Nullable
                @Override
                public Integer poll() throws Exception {
                    throw new TestException();
                }

                @Override
                public boolean isEmpty() {
                    return false;
                }

                @Override
                public void clear() {
                }
            };
            oo.clear();
            oo.trySchedule();
        }
    }.observeOn(Schedulers.single()).test(0L).awaitDone(5, TimeUnit.SECONDS).assertFailure(TestException.class);
}
Also used : BaseObserveOnSubscriber(io.reactivex.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) BaseObserveOnSubscriber(io.reactivex.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber) Nullable(io.reactivex.annotations.Nullable) Test(org.junit.Test)

Aggregations

BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)131 Test (org.junit.Test)70 TestSubscriber (io.reactivex.subscribers.TestSubscriber)31 TestException (io.reactivex.exceptions.TestException)24 InOrder (org.mockito.InOrder)21 IOException (java.io.IOException)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BooleanSupplier (io.reactivex.functions.BooleanSupplier)5 ForEachWhileSubscriber (io.reactivex.internal.subscribers.ForEachWhileSubscriber)5 ArrayDeque (java.util.ArrayDeque)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 Disposable (io.reactivex.disposables.Disposable)4 GroupedFlowable (io.reactivex.flowables.GroupedFlowable)4 Subscriber (org.reactivestreams.Subscriber)4 BaseObserveOnSubscriber (io.reactivex.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber)3 Worker (io.reactivex.Scheduler.Worker)2 Nullable (io.reactivex.annotations.Nullable)2 ConnectableFlowable (io.reactivex.flowables.ConnectableFlowable)2 SubscribeOnSubscriber (io.reactivex.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber)2 FutureSubscriber (io.reactivex.internal.subscribers.FutureSubscriber)2