Search in sources :

Example 51 with LazyReact

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

the class LazySeqAgronaTest 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) 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)

Example 52 with LazyReact

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

the class LazyToQueue method toQueue.

/*
     * Populate provided queues with the sharded data from this Stream.
     *
     *	@param shards Map of key to Queue shards
     *	@param sharder Sharding function, element to key converter
     * @see com.oath.cyclops.react.stream.traits.ToQueue#toQueue(java.util.Map, java.util.function.Function)
     */
@Override
default <K> void toQueue(final Map<K, Queue<U>> shards, final Function<? super U, ? extends K> sharder) {
    // in this case all the items have to be pushed to the shards,
    // we can't rely on the client pulling them all to getValue them in to the right shards
    final LazyReact service = getPopulator();
    then(it -> shards.get(sharder.apply(it)).offer(it), service.getExecutor()).runThread(() -> {
        shards.values().forEach(it -> it.close());
        returnPopulator(service);
    });
}
Also used : Consumer(java.util.function.Consumer) Queue(com.oath.cyclops.async.adapters.Queue) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException) Executor(java.util.concurrent.Executor) LazyReact(cyclops.futurestream.LazyReact) Map(java.util.Map) FutureStream(cyclops.futurestream.FutureStream) CompletedException(com.oath.cyclops.internal.react.async.future.CompletedException) Function(java.util.function.Function) LazyReact(cyclops.futurestream.LazyReact)

Example 53 with LazyReact

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

the class PushableStreamTest method testLazyFutureStream.

@Test
public void testLazyFutureStream() {
    PushableFutureStream<Integer> pushable = StreamSource.ofUnbounded().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 54 with LazyReact

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

the class PushableStreamTest method testLazyFutureStreamAdapter.

@Test
public void testLazyFutureStreamAdapter() {
    Signal<Integer> signal = Signal.queueBackedSignal();
    FutureStream<Integer> pushable = StreamSource.futureStream(signal.getDiscrete(), new LazyReact());
    signal.set(100);
    signal.close();
    assertThat(pushable.collect(Collectors.toList()), hasItem(100));
}
Also used : LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 55 with LazyReact

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

the class PushableStreamTest method testMultiple.

@Test
public void testMultiple() {
    MultipleStreamSource<Integer> multi = StreamSource.ofMultiple();
    FutureStream<Integer> pushable = multi.futureStream(new LazyReact());
    ReactiveSeq<Integer> seq = multi.reactiveSeq();
    Stream<Integer> stream = multi.stream();
    multi.getInput().offer(100);
    multi.getInput().close();
    Set<Integer> vals = new TreeSet<>();
    pushable.forEach(vals::add);
    seq.forEach(vals::add);
    stream.forEach(vals::add);
    assertThat(Sets.newSet(100), is(vals));
}
Also used : LazyReact(cyclops.futurestream.LazyReact) TreeSet(java.util.TreeSet) 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