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());
}
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());
}
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());
}
Aggregations