Search in sources :

Example 1 with QueueFactories

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) {
    }
}
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 2 with QueueFactories

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();
    }
}
Also used : Arrays(java.util.Arrays) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) FutureStream(cyclops.futurestream.FutureStream) CompletableFuture(java.util.concurrent.CompletableFuture) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) Before(org.junit.Before) Spouts(cyclops.reactive.Spouts) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) Collection(java.util.Collection) BaseSimpleReactStream.parallel(com.oath.cyclops.types.futurestream.BaseSimpleReactStream.parallel) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ExecutionException(java.util.concurrent.ExecutionException) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) ForkJoinPool(java.util.concurrent.ForkJoinPool) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with QueueFactories

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));
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Arrays(java.util.Arrays) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) NoSuchElementException(java.util.NoSuchElementException) Pipes(cyclops.futurestream.Pipes) Before(org.junit.Before) cyclops.control(cyclops.control) Executor(java.util.concurrent.Executor) lombok.val(lombok.val) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Executors(java.util.concurrent.Executors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) QueueBasedSubscriber(com.oath.cyclops.types.reactive.QueueBasedSubscriber) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 4 with QueueFactories

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));
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Arrays(java.util.Arrays) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) NoSuchElementException(java.util.NoSuchElementException) Pipes(cyclops.futurestream.Pipes) Before(org.junit.Before) cyclops.control(cyclops.control) Executor(java.util.concurrent.Executor) lombok.val(lombok.val) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Executors(java.util.concurrent.Executors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) QueueBasedSubscriber(com.oath.cyclops.types.reactive.QueueBasedSubscriber) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 5 with QueueFactories

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Matchers.not(org.hamcrest.Matchers.not) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before) ReactiveSeq.of(cyclops.reactive.ReactiveSeq.of) Collection(java.util.Collection) BaseSimpleReactStream.parallel(com.oath.cyclops.types.futurestream.BaseSimpleReactStream.parallel) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Aggregations

QueueFactories (com.oath.cyclops.async.QueueFactories)9 LazyReact (cyclops.futurestream.LazyReact)9 Ignore (org.junit.Ignore)9 Test (org.junit.Test)9 List (java.util.List)8 Assert.assertThat (org.junit.Assert.assertThat)7 Queue (com.oath.cyclops.async.adapters.Queue)6 ArrayList (java.util.ArrayList)6 Before (org.junit.Before)6 ReactiveSeq (cyclops.reactive.ReactiveSeq)5 Assert.assertFalse (org.junit.Assert.assertFalse)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 SimpleReact (cyclops.futurestream.SimpleReact)4 Arrays (java.util.Arrays)4 Executors (java.util.concurrent.Executors)4 ForkJoinPool (java.util.concurrent.ForkJoinPool)4 TimeUnit (java.util.concurrent.TimeUnit)4 Matchers.equalTo (org.hamcrest.Matchers.equalTo)4 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)4 BaseSimpleReactStream (com.oath.cyclops.types.futurestream.BaseSimpleReactStream)3