Search in sources :

Example 1 with GroupedObservable

use of rx.observables.GroupedObservable in project pinpoint by pinpoint-apm.

the class GroupedObservableTestRunner method groupedObservable.

public void groupedObservable() throws Exception {
    int numMessageChunks = 2;
    final List<String> messages = new ArrayList<String>();
    final List<String> messageChunk = Arrays.asList("Hello", "World");
    for (int i = 0; i < numMessageChunks; i++) {
        messages.addAll(messageChunk);
    }
    final CountDownLatch completeLatch = new CountDownLatch(1);
    final List<String> helloMessages = Collections.synchronizedList(new ArrayList<String>());
    final List<String> worldMessages = Collections.synchronizedList(new ArrayList<String>());
    Observable<GroupedObservable<String, String>> grouped = echoesService.echo(messages).subscribeOn(Schedulers.computation()).groupBy(new Func1<String, String>() {

        @Override
        public String call(String s) {
            return s;
        }
    });
    grouped.subscribe(new Action1<GroupedObservable<String, String>>() {

        @Override
        public void call(final GroupedObservable<String, String> groupedObservable) {
            String key = groupedObservable.getKey();
            if (key.equals("Hello")) {
                groupedObservable.subscribe(new Action1<String>() {

                    @Override
                    public void call(String s) {
                        helloMessages.add(s);
                    }
                });
            } else if (key.equals("World")) {
                groupedObservable.subscribe(new Action1<String>() {

                    @Override
                    public void call(String s) {
                        worldMessages.add(s);
                    }
                });
            }
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            completeLatch.countDown();
        }
    }, new Action0() {

        @Override
        public void call() {
            completeLatch.countDown();
        }
    });
    completeLatch.await(500L, TimeUnit.MILLISECONDS);
    Assert.assertEquals(messages.size() / messageChunk.size(), helloMessages.size());
    Assert.assertEquals(messages.size() / messageChunk.size(), worldMessages.size());
    for (String helloMessage : helloMessages) {
        Assert.assertEquals("Hello", helloMessage);
    }
    for (String worldMessage : worldMessages) {
        Assert.assertEquals("World", worldMessage);
    }
    TestHelper.awaitForSpanDataFlush();
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.awaitTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"), 20, 3000);
    verifier.printCache();
    // Skip rx java internal traces as they differ between versions and it's too much work to split the tests.
    // Instead, we can verify them indirectly by checking if user methods have been traced.
    verifier.ignoreServiceType("RX_JAVA_INTERNAL");
    Method groupByMethod = Observable.class.getDeclaredMethod("groupBy", Func1.class);
    verifier.verifyTrace(event("RX_JAVA", groupByMethod));
    Method subscribeMethod = Observable.class.getDeclaredMethod("subscribe", Action1.class, Action1.class, Action0.class);
    verifier.verifyTrace(event("RX_JAVA", subscribeMethod));
    // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0)
    verifier.verifyTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"));
    // event - RX_JAVA_INTERNAL some form of Action0 implementation's call() inside OperatorSubscribeOn that gets scheduled
    Method echoMethod = EchoRepository.class.getDeclaredMethod("echo", String.class);
    Method groupedObservableSubscribeMethod = Observable.class.getDeclaredMethod("subscribe", Action1.class);
    for (int i = 0; i < numMessageChunks; i++) {
        for (int j = 0; j < messageChunk.size(); j++) {
            verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), echoMethod));
            if (i == 0) {
                verifier.verifyTrace(event("RX_JAVA", groupedObservableSubscribeMethod));
            }
        }
    }
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) GroupedObservable(rx.observables.GroupedObservable)

Example 2 with GroupedObservable

use of rx.observables.GroupedObservable in project risky by amsa-code.

the class DistanceTravelledInEezMain method calculateDistance.

