Search in sources :

Example 1 with FutureStream

use of cyclops.futurestream.FutureStream 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 2 with FutureStream

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

the class LazySeqAgronaTest method zipFastSlow.

@Test
public void zipFastSlow() {
    FutureStream<Integer> s;
    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 : 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) Matchers.hasItems(org.hamcrest.Matchers.hasItems) Supplier(java.util.function.Supplier) 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) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) 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) LazyReact(cyclops.futurestream.LazyReact) Queue(com.oath.cyclops.async.adapters.Queue) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

Example 3 with FutureStream

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

the class LazySeqAgronaTest method testZipWithFuturesCoreStream.

@Test
public void testZipWithFuturesCoreStream() {
    Stream stream = Stream.of("a", "b");
    FutureStream<Tuple2<Integer, String>> seq = of(1, 2).actOnFutures().zip(stream);
    // .map(tuple -> Tuple.tuple(tuple.v1.join(),tuple.v2)).collect(CyclopsCollectors.toList());
    List<Tuple2<Integer, String>> result = seq.block();
    assertThat(result.size(), is(asList(tuple(1, "a"), tuple(2, "b")).size()));
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) FutureStream(cyclops.futurestream.FutureStream) Stream(java.util.stream.Stream) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

Example 4 with FutureStream

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

the class LazySeqAgronaTest method testZipWithFutures.

@Test
public void testZipWithFutures() {
    FutureStream stream = of("a", "b");
    FutureStream<Tuple2<Integer, String>> seq = of(1, 2).actOnFutures().zip(stream);
    // .map(tuple -> Tuple.tuple(tuple.v1.join(),tuple.v2)).collect(CyclopsCollectors.toList());
    List<Tuple2<Integer, String>> result = seq.block();
    assertThat(result.size(), is(asList(tuple(1, "a"), tuple(2, "b")).size()));
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) FutureStream(cyclops.futurestream.FutureStream) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

Example 5 with FutureStream

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

the class LazySeqAgronaTest method shouldZipInfiniteWithFiniteSeq.

@Test
public void shouldZipInfiniteWithFiniteSeq() throws Exception {
    ThreadPools.setUseCommon(false);
    // <-- MEMORY LEAK!- no auto-closing yet, so writes infinetely to it's async queue
    final FutureStream<Integer> units = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(1, n -> n + 1);
    final ReactiveSeq<Integer> hundreds = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(100, n -> n + 100).limit(5);
    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) Matchers.hasItems(org.hamcrest.Matchers.hasItems) Supplier(java.util.function.Supplier) 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) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) 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) LazyReact(cyclops.futurestream.LazyReact) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

Aggregations

FutureStream (cyclops.futurestream.FutureStream)31 Test (org.junit.Test)27 Tuple2 (cyclops.data.tuple.Tuple2)19 Stream (java.util.stream.Stream)17 BaseSeqTest (cyclops.futurestream.react.base.BaseSeqTest)16 LazyReact (cyclops.futurestream.LazyReact)12 Queue (com.oath.cyclops.async.adapters.Queue)11 ReactiveSeq (cyclops.reactive.ReactiveSeq)11 Collectors (java.util.stream.Collectors)11 QueueFactories (com.oath.cyclops.async.QueueFactories)8 Collection (java.util.Collection)8 Signal (com.oath.cyclops.async.adapters.Signal)7 ThreadPools (com.oath.cyclops.react.ThreadPools)7 Tuple.tuple (cyclops.data.tuple.Tuple.tuple)7 Serializable (java.io.Serializable)7 Arrays.asList (java.util.Arrays.asList)7 List (java.util.List)6 ForkJoinPool (java.util.concurrent.ForkJoinPool)6 Matchers.is (org.hamcrest.Matchers.is)6 Ignore (org.junit.Ignore)6