Search in sources :

Example 1 with GroupedFlowable

use of io.reactivex.flowables.GroupedFlowable 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 2 with GroupedFlowable

use of io.reactivex.flowables.GroupedFlowable 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 3 with GroupedFlowable

use of io.reactivex.flowables.GroupedFlowable 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)

Aggregations

GroupedFlowable (io.reactivex.flowables.GroupedFlowable)3 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)3 Test (org.junit.Test)3