private static Observable<? extends Vessel> calculateDistance(File file, Shapefile eezLine, Shapefile eezPolygon, GroupedObservable<Integer, Fix> o) {
    return Observable.defer(() -> {
        State state = new State();
        state.date = file.getName().substring(0, file.getName().indexOf(".track.gz"));
        state.mmsi = o.getKey();
        state.location = Location.UNKNOWN;
        return // 
        o.compose(// 
        Downsample.minTimeStep(5, TimeUnit.MINUTES)).lift(new OperatorEffectiveSpeedChecker(SegmentOptions.builder().acceptAnyFixHours(480L).maxSpeedKnots(50).build())).filter(// 
        check -> check.isOk()).map(// 
        check -> check.fix()).doOnNext(fix -> {
            // TODO unit test
            boolean inside = eezPolygon.contains(fix.lat(), fix.lon());
            Location location = inside ? Location.IN : Location.OUT;
            if (state.location != Location.UNKNOWN) {
                boolean crossed = state.location != location;
                if (crossed) {
                    TimedPosition point = ShapefileUtil.findRegionCrossingPoint(eezLine, state.fix, fix);
                    final double distance;
                    if (location == Location.IN) {
                        distance = distanceKm(fix.lat(), fix.lon(), point.lat, point.lon);
                    } else {
                        distance = distanceKm(state.fix.lat(), state.fix.lon(), point.lat, point.lon);
                    }
                    state.distanceKm += distance;
                    double d = distanceKm(state.fix.lat(), state.fix.lon(), fix.lat(), fix.lon());
                    if (d >= MIN_DISTANCE_KM_TO_ESTIMATE_TIME) {
                        // we ensure that d is not close to zero so that the time estimate does not get
                        // blown out by instability in the division.
                        state.totalTimeMs += distance / d * (fix.time() - state.fix.time());
                    }
                } else if (location == Location.IN) {
                    state.distanceKm += distanceKm(state.fix.lat(), state.fix.lon(), fix.lat(), fix.lon());
                    state.totalTimeMs += fix.time() - state.fix.time();
                }
            }
            state.fix = fix;
            state.location = location;
        }).count().map(count -> new Vessel(count, state));
    });
}
Also used : Arrays(java.util.Arrays) Downsample(au.gov.amsa.risky.format.Downsample) Date(java.util.Date) SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) OperatorEffectiveSpeedChecker(au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker) SimpleDateFormat(java.text.SimpleDateFormat) ShapefileUtil(au.gov.amsa.geo.ShapefileUtil) BufferedOutputStream(java.io.BufferedOutputStream) TimedPosition(au.gov.amsa.geo.TimedPosition) Observable(rx.Observable) Logger(org.apache.log4j.Logger) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) Fix(au.gov.amsa.risky.format.Fix) Schedulers(rx.schedulers.Schedulers) Eez(au.gov.amsa.geo.Eez) ParseException(java.text.ParseException) PrintStream(java.io.PrintStream) TimeZone(java.util.TimeZone) DecimalFormat(java.text.DecimalFormat) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) BinaryFixesFormat(au.gov.amsa.risky.format.BinaryFixesFormat) TimeUnit(java.util.concurrent.TimeUnit) Position(com.github.davidmoten.grumpy.core.Position) List(java.util.List) GroupedObservable(rx.observables.GroupedObservable) Shapefile(au.gov.amsa.gt.Shapefile) MmsiValidator2(au.gov.amsa.util.identity.MmsiValidator2) OperatorEffectiveSpeedChecker(au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker) TimedPosition(au.gov.amsa.geo.TimedPosition)

Example 3 with GroupedObservable

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

the class OperatorGroupByTest method doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream.

private void doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(Observable<Event> es, AtomicInteger subscribeCounter) throws InterruptedException {
    final AtomicInteger eventCounter = new AtomicInteger();
    final AtomicInteger groupCounter = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(1);
    es.groupBy(new Func1<Event, Integer>() {

        @Override
        public Integer call(Event e) {
            return e.source;
        }
    }).take(// we want only the first group
    1).flatMap(new Func1<GroupedObservable<Integer, Event>, Observable<String>>() {

        @Override
        public Observable<String> call(GroupedObservable<Integer, Event> eventGroupedObservable) {
            System.out.println("testUnsubscribe => GroupedObservable Key: " + eventGroupedObservable.getKey());
            groupCounter.incrementAndGet();
            return eventGroupedObservable.take(// limit to only 20 events on this group
            20).map(new Func1<Event, String>() {

                @Override
                public String call(Event event) {
                    return "testUnsubscribe => 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();
        }
    });
    if (!latch.await(2000, TimeUnit.MILLISECONDS)) {
        fail("timed out so likely did not unsubscribe correctly");
    }
    assertEquals(1, subscribeCounter.get());
    assertEquals(1, groupCounter.get());
    assertEquals(20, eventCounter.get());
// sentEvents will go until 'eventCounter' hits 20 and then unsubscribes
// which means it will also send (but ignore) the 19/20 events for the other group
// It will not however send all 100 events.
}
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)

Example 4 with GroupedObservable

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

the class OperatorGroupByTest method testUnsubscribeViaTakeOnGroupThenTakeOnInner.

@Test
public void testUnsubscribeViaTakeOnGroupThenTakeOnInner() {
    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) {
            int numToTake = 0;
            if (eventGroupedObservable.getKey() == 1) {
                numToTake = 10;
            } else if (eventGroupedObservable.getKey() == 2) {
                numToTake = 5;
            }
            return eventGroupedObservable.take(numToTake).map(new Func1<Event, String>() {

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

        @Override
        public void call(String s) {
            eventCounter.incrementAndGet();
            System.out.println("=> " + s);
        }
    });
    assertEquals(15, eventCounter.get());
    // we should send 22 additional events that are filtered out as they are skipped while taking the 15 we want
    assertEquals(37, sentEventCounter.get());
}
Also used : 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 5 with GroupedObservable

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

the class OperatorGroupByTest method testStaggeredCompletion.

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

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

        @Override
        public Observable<Integer> call(GroupedObservable<Integer, Integer> group) {
            if (group.getKey() == 0) {
                return group.delay(100, TimeUnit.MILLISECONDS).map(new Func1<Integer, Integer>() {

                    @Override
                    public Integer call(Integer t) {
                        return t * 10;
                    }
                });
            } else {
                return group;
            }
        }
    }).subscribe(new Subscriber<Integer>() {

        @Override
        public void onCompleted() {
            System.out.println("=> onCompleted");
            latch.countDown();
        }

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

        @Override
        public void onNext(Integer s) {
            eventCounter.incrementAndGet();
            System.out.println("=> " + s);
        }
    });
    if (!latch.await(3000, TimeUnit.MILLISECONDS)) {
        fail("timed out");
    }
    assertEquals(100, 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)

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