use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.
the class IterableFlatMapTest method flatMapAsyncRS.
@Test
public void flatMapAsyncRS() {
for (int k = 0; k < 1000; k++) {
complete = new AtomicBoolean(false);
count = new AtomicInteger(0);
ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
Spouts.of(1, 2, 3).peek(System.out::println).concatMap(i -> nextAsyncRS()).subscribe(new Subscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
subs = s;
}
@Override
public void onNext(Integer integer) {
System.out.println("RECIEVED " + integer);
assertThat(integer, Matchers.isOneOf(1, 2));
System.out.println("count " + count.incrementAndGet());
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
complete.set(true);
}
});
subs.request(Long.MAX_VALUE);
while (!complete.get()) {
}
assertThat(count.get(), equalTo(6));
}
}
use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.
the class FlatMapTest method flatMapAsyncRS.
@Test
public void flatMapAsyncRS() {
for (int k = 0; k < 1000; k++) {
complete = new AtomicBoolean(false);
count = new AtomicInteger(0);
ReactiveSubscriber<Integer> sub = Spouts.reactiveSubscriber();
Spouts.of(1, 2, 3).peek(System.out::println).flatMap(i -> nextAsyncRS()).subscribe(new Subscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
subs = s;
}
@Override
public void onNext(Integer integer) {
System.out.println("RECIEVED " + integer);
assertThat(integer, Matchers.isOneOf(1, 2));
System.out.println("count " + count.incrementAndGet());
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
complete.set(true);
}
});
subs.request(Long.MAX_VALUE);
while (!complete.get()) {
}
assertThat(count.get(), equalTo(6));
}
}
use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.
the class Spouts method schedule.
static <T> ReactiveSeq<T> schedule(final Stream<T> stream, final String cron, final ScheduledExecutorService exec) {
ReactiveSubscriber<T> sub = reactiveSubscriber();
AtomicBoolean isOpen = new AtomicBoolean(true);
Subscription[] s = { null };
sub.onSubscribe(new Subscription() {
@Override
public void request(long n) {
s[0].request(n);
}
@Override
public void cancel() {
isOpen.set(false);
}
});
s[0] = ReactiveSeq.fromStream(stream).takeWhile(e -> isOpen.get()).schedule(cron, exec).connect().forEach(0, e -> sub.onNext(e), t -> sub.onError(t), () -> sub.onComplete());
return sub.reactiveStream();
}
use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.
the class Spouts method interval.
static ReactiveSeq<Integer> interval(String cron, ScheduledExecutorService exec) {
ReactiveSubscriber<Integer> sub = reactiveSubscriber();
AtomicBoolean isOpen = new AtomicBoolean(true);
Subscription[] s = { null };
sub.onSubscribe(new Subscription() {
@Override
public void request(long n) {
s[0].request(n);
}
@Override
public void cancel() {
isOpen.set(false);
}
});
s[0] = ReactiveSeq.iterate(1, a -> a + 1).takeWhile(e -> isOpen.get()).schedule(cron, exec).connect().forEach(1, e -> sub.onNext(e));
return sub.reactiveStream();
}
use of com.oath.cyclops.types.reactive.ReactiveSubscriber in project cyclops by aol.
the class Spouts method interval.
static ReactiveSeq<Integer> interval(final long millis, ScheduledExecutorService exec) {
ReactiveSubscriber<Integer> sub = reactiveSubscriber();
AtomicBoolean isOpen = new AtomicBoolean(true);
Subscription[] s = { null };
sub.onSubscribe(new Subscription() {
@Override
public void request(long n) {
s[0].request(n);
}
@Override
public void cancel() {
isOpen.set(false);
}
});
s[0] = ReactiveSeq.iterate(1, a -> a + 1).takeWhile(e -> isOpen.get()).scheduleFixedDelay(millis, exec).connect().forEach(1, e -> sub.onNext(e));
return sub.reactiveStream();
}
Aggregations