use of cyclops.futurestream.LazyReact in project cyclops by aol.
the class PushableStreamTest method testWithBackPressureNegativeAfterButOn.
@Test(expected = IllegalArgumentException.class)
public void testWithBackPressureNegativeAfterButOn() {
PushableFutureStream<Integer> pushable = StreamSource.of(-10).futureStream(new LazyReact());
pushable.getInput().add(100);
pushable.getInput().close();
assertThat(pushable.getStream().collect(Collectors.toList()), hasItem(100));
}
use of cyclops.futurestream.LazyReact in project cyclops by aol.
the class Starter method start.
@Override
public void start() throws Exception {
super.start();
// this can't work as the tasks from cyclops2-react are being passed back into the
// event loop that is executing this task, LazyReact needs to pass tasks to a different
// event loop
LazyReact react = new LazyReact(new VertxExecutor(getVertx()));
int number = react.of(1, 2, 3).map(i -> i + 1).reduce((a, b) -> a + b).orElse(Integer.MIN_VALUE);
// 2 + 3 + 4 = 9
System.out.println("sum = " + number);
}
use of cyclops.futurestream.LazyReact in project cyclops by aol.
the class VertxTest method downloadUrls.
@Test
@Ignore
public void downloadUrls() {
// cyclops2-react async.Queues
Queue<String> downloadQueue = new Queue<String>();
Queue<String> completedQueue = new Queue<String>();
// vert.x meets cyclops2-react
Vertx vertx = Vertx.factory.vertx();
LazyReact react = new LazyReact(c -> vertx.runOnContext(v -> c.run()));
// populate the download queue asynchronously
ReactiveSeq.of("www.aol.com", "www.rte.ie", "www.aol.com").peek(next -> System.out.println("adding toNested download queue " + next)).runFuture(c -> vertx.runOnContext(v -> c.run()), t -> t.forEach(downloadQueue::add, System.err::println));
// download asynchronously : all cyclops2-react tasks are passed into vert.x
react.fromStream(downloadQueue.stream()).peek(System.out::println).map(url -> vertx.createHttpClient().getNow(url, "", resp -> resp.bodyHandler(body -> completedQueue.add(body.getString(0, body.length()))))).run();
// handle the results
completedQueue.stream().peek(next -> System.out.println("just downloaded" + next)).forEach(System.out::println);
}
use of cyclops.futurestream.LazyReact in project cyclops by aol.
the class VertxTest method httpServer.
@Test
@Ignore
public void httpServer() {
Vertx vertx = Vertx.factory.vertx();
CompletableFuture<HttpServer> server = new CompletableFuture<>();
Queue<HttpServerRequest> reqs = QueueFactories.<HttpServerRequest>boundedNonBlockingQueue(1000, WaitStrategy.spinWait()).build();
vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost")).requestHandler(event -> {
reqs.add(event);
System.out.println(event.absoluteURI());
}).listen(e -> {
if (e.succeeded())
server.complete(e.result());
else
server.completeExceptionally(e.cause());
});
LazyReact react = new LazyReact(c -> vertx.runOnContext(v -> c.run()));
react.fromStream(reqs.stream()).filter(req -> req.getParam("num") != null).peek(i -> System.out.println("grouping " + i)).grouped(2).map(list -> tuple(list.getOrElse(0, null).response(), list.getOrElse(1, null).response(), getParam(list.getOrElse(0, null)), getParam(list.getOrElse(1, null)))).peek(i -> System.out.println("peeking + " + i)).peek(t -> t._1().end("adding " + (t._3() + t._4()))).peek(t -> t._2().end("multiplying " + t._3() * t._4())).run();
new SimpleReact(c -> vertx.runOnContext(v -> c.run())).from(server).then(s -> "server started").onFail(e -> "failed toNested skip " + e.getMessage()).peek(System.out::println);
while (true) {
}
}
use of cyclops.futurestream.LazyReact in project cyclops by aol.
the class MiscTest method test.
@Test
public void test() {
final Supplier<Integer> count = countGen(new AtomicInteger(1));
final Optional<Integer> sum = new LazyReact(100, 100).generate(count).limit(10).reduce((a, b) -> a + b);
assertThat(sum.get(), equalTo(55));
}
Aggregations