Search in sources :

Example 6 with LazyReact

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));
}
Also used : LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 7 with LazyReact

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);
}
Also used : LazyReact(cyclops.futurestream.LazyReact) AbstractVerticle(io.vertx.core.AbstractVerticle) Vertx(io.vertx.core.Vertx) LazyReact(cyclops.futurestream.LazyReact)

Example 8 with LazyReact

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);
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SimpleReact(cyclops.futurestream.SimpleReact) HttpServerOptions(io.vertx.core.http.HttpServerOptions) WaitStrategy(com.oath.cyclops.async.wait.WaitStrategy) LazyReact(cyclops.futurestream.LazyReact) Vertx(io.vertx.core.Vertx) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with LazyReact

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) {
    }
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SimpleReact(cyclops.futurestream.SimpleReact) HttpServerOptions(io.vertx.core.http.HttpServerOptions) WaitStrategy(com.oath.cyclops.async.wait.WaitStrategy) CompletableFuture(java.util.concurrent.CompletableFuture) LazyReact(cyclops.futurestream.LazyReact) SimpleReact(cyclops.futurestream.SimpleReact) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Vertx(io.vertx.core.Vertx) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with LazyReact

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyReact(cyclops.futurestream.LazyReact) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

LazyReact (cyclops.futurestream.LazyReact)60 Test (org.junit.Test)58 List (java.util.List)27 Assert.assertThat (org.junit.Assert.assertThat)25 Queue (com.oath.cyclops.async.adapters.Queue)21 Matchers.equalTo (org.hamcrest.Matchers.equalTo)21 Ignore (org.junit.Ignore)20 ReactiveSeq (cyclops.reactive.ReactiveSeq)19 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)19 QueueFactories (com.oath.cyclops.async.QueueFactories)17 Arrays.asList (java.util.Arrays.asList)16 Collectors (java.util.stream.Collectors)16 Matchers.is (org.hamcrest.Matchers.is)16 ArrayList (java.util.ArrayList)15 ForkJoinPool (java.util.concurrent.ForkJoinPool)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Tuple2 (cyclops.data.tuple.Tuple2)14 FutureStream (cyclops.futurestream.FutureStream)13 ThreadPools (com.oath.cyclops.react.ThreadPools)12 Tuple.tuple (cyclops.data.tuple.Tuple.tuple)12