use of com.oath.cyclops.async.QueueFactories 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 com.oath.cyclops.async.QueueFactories in project cyclops by aol.
the class TopicTest method concurrentSub.
@Test
public void concurrentSub() {
ReactiveSeq<Integer> initialStream = ReactiveSeq.of(1, 2, 3, 4, 5, 6);
FutureStream<Integer> futureStream = FutureStream.builder().fromStream(initialStream).map(v -> v - 1);
Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(1000).build();
Topic<Integer> topic = new Topic<Integer>(queue, QueueFactories.<Integer>boundedNonBlockingQueue(1000));
ReactiveSeq<Integer> s2 = topic.stream();
ReactiveSeq<Integer> s1 = topic.stream();
Thread t = new Thread(() -> {
topic.fromStream(futureStream);
topic.close();
});
t.start();
CompletableFuture future1 = CompletableFuture.runAsync(() -> s1.forEach(v -> System.out.println("1 -> " + v)));
CompletableFuture future2 = CompletableFuture.runAsync(() -> s2.forEach(v -> System.out.println("2 -> " + v)));
try {
future1.get();
future2.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
use of com.oath.cyclops.async.QueueFactories in project cyclops by aol.
the class PipesTest method futureStreamCustomTest.
@Test
public void futureStreamCustomTest() {
Pipes<String, Integer> bus = Pipes.of();
bus.register("reactor", QueueFactories.<Integer>boundedNonBlockingQueue(1000).build());
bus.publishTo("reactor", ReactiveSeq.of(10, 20, 30));
bus.close("reactor");
System.out.println(Thread.currentThread().getId());
List<String> res = bus.futureStream("reactor", new LazyReact(10, 10)).toOptional().get().map(i -> "fan-out toNested handle blocking I/O:" + Thread.currentThread().getId() + ":" + i).toList();
System.out.println(res);
assertThat(res.size(), equalTo(3));
}
use of com.oath.cyclops.async.QueueFactories in project cyclops by aol.
the class PipesTest method publishToTest.
@Test
public void publishToTest() {
Pipes<String, Integer> bus = Pipes.of();
bus.register("reactor", QueueFactories.<Integer>boundedNonBlockingQueue(1000).build());
bus.publishTo("reactor", Flux.just(10, 20, 30));
bus.close("reactor");
System.out.println(Thread.currentThread().getId());
List<String> res = bus.futureStream("reactor", new LazyReact()).toOptional().get().map(i -> "fan-out toNested handle blocking I/O:" + Thread.currentThread().getId() + ":" + i).toList();
System.out.println(res);
assertThat(res.size(), equalTo(3));
}
use of com.oath.cyclops.async.QueueFactories in project cyclops by aol.
the class QueueTest method parallelStream.
@Test
public void parallelStream() {
success = false;
AtomicLong threadId = new AtomicLong(Thread.currentThread().getId());
Queue<Integer> q = QueueFactories.<Integer>boundedQueue(2000).build();
for (int i = 0; i < 10000; i++) {
q.add(i);
}
System.out.println(" queue " + q.size());
System.out.println(threadId.get());
q.jdkStream().parallel().peek(System.out::println).peek(i -> {
System.out.println(Thread.currentThread().getId());
if (threadId.get() != Thread.currentThread().getId()) {
System.out.println("closing");
success = true;
q.close();
}
}).peek(i -> System.out.println(Thread.currentThread().getId())).forEach(System.out::println);
assertTrue(success);
}
Aggregations