Search in sources :

Example 41 with LazyReact

use of cyclops.futurestream.LazyReact in project cyclops by aol.

the class BatchingInvestigationsTest method streamBatch.

@Test
public void streamBatch() {
    Queue<String> queue = QueueFactories.<String>unboundedQueue().build();
    new Thread(() -> {
        for (int i = 0; i < 10; i++) {
            queue.offer("New message " + i);
            sleep(10000);
        }
        queue.close();
    }).start();
    long toRun = TimeUnit.MILLISECONDS.toNanos(500l);
    queue.streamBatch(new Subscription(), source -> {
        return () -> {
            List<String> result = new ArrayList<>();
            long start = System.nanoTime();
            while (result.size() < 10 && (System.nanoTime() - start) < toRun) {
                try {
                    String next = source.apply(1l, TimeUnit.MILLISECONDS);
                    if (next != null) {
                        result.add(next);
                    }
                } catch (Queue.QueueTimeoutException e) {
                }
            }
            if (result.size() > 0) {
                System.out.println("Result " + result);
            }
            start = System.nanoTime();
            return result;
        };
    }).filter(l -> l.size() > 0).to(s -> new LazyReact(ThreadPools.getSequential()).fromStream(s)).async().peek(System.out::println).run();
    while (true) {
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) QueueFactories(com.oath.cyclops.async.QueueFactories) Ignore(org.junit.Ignore) LazyReact(cyclops.futurestream.LazyReact) ThreadPools(com.oath.cyclops.react.ThreadPools) Test(org.junit.Test) StreamSource(cyclops.stream.StreamSource) Subscription(com.oath.cyclops.react.async.subscription.Subscription) ArrayList(java.util.ArrayList) LazyReact(cyclops.futurestream.LazyReact) ArrayList(java.util.ArrayList) Subscription(com.oath.cyclops.react.async.subscription.Subscription) Queue(com.oath.cyclops.async.adapters.Queue) Test(org.junit.Test)

Example 42 with LazyReact

use of cyclops.futurestream.LazyReact in project cyclops by aol.

the class BatchingInvestigationsTest method batchIssue.

@Test
public void batchIssue() throws InterruptedException {
    Queue<String> queue = QueueFactories.<String>unboundedQueue().build();
    new Thread(() -> {
        for (int i = 0; i < 10; i++) {
            queue.offer("New message " + i);
            sleep(10000);
        }
    }).start();
    queue.stream().groupedBySizeAndTime(10, 500, TimeUnit.MILLISECONDS).to(s -> new LazyReact(ThreadPools.getSequential()).fromStream(s)).async().peek(System.out::println).run();
    while (true) {
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) QueueFactories(com.oath.cyclops.async.QueueFactories) Ignore(org.junit.Ignore) LazyReact(cyclops.futurestream.LazyReact) ThreadPools(com.oath.cyclops.react.ThreadPools) Test(org.junit.Test) StreamSource(cyclops.stream.StreamSource) Subscription(com.oath.cyclops.react.async.subscription.Subscription) ArrayList(java.util.ArrayList) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 43 with LazyReact

use of cyclops.futurestream.LazyReact in project cyclops by aol.

the class BatchingInvestigationsTest method batchIssueStreamSource.

@Test
public void batchIssueStreamSource() throws InterruptedException {
    Queue<String> queue = QueueFactories.<String>unboundedQueue().build();
    new Thread(() -> {
        while (true) {
            sleep(1000);
            queue.offer("New message " + System.currentTimeMillis());
        }
    }).start();
    StreamSource.futureStream(queue, new LazyReact(ThreadPools.getSequential())).groupedBySizeAndTime(10, 500, TimeUnit.MILLISECONDS).forEach(i -> System.out.println(i + " Batch Time:" + System.currentTimeMillis()));
    while (true) {
    }
}
Also used : LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 44 with LazyReact

use of cyclops.futurestream.LazyReact in project cyclops by aol.

the class MiscTest method pVectorX.

@Test
public void pVectorX() {
    ReactiveSeq<String> seq = Spouts.from(VectorX.of(1, 2, 3, 4).plus(5).map(i -> "connect toNested Akka, RxJava and more with reactiveBuffer-streams" + i));
    PersistentSetX<String> setX = seq.to(s -> new LazyReact().fromStream(s)).map(data -> "fan out across threads with futureStreams" + data).to(ReactiveConvertableSequence::converter).persistentSetX();
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Arrays(java.util.Arrays) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) Spouts(cyclops.reactive.Spouts) LazyReact(cyclops.futurestream.LazyReact) VectorX(cyclops.reactive.collections.immutable.VectorX) Futures(cyclops.companion.Futures) Test(org.junit.Test) Future(cyclops.control.Future) Supplier(java.util.function.Supplier) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SimpleReact(cyclops.futurestream.SimpleReact) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) PersistentSetX(cyclops.reactive.collections.immutable.PersistentSetX) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 45 with LazyReact

use of cyclops.futurestream.LazyReact in project cyclops by aol.

the class PipesTest method futureStreamTest.

@Test
public void futureStreamTest() {
    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()).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)

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