Search in sources :

Example 21 with GroupedObservable

use of rx.observables.GroupedObservable in project mantis by Netflix.

the class OperatorGroupByTest method testIgnoringGroups.

@Test
public void testIgnoringGroups() {
    final AtomicInteger subscribeCounter = new AtomicInteger();
    final AtomicInteger sentEventCounter = new AtomicInteger();
    final AtomicInteger eventCounter = new AtomicInteger();
    SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter).groupBy(new Func1<Event, Integer>() {

        @Override
        public Integer call(Event e) {
            return e.source;
        }
    }).flatMap(new Func1<GroupedObservable<Integer, Event>, Observable<String>>() {

        @Override
        public Observable<String> call(GroupedObservable<Integer, Event> eventGroupedObservable) {
            Observable<Event> eventStream = eventGroupedObservable;
            if (eventGroupedObservable.getKey() >= 2) {
                // filter these
                eventStream = eventGroupedObservable.filter(new Func1<Event, Boolean>() {

                    @Override
                    public Boolean call(Event t1) {
                        return false;
                    }
                });
            }
            return eventStream.map(new Func1<Event, String>() {

                @Override
                public String call(Event event) {
                    return "testUnsubscribe => Source: " + event.source + "  Message: " + event.message;
                }
            });
        }
    }).take(30).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            eventCounter.incrementAndGet();
            System.out.println("=> " + s);
        }
    });
    assertEquals(30, eventCounter.get());
    // we should send 30 additional events that are filtered out as they are in the groups we skip
    assertEquals(60, sentEventCounter.get());
}
Also used : Observable(rx.Observable) GroupedObservable(rx.observables.GroupedObservable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Func1(rx.functions.Func1) GroupedObservable(rx.observables.GroupedObservable) Test(org.junit.Test)

Example 22 with GroupedObservable

use of rx.observables.GroupedObservable in project mantis by Netflix.

the class OperatorGroupByTest 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>();
    Observable.create(new OnSubscribe<Integer>() {

        @Override
        public void call(Subscriber<? super Integer> sub) {
            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.onCompleted();
        }
    }).groupBy(new Func1<Integer, Integer>() {

        @Override
        public Integer call(Integer t) {
            return t;
        }
    }).flatMap(new Func1<GroupedObservable<Integer, Integer>, Observable<String>>() {

        @Override
        public Observable<String> call(final GroupedObservable<Integer, Integer> group) {
            if (group.getKey() < 3) {
                return group.map(new Func1<Integer, String>() {

                    @Override
                    public String call(Integer t1) {
                        return "first groups: " + t1;
                    }
                }).take(2).doOnCompleted(new Action0() {

                    @Override
                    public void call() {
                        first.countDown();
                    }
                });
            } else {
                return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Func1<Integer, String>() {

                    @Override
                    public String call(Integer t1) {
                        return "last group: " + t1;
                    }
                });
            }
        }
    }).toBlocking().forEach(new Action1<String>() {

        @Override
        public void call(String s) {
            results.add(s);
        }
    });
    System.out.println("Results: " + results);
    assertEquals(6, results.size());
}
Also used : Action0(rx.functions.Action0) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(rx.Observable) GroupedObservable(rx.observables.GroupedObservable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Func1(rx.functions.Func1) GroupedObservable(rx.observables.GroupedObservable) Test(org.junit.Test)

Example 23 with GroupedObservable

use of rx.observables.GroupedObservable in project mantis by Netflix.

the class OperatorGroupByTest method testUnsubscribeViaTakeOnGroupThenMergeAndTake.

@Test
public void testUnsubscribeViaTakeOnGroupThenMergeAndTake() {
    final AtomicInteger subscribeCounter = new AtomicInteger();
    final AtomicInteger sentEventCounter = new AtomicInteger();
    final AtomicInteger eventCounter = new AtomicInteger();
    SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter).groupBy(new Func1<Event, Integer>() {

        @Override
        public Integer call(Event e) {
            return e.source;
        }
    }).take(2).flatMap(new Func1<GroupedObservable<Integer, Event>, Observable<String>>() {

        @Override
        public Observable<String> call(GroupedObservable<Integer, Event> eventGroupedObservable) {
            return eventGroupedObservable.map(new Func1<Event, String>() {

                @Override
                public String call(Event event) {
                    return "testUnsubscribe => Source: " + event.source + "  Message: " + event.message;
                }
            });
        }
    }).take(30).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            eventCounter.incrementAndGet();
            System.out.println("=> " + s);
        }
    });
    assertEquals(30, eventCounter.get());
    // we should send 28 additional events that are filtered out as they are in the groups we skip
    assertEquals(58, sentEventCounter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Func1(rx.functions.Func1) Observable(rx.Observable) GroupedObservable(rx.observables.GroupedObservable) GroupedObservable(rx.observables.GroupedObservable) Test(org.junit.Test)

Aggregations

GroupedObservable (rx.observables.GroupedObservable)23 Func1 (rx.functions.Func1)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 Test (org.junit.Test)13 Observable (rx.Observable)13 Action0 (rx.functions.Action0)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ArrayList (java.util.ArrayList)7 Action1 (rx.functions.Action1)6 List (java.util.List)4 Notification (rx.Notification)4 MantisGroup (io.mantisrx.common.MantisGroup)3 Metrics (io.mantisrx.common.metrics.Metrics)3 Endpoint (io.mantisrx.common.network.Endpoint)3 Context (io.mantisrx.runtime.Context)3 StageConfig (io.mantisrx.runtime.StageConfig)3 TimeUnit (java.util.concurrent.TimeUnit)3 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)2 Gauge (io.mantisrx.common.metrics.Gauge)2 ConnectToGroupedObservable (io.reactivex.mantis.remote.observable.ConnectToGroupedObservable)2