use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class ResultCollectionTest method testBreakoutAllCompletedAndTime.
@Test
public void testBreakoutAllCompletedAndTime() throws InterruptedException, ExecutionException {
count = 0;
List<Integer> results = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).then(it -> it * 100).then(it -> {
sleep(it);
return it;
}).capture(e -> count++).block(status -> status.getAllCompleted() > 1 && status.getElapsedMillis() > 200);
assertThat(results.size(), greaterThan(1));
assertThat(count, is(0));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class AlgorithmCompareTest method testFastestLessBlocking.
@Test
public void testFastestLessBlocking() throws InterruptedException, ExecutionException {
ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
for (int i = 0; i < 1001; i++) {
arrayList.add(i);
linkedList.add(i);
}
SimpleTimer timer = new SimpleTimer();
Result result = new SimpleReact().<Result>ofAsync(() -> Result.builder().name("approach1 : arrayList").result(retrieval(arrayList)).build(), () -> Result.builder().name("approach2 : linkedList").result(retrieval(linkedList)).build()).then(it -> it.withTime(timer.getElapsedNanoseconds())).filter(it -> it.getResult() == 1000).block().firstValue(null);
assertThat(result.getName(), is("approach1 : arrayList"));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class QueueTest method backPressureTimeoutTestVeryHigh.
@Test
public void backPressureTimeoutTestVeryHigh() {
Queue<Integer> q = new Queue<Integer>(new LinkedBlockingQueue<>(2)).withOfferTimeout(1).withOfferTimeUnit(TimeUnit.DAYS);
BaseSimpleReactStream<Boolean> s = new SimpleReact().ofAsync(() -> offerAndIncrementFound(q), () -> offerAndIncrementFound(q), () -> offerAndIncrementFound(q), () -> offerAndIncrementFound(q));
sleep(10);
assertThat(found.get(), is(2));
assertThat(q.stream().limit(4).collect(Collectors.toList()).size(), is(4));
Set<Boolean> results = s.block(Collectors.toSet());
assertThat(found.get(), is(4));
assertThat(results.size(), is(1));
// some offers failed.
assertThat(results, not(hasItem(false)));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class QueueTest method queueTestRun.
@Test
public void queueTestRun() {
try {
Queue<Integer> q = new Queue<>(new LinkedBlockingQueue<>());
new SimpleReact().ofAsync(() -> q.offer(1), () -> q.offer(2), () -> {
sleep(20);
return q.offer(4);
}, () -> {
sleep(400);
q.close();
return 1;
});
List<String> result = parallel().fromStream(q.stream()).then(it -> "*" + it).peek(it -> found.getAndAdd(1)).peek(it -> System.out.println(it)).block();
assertThat(result, hasItem("*1"));
} finally {
assertThat(found.get(), is(3));
}
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class QueueTest method queueTestBlock.
@Test
public void queueTestBlock() {
try {
Queue q = new Queue<>(new LinkedBlockingQueue<>());
new SimpleReact().ofAsync(() -> q.offer(1), () -> q.offer(2), () -> {
sleep(50);
return q.offer(4);
}, () -> {
sleep(400);
q.close();
return 1;
});
parallel().fromStream(q.streamCompletableFutures()).then(it -> "*" + it).peek(it -> found.getAndAdd(1)).peek(it -> System.out.println(it)).block();
} finally {
assertThat(found.get(), is(3));
}
}
Aggregations