Search in sources :

Example 6 with GroupedObservable

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

the class OperatorGroupByTest method testCompletionIfInnerNotSubscribed.

@Test(timeout = 1000)
public void testCompletionIfInnerNotSubscribed() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger eventCounter = new AtomicInteger();
    Observable.range(0, 100).groupBy(new Func1<Integer, Integer>() {

        @Override
        public Integer call(Integer i) {
            return i % 2;
        }
    }).subscribe(new Subscriber<GroupedObservable<Integer, Integer>>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            latch.countDown();
        }

        @Override
        public void onNext(GroupedObservable<Integer, Integer> s) {
            eventCounter.incrementAndGet();
            System.out.println("=> " + s);
        }
    });
    if (!latch.await(500, TimeUnit.MILLISECONDS)) {
        fail("timed out - never got completion");
    }
    assertEquals(2, eventCounter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Func1(rx.functions.Func1) GroupedObservable(rx.observables.GroupedObservable) Test(org.junit.Test)

Example 7 with GroupedObservable

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

the class OperatorGroupByTest method testGroupedEventStream.

/**
 * Assert that only a single subscription to a stream occurs and that all events are received.
 *
 * @throws Throwable
 */
@Test
public void testGroupedEventStream() throws Throwable {
    final AtomicInteger eventCounter = new AtomicInteger();
    final AtomicInteger subscribeCounter = new AtomicInteger();
    final AtomicInteger groupCounter = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(1);
    final int count = 10000;
    final int groupCount = 2000;
    Observable<Event> es = Observable.create(new Observable.OnSubscribe<Event>() {

        @Override
        public void call(final Subscriber<? super Event> observer) {
            System.out.println("*** Subscribing to EventStream ***");
            subscribeCounter.incrementAndGet();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < count; i++) {
                        Event e = new Event();
                        e.source = i % groupCount;
                        e.message = "Event-" + i;
                        observer.onNext(e);
                    }
                    observer.onCompleted();
                }
            }).start();
        }
    });
    es.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) {
            System.out.println("GroupedObservable Key: " + eventGroupedObservable.getKey());
            groupCounter.incrementAndGet();
            return eventGroupedObservable.map(new Func1<Event, String>() {

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

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            latch.countDown();
        }

        @Override
        public void onNext(String outputMessage) {
            System.out.println(outputMessage);
            eventCounter.incrementAndGet();
        }
    });
    latch.await(5000, TimeUnit.MILLISECONDS);
    assertEquals(1, subscribeCounter.get());
    assertEquals(groupCount, groupCounter.get());
    assertEquals(count, eventCounter.get());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) 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 8 with GroupedObservable

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

the class OperatorGroupByTest method testgroupByBackpressure.

@Test
public void testgroupByBackpressure() throws InterruptedException {
    TestSubscriber<String> ts = new TestSubscriber<String>();
    Observable.range(1, 4000).groupBy(IS_EVEN2).flatMap(new Func1<GroupedObservable<Boolean, Integer>, Observable<String>>() {

        @Override
        public Observable<String> call(final GroupedObservable<Boolean, Integer> g) {
            return g.doOnCompleted(new Action0() {

                @Override
                public void call() {
                    System.out.println("//////////////////// COMPLETED-A");
                }
            }).observeOn(Schedulers.computation()).map(new Func1<Integer, String>() {

                int c = 0;

                @Override
                public String call(Integer l) {
                    if (g.getKey()) {
                        if (c++ < 400) {
                            try {
                                Thread.sleep(1);
                            } catch (InterruptedException e) {
                            }
                        }
                        return l + " is even.";
                    } else {
                        return l + " is odd.";
                    }
                }
            }).doOnCompleted(new Action0() {

                @Override
                public void call() {
                    System.out.println("//////////////////// COMPLETED-B");
                }
            });
        }
    }).doOnEach(new Action1<Notification<? super String>>() {

        @Override
        public void call(Notification<? super String> t1) {
            System.out.println("NEXT: " + t1);
        }
    }).subscribe(ts);
    ts.awaitTerminalEvent();
    ts.assertNoErrors();
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) Observable(rx.Observable) GroupedObservable(rx.observables.GroupedObservable) Notification(rx.Notification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(rx.observers.TestSubscriber) GroupedObservable(rx.observables.GroupedObservable) Test(org.junit.Test)

Example 9 with GroupedObservable

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

the class OperatorGroupByTest method testFirstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenSubscribesOnAndDelaysAndThenCompletes.

@Test
public void testFirstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenSubscribesOnAndDelaysAndThenCompletes() throws InterruptedException {
    System.err.println("----------------------------------------------------------------------------------------------");
    // 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.subscribeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Func1<Integer, String>() {

                    @Override
                    public String call(Integer t1) {
                        return "last group: " + t1;
                    }
                }).doOnEach(new Action1<Notification<? super String>>() {

                    @Override
                    public void call(Notification<? super String> t1) {
                        System.err.println("subscribeOn notification => " + t1);
                    }
                });
            }
        }
    }).doOnEach(new Action1<Notification<? super String>>() {

        @Override
        public void call(Notification<? super String> t1) {
            System.err.println("outer notification => " + 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) OnSubscribe(rx.Observable.OnSubscribe) CountDownLatch(java.util.concurrent.CountDownLatch) Notification(rx.Notification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(rx.observers.TestSubscriber) Subscriber(rx.Subscriber) Func1(rx.functions.Func1) GroupedObservable(rx.observables.GroupedObservable) Test(org.junit.Test)

Example 10 with GroupedObservable

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

the class OperatorGroupByTest 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>();
    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.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)

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