Search in sources :

Example 6 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class TopicTest method disconnectAllStreams.

@Test
public void disconnectAllStreams() {
    Topic<Integer> topic = new Topic<>();
    ReactiveSeq s1 = topic.stream();
    // 3 Queues
    ReactiveSeq s2 = topic.stream();
    topic.disconnect(s1);
    topic.disconnect(s2);
    assertThat(topic.getDistributor().getSubscribers().size(), is(0));
    assertThat(topic.getStreamToQueue().size(), is(0));
}
Also used : ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 7 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq 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)

Example 8 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class SimpleReactStream method duplicate.

/**
 * Duplicate a Stream into two equivalent LazyFutureStreams, underlying
 * Stream of Futures is duplicated
 * <pre>
 * {@code
 * EagerFutureStream.of(1, 2, 3).duplicate()
 *
 * results in
 *
 * tuple((1,2,3),(1,2,3))
 * }</pre>
 * Care should be taken not to use this method with infinite streams!
 *
 * @return Two equivalent Streams
 *
 * @see #duplicate()
 */
default Tuple2<SimpleReactStream<U>, SimpleReactStream<U>> duplicate() {
    // unblocking impl
    final Stream stream = getLastActive().stream();
    final Tuple2<ReactiveSeq<CompletableFuture<U>>, ReactiveSeq<CompletableFuture<U>>> duplicated = ReactiveSeq.fromStream((Stream<CompletableFuture<U>>) stream).duplicate();
    final Tuple2 dup = Tuple.tuple(fromStreamOfFutures(duplicated._1()), fromStreamOfFutures(duplicated._2()));
    return dup;
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) FutureStream(cyclops.futurestream.FutureStream) Stream(java.util.stream.Stream) ReactiveSeq(cyclops.reactive.ReactiveSeq)

Example 9 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class BaseSeqTest method testPartition100.

@Test
public void testPartition100() {
    for (int i = 0; i < 0; i++) {
        Supplier<ReactiveSeq<Tuple2<Integer, Integer>>> s = () -> of(tuple(1, 0), tuple(2, 1), tuple(3, 2), tuple(4, 3), tuple(5, 4), tuple(6, 5));
        Tuple2<ReactiveSeq<Tuple2<Integer, Integer>>, ReactiveSeq<Tuple2<Integer, Integer>>> tuple = s.get().partition(t -> t._2() < 3);
        Tuple2<ReactiveSeq<Integer>, ReactiveSeq<Integer>> t2 = tuple.transform((v1, v2) -> Tuple.<ReactiveSeq<Integer>, ReactiveSeq<Integer>>tuple(v1.map(t -> t._1()), v2.map(t -> t._1())));
        List l1 = t2._1().toList();
        System.out.println("l1:" + l1);
        List l2 = t2._2().toList();
        System.out.println("l2:" + l2);
        assertThat(l1.size(), is(3));
        assertThat(l2.size(), is(3));
    }
}
Also used : ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) LinkedList(java.util.LinkedList) List(java.util.List) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 10 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class ReactiveStreamX method multicast.

