use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class SignalTest method signalDiscrete3.
@Test
public void signalDiscrete3() {
try {
Signal<Integer> q = Signal.queueBackedSignal();
new SimpleReact().ofAsync(() -> q.set(1), () -> q.set(2), () -> {
sleep(20);
return q.set(4);
}, () -> {
sleep(400);
q.getDiscrete().close();
return 1;
});
parallel().fromStream(q.getDiscrete().streamCompletableFutures()).then(it -> "*" + it).peek(it -> incrementFound()).peek(it -> System.out.println(it)).block();
} finally {
assertThat(found, is(3));
}
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class TopicTest method multipleSubscribersGetSameMessagesSimpleReact.
@Test
public void multipleSubscribersGetSameMessagesSimpleReact() throws InterruptedException, ExecutionException {
Topic<String> topic = new Topic<>(new Queue<>());
Stream<String> input = Stream.of("hello", "world");
// read from the topic concurrently in 2 threads
BaseSimpleReactStream<Collection<String>> stage = new SimpleReact(new ForkJoinPool(2)).ofAsync(() -> parallel().fromStream(topic.stream()).then(it -> it + "*").block(), () -> parallel().fromStream(topic.stream()).then(it -> it + "!").peek(// make sure takes slightly longer to complete
it -> sleep(10)).block(Collectors.toSet()));
// make sure streams are set up
sleep(50);
topic.fromStream(input);
// wait until Topic has been read from
sleep(400);
topic.close();
List<Collection<String>> result = stage.block();
assertThat(result.get(0), instanceOf(List.class));
assertThat(result.get(0), hasItem("hello*"));
assertThat(result.get(0), hasItem("world*"));
assertThat(result.get(1), instanceOf(HashSet.class));
assertThat(result.get(1), hasItem("hello!"));
assertThat(result.get(1), hasItem("world!"));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class TopicTest method mergingAndSplittingSimpleReact.
// too non-deterministic to run regularly - relying on population from competing threads
@Test
// too non-deterministic to run regularly - relying on population from competing threads
@Ignore
public void mergingAndSplittingSimpleReact() {
Topic<Integer> topic = new Topic<>();
BaseSimpleReactStream<Collection<String>> stage = new SimpleReact(new ForkJoinPool(2)).ofAsync(() -> parallel().fromStream(topic.streamCompletableFutures()).then(it -> it + "*").block(Collectors.toList()), () -> parallel().fromStream(topic.streamCompletableFutures()).then(it -> it + "!").block(Collectors.toSet()));
// make sure streams are set up
sleep(50);
new SimpleReact(new ForkJoinPool(1)).ofAsync(() -> topic.fromStream(Stream.generate(() -> count++)));
new SimpleReact(new ForkJoinPool(1)).ofAsync(() -> topic.fromStream(Stream.generate(() -> count1++)));
// wait until Topic has been read from
sleep(40);
System.out.println("Closing!");
topic.close();
System.out.println("Closed! Blocking..");
List<Collection<String>> result = stage.block();
System.out.println("Completed " + result.size());
assertThat(extract1(result), hasItem("0*"));
assertThat(extract1(result), hasItem("100000*"));
assertThat(extract2(result), hasItem("0!"));
assertThat(extract2(result), hasItem("100000!"));
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class TopicTest method mergingAndSplitting.
// too non-deterministic to run regularly - relying on population from competing threads
@Test
// too non-deterministic to run regularly - relying on population from competing threads
@Ignore
public void mergingAndSplitting() {
Topic<Integer> topic = new Topic<>();
Stream<Integer> stream1 = topic.stream();
Stream<Integer> stream2 = topic.stream();
new SimpleReact().ofAsync(() -> topic.fromStream(Stream.generate(() -> count++)));
new SimpleReact().ofAsync(() -> topic.fromStream(Stream.generate(() -> count1++)));
for (Stream<Integer> stream : Arrays.asList(stream1, stream2)) {
List<Integer> result = stream.limit(1000).peek(it -> System.out.println(it)).collect(Collectors.toList());
assertThat(result, hasItem(100000));
assertThat(result, hasItem(0));
}
}
use of cyclops.futurestream.SimpleReact in project cyclops by aol.
the class QueueTest method mergingTestEagerStreamMerge.
@Test
@Ignore
public // competing threads
void mergingTestEagerStreamMerge() {
count = 0;
count1 = 100000;
Queue<Integer> q = new Queue(new LinkedBlockingQueue());
new SimpleReact().ofAsync(() -> q.fromStream(Stream.generate(() -> count++)));
new SimpleReact().ofAsync(() -> q.fromStream(Stream.generate(() -> count1++)));
List<Integer> result = q.stream().limit(1000).peek(it -> System.out.println(it)).collect(Collectors.toList());
assertThat(result, hasItem(100000));
assertThat(result, hasItem(0));
}
Aggregations