use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class QueueTest method backPressureJDKTest.
@Test
public void backPressureJDKTest() {
Queue<String> q = new Queue<>(new LinkedBlockingQueue<>(2));
new SimpleReact().ofAsync(() -> {
Stream.of("1", "2", "3", "4").forEach(it -> {
q.offer(it);
found.getAndAdd(1);
});
return 1;
});
sleep(10);
assertThat(found.get(), is(2));
assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
sleep(10);
assertThat(found.get(), is(4));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class QueueTest method queueTestTimeout.
@Test
public void queueTestTimeout() {
Queue<Integer> q = new Queue<>(new LinkedBlockingQueue<Integer>()).withTimeout(1).withTimeUnit(TimeUnit.MILLISECONDS);
new SimpleReact().ofAsync(() -> q.offer(1), () -> q.offer(2), () -> {
sleep(500);
return q.offer(4);
}, () -> q.offer(5), () -> {
sleep(1000);
return q.close();
});
Collection<String> results = parallel().fromStream(q.stream()).then(it -> "*" + it).block();
assertThat(results.size(), equalTo(4));
assertThat(results, hasItem("*5"));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class QueueTest method backPressureTest.
@Test
public void backPressureTest() {
Queue<Integer> q = new Queue<>(new LinkedBlockingQueue<>(2));
new SimpleReact().ofAsync(() -> {
q.offer(1);
return found.getAndAdd(1);
}, () -> {
q.offer(1);
return found.getAndAdd(1);
}, () -> {
q.offer(6);
return found.getAndAdd(1);
}, () -> {
q.offer(5);
return found.getAndAdd(1);
});
sleep(10);
assertThat(found.get(), is(2));
assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
assertThat(q.stream().limit(2).collect(Collectors.toList()).size(), is(2));
sleep(10);
assertThat(found.get(), is(4));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class LazyStream method run.
/**
* Trigger a lazy stream as a task on the provided Executor
*/
default void run() {
final SimpleReact reactor = SequentialElasticPools.simpleReact.nextReactor();
reactor.ofAsync(() -> run(new NonCollector<>())).peek(n -> SequentialElasticPools.simpleReact.populate(reactor)).onFail(n -> {
SequentialElasticPools.simpleReact.populate(reactor);
return 1;
});
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class OnFailTest method chained2.
@Test
public void chained2() {
new SimpleReact().ofAsync(() -> 1, () -> 2).then(this::throwException).then(i -> i + 2).onFail(IOException.class, e -> shouldNeverBeCalled.incrementAndGet()).onFail(RuntimeException.class, e -> shouldBeCalled.incrementAndGet()).onFail(ClosedQueueException.class, e -> shouldNeverBeReached.incrementAndGet()).block();
assertThat(shouldNeverBeCalled.get(), equalTo(0));
assertThat(shouldBeCalled.get(), equalTo(2));
assertThat(shouldNeverBeReached.get(), equalTo(0));
}
Aggregations