public Seq<ReactiveSeq<T>> multicast(int num) {
    if (this.async == Type.NO_BACKPRESSURE) {
        ConcurrentLinkedQueue<Subscriber> subs = new ConcurrentLinkedQueue<>();
        Seq<ReactiveSeq<T>> result = Seq.empty();
        for (int i = 0; i < num; i++) {
            ReactiveSeq<T> seq = Spouts.<T>async(s1 -> {
                subs.add(s1.asSubscriber());
                if (subs.size() == num) {
                    this.forEach(e -> subs.forEach(s -> s.onNext(e)), ex -> subs.forEach(s -> s.onError(ex)), () -> subs.forEach(s -> s.onComplete()));
                }
            });
            result = result.plus(seq);
        }
        return result;
    }
    if (this.async == BACKPRESSURE) {
        ConcurrentLinkedQueue<Subscriber> subs = new ConcurrentLinkedQueue<>();
        Seq<ReactiveSeq<T>> result = Seq.empty();
        Subscription sub = forEachSubscribe(e -> subs.forEach(s -> s.onNext(e)), ex -> subs.forEach(s -> s.onError(ex)), () -> subs.forEach(s -> s.onComplete()));
        for (int i = 0; i < num; i++) {
            ReactiveSeq<T> seq = new ReactiveStreamX<T>(new PublisherToOperator<T>(new Publisher<T>() {

                @Override
                public void subscribe(Subscriber<? super T> s) {
                    subs.add(s);
                    s.onSubscribe(sub);
                }
            }));
            result = result.plus(seq);
        }
        return result;
    }
    return Streams.toBufferingCopier(() -> iterator(), num, () -> new ArrayDeque<T>(100)).map(ReactiveSeq::fromIterable);
}
Also used : Tuple4(cyclops.data.tuple.Tuple4) Tuple3(cyclops.data.tuple.Tuple3) Connectable(com.oath.cyclops.types.stream.Connectable) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) Continuation(com.oath.cyclops.types.futurestream.Continuation) PersistentCollection(com.oath.cyclops.types.persistent.PersistentCollection) QueueFactories(com.oath.cyclops.async.QueueFactories) Getter(lombok.Getter) Topic(com.oath.cyclops.async.adapters.Topic) Wither(lombok.experimental.Wither) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) com.oath.cyclops.internal.stream.spliterators.push(com.oath.cyclops.internal.stream.spliterators.push) Future(cyclops.control.Future) Vector(cyclops.data.Vector) Seq(cyclops.data.Seq) Tuple(cyclops.data.tuple.Tuple) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamSupport(java.util.stream.StreamSupport) Collector(java.util.stream.Collector) Subscriber(org.reactivestreams.Subscriber) cyclops.control(cyclops.control) Spouts(cyclops.reactive.Spouts) Zipping3Spliterator(com.oath.cyclops.internal.stream.spliterators.Zipping3Spliterator) Streams(cyclops.companion.Streams) Publisher(org.reactivestreams.Publisher) OneToOneConcurrentArrayQueue(org.agrona.concurrent.OneToOneConcurrentArrayQueue) Monoid(cyclops.function.Monoid) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) ReactiveSeq(cyclops.reactive.ReactiveSeq) Queue(com.oath.cyclops.async.adapters.Queue) Stream(java.util.stream.Stream) Function3(cyclops.function.Function3) Function4(cyclops.function.Function4) Subscription(org.reactivestreams.Subscription) IterableX(com.oath.cyclops.types.traversable.IterableX) SYNC(com.oath.cyclops.internal.stream.ReactiveStreamX.Type.SYNC) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) BACKPRESSURE(com.oath.cyclops.internal.stream.ReactiveStreamX.Type.BACKPRESSURE) QueueFactory(com.oath.cyclops.async.adapters.QueueFactory) java.util.function(java.util.function) Signal(com.oath.cyclops.async.adapters.Signal) AllArgsConstructor(lombok.AllArgsConstructor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Publisher(org.reactivestreams.Publisher) ReactiveSeq(cyclops.reactive.ReactiveSeq) Subscriber(org.reactivestreams.Subscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Subscription(org.reactivestreams.Subscription)

Aggregations

ReactiveSeq (cyclops.reactive.ReactiveSeq)101 Test (org.junit.Test)91 Tuple3 (cyclops.data.tuple.Tuple3)33 Tuple4 (cyclops.data.tuple.Tuple4)33 Tuple2 (cyclops.data.tuple.Tuple2)31 FlowableReactiveSeq (cyclops.reactive.FlowableReactiveSeq)19 FluxReactiveSeq (cyclops.reactive.FluxReactiveSeq)18 Stream (java.util.stream.Stream)17 Collectors (java.util.stream.Collectors)16 FutureStream (cyclops.futurestream.FutureStream)12 QueueFactories (com.oath.cyclops.async.QueueFactories)11 Queue (com.oath.cyclops.async.adapters.Queue)11 LazyReact (cyclops.futurestream.LazyReact)11 Supplier (java.util.function.Supplier)10 Signal (com.oath.cyclops.async.adapters.Signal)9 List (java.util.List)9 Spouts (cyclops.reactive.Spouts)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Matchers.equalTo (org.hamcrest.Matchers.equalTo)8 Assert.assertThat (org.junit.Assert.assertThat)8