Search in sources :

Example 21 with LazyReact

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

the class LazySeqTest method zipFastSlow.

@Test
public void zipFastSlow() {
    Queue q = new Queue();
    LazyReact.parallelBuilder().generate(() -> sleep(100)).then(it -> q.add("100")).runThread(new Thread());
    new LazyReact().of(1, 2, 3, 4, 5, 6).zip(q.stream()).peek(it -> System.out.println(it)).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) Tuple2(cyclops.data.tuple.Tuple2) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) Matchers.not(org.hamcrest.Matchers.not) FutureStream(cyclops.futurestream.FutureStream) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) Arrays.asList(java.util.Arrays.asList) Matchers.lessThan(org.hamcrest.Matchers.lessThan) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Iterator(java.util.Iterator) Collection(java.util.Collection) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ThreadPools(com.oath.cyclops.react.ThreadPools) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Signal(com.oath.cyclops.async.adapters.Signal) Assert.assertEquals(org.junit.Assert.assertEquals) LazyReact(cyclops.futurestream.LazyReact) Queue(com.oath.cyclops.async.adapters.Queue) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

Example 22 with LazyReact

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

the class ConnectableTest method hotStreamConnect.

@Test
public void hotStreamConnect() throws InterruptedException {
    for (int i = 0; i < 10; i++) {
        System.out.println(i);
        value = null;
        CountDownLatch latch = new CountDownLatch(1);
        new LazyReact().range(0, Integer.MAX_VALUE).limit(100).peek(v -> value = v).peek(v -> latch.countDown()).peek(System.out::println).hotStream(exec).connect().limit(100).runFuture(ForkJoinPool.commonPool(), s -> s.forEach(System.out::println));
        latch.await();
        assertTrue(value != null);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Executor(java.util.concurrent.Executor) LazyReact(cyclops.futurestream.LazyReact) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) LazyReact(cyclops.futurestream.LazyReact) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with LazyReact

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

the class ConnectableTest method hotStreamConnectBlockingQueue.

@Test
public void hotStreamConnectBlockingQueue() throws InterruptedException {
    value = null;
    CountDownLatch latch = new CountDownLatch(1);
    new LazyReact().range(0, Integer.MAX_VALUE).limit(1000).peek(v -> value = v).peek(v -> latch.countDown()).hotStream(exec).connect(new LinkedBlockingQueue<>()).limit(100).runFuture(ForkJoinPool.commonPool(), s -> s.forEach(System.out::println));
    latch.await();
    assertTrue(value != null);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Executor(java.util.concurrent.Executor) LazyReact(cyclops.futurestream.LazyReact) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) LazyReact(cyclops.futurestream.LazyReact) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 24 with LazyReact

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

the class LazySeqObjectPoolingTest method shouldZipTwoInfiniteSequences.

@Test
@Ignore
public void shouldZipTwoInfiniteSequences() throws Exception {
    final FutureStream<Integer> units = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(1, n -> n + 1);
    final FutureStream<Integer> hundreds = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(100, n -> n + 100);
    final ReactiveSeq<String> zipped = units.zip(hundreds, (n, p) -> n + ": " + p);
    assertThat(zipped.limit(5).join(), equalTo(of("1: 100", "2: 200", "3: 300", "4: 400", "5: 500").join()));
}
Also used : LazyReact(cyclops.futurestream.LazyReact) Ignore(org.junit.Ignore) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

Example 25 with LazyReact

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

the class LazySeqObjectPoolingTest method shouldZipFiniteWithInfiniteSeq.

@Test
public void shouldZipFiniteWithInfiniteSeq() throws Exception {
    ThreadPools.setUseCommon(false);
    final ReactiveSeq<Integer> units = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(1, n -> n + 1).limit(5);
    // <-- MEMORY LEAK! - no auto-closing yet, so writes infinetely to it's async queue
    final FutureStream<Integer> hundreds = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(100, n -> n + 100);
    final ReactiveSeq<String> zipped = units.zip(hundreds, (n, p) -> n + ": " + p);
    assertThat(zipped.limit(5).join(), equalTo(of("1: 100", "2: 200", "3: 300", "4: 400", "5: 500").join()));
    ThreadPools.setUseCommon(true);
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) Matchers.not(org.hamcrest.Matchers.not) FutureStream(cyclops.futurestream.FutureStream) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) Arrays.asList(java.util.Arrays.asList) Matchers.lessThan(org.hamcrest.Matchers.lessThan) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Iterator(java.util.Iterator) Collection(java.util.Collection) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ThreadPools(com.oath.cyclops.react.ThreadPools) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Assert(org.junit.Assert) Signal(com.oath.cyclops.async.adapters.Signal) LazyReact(cyclops.futurestream.LazyReact) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) 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