Search in sources :

Example 66 with ReactiveSeq

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

the class BaseSeqTest method testShuffleRandom.

@Test
public void testShuffleRandom() {
    Random r = new Random();
    Supplier<ReactiveSeq<Integer>> s = () -> of(1, 2, 3);
    assertEquals(3, s.get().shuffle(r).toList().size());
    assertThat(s.get().shuffle(r).toList(), hasItems(1, 2, 3));
}
Also used : Random(java.util.Random) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 67 with ReactiveSeq

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

the class TopicTest method disconnectAllStreamsAndReconnect.

@Test
public void disconnectAllStreamsAndReconnect() {
    for (int i = 0; i < 100_000; i++) {
        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(), topic.getDistributor().getSubscribers().size(), is(0));
        assertThat(topic.getStreamToQueue().size(), is(0));
        topic.stream();
        assertThat(topic.getDistributor().getSubscribers().size(), is(1));
        assertThat(topic.getStreamToQueue().size(), is(1));
    }
}
Also used : ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 68 with ReactiveSeq

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

the class TopicTest method disconnectStreams.

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

Example 69 with ReactiveSeq

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

the class Queue method streamGroupedBySizeAndTime.

public ReactiveSeq<Seq<T>> streamGroupedBySizeAndTime(int size, long time, TimeUnit t) {
    long toRun = t.toNanos(time);
    return streamBatch(new Subscription(), source -> {
        return () -> {
            List<T> result = new ArrayList<>();
            long start = System.nanoTime();
            try {
                while (result.size() < size && checkTime(System.nanoTime(), start, toRun)) {
                    try {
                        T next = source.apply(100l, TimeUnit.MICROSECONDS);
                        if (next != null) {
                            result.add(next);
                        }
                    } catch (Queue.QueueTimeoutException e) {
                    }
                }
            } catch (Queue.ClosedQueueException e) {
                if (result.size() > 0) {
                    List list = new ArrayList<>();
                    list.add(result);
                    throw new ClosedQueueException(list);
                }
            }
            return result;
        };
    }).filter(l -> l.size() > 0).map(Seq::fromIterable);
}
Also used : java.util(java.util) Setter(lombok.Setter) Continuation(com.oath.cyclops.types.futurestream.Continuation) QueueFactories(com.oath.cyclops.async.QueueFactories) Getter(lombok.Getter) BiFunction(java.util.function.BiFunction) Wither(lombok.experimental.Wither) CompletableFuture(java.util.concurrent.CompletableFuture) Subscription(com.oath.cyclops.react.async.subscription.Subscription) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Seq(cyclops.data.Seq) AccessLevel(lombok.AccessLevel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamSupport(java.util.stream.StreamSupport) AlwaysContinue(com.oath.cyclops.react.async.subscription.AlwaysContinue) WaitStrategy(com.oath.cyclops.async.wait.WaitStrategy) SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException) DirectWaitStrategy(com.oath.cyclops.async.wait.DirectWaitStrategy) Continueable(com.oath.cyclops.react.async.subscription.Continueable) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) SimpleTimer(com.oath.cyclops.util.SimpleTimer) TimeUnit(java.util.concurrent.TimeUnit) ReactiveSeq(cyclops.reactive.ReactiveSeq) Stream(java.util.stream.Stream) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) AllArgsConstructor(lombok.AllArgsConstructor) Subscription(com.oath.cyclops.react.async.subscription.Subscription) Seq(cyclops.data.Seq) ReactiveSeq(cyclops.reactive.ReactiveSeq)

Example 70 with ReactiveSeq

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

the class AsyncRSZippingTest method testUnzipWithLimits.

@Test
public void testUnzipWithLimits() {
    Supplier<ReactiveSeq<Tuple2<Integer, String>>> s = () -> of(new Tuple2(1, "a"), new Tuple2(2, "b"), new Tuple2(3, "c"));
    Tuple2<ReactiveSeq<Integer>, ReactiveSeq<String>> u1 = ReactiveSeq.unzip(s.get());
    assertTrue(u1._1().limit(2).toList().containsAll(Arrays.asList(1, 2)));
    assertTrue(u1._2().toList().containsAll(asList("a", "b", "c")));
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) FlowableReactiveSeq(cyclops.reactive.FlowableReactiveSeq) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

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