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