Search in sources :

Example 1 with ReactiveSeq

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

the class SyncZippingTest method testUnzip4WithLimits.

@Test
public void testUnzip4WithLimits() {
    Supplier<ReactiveSeq<Tuple4<Integer, String, Long, Character>>> s = () -> of(new Tuple4(1, "a", 2l, 'z'), new Tuple4(2, "b", 3l, 'y'), new Tuple4(3, "c", 4l, 'x'));
    Tuple4<ReactiveSeq<Integer>, ReactiveSeq<String>, ReactiveSeq<Long>, ReactiveSeq<Character>> u1 = ReactiveSeq.unzip4(s.get());
    assertTrue(u1._1().limit(1).toList().containsAll(Arrays.asList(1)));
    assertTrue(u1._2().limit(2).toList().containsAll(asList("a", "b")));
    assertTrue(u1._3().limit(3).toList().containsAll(asList(2l, 3l, 4l)));
    assertTrue(u1._4().limit(4).toList().containsAll(asList('z', 'y', 'x')));
}
Also used : Tuple4(cyclops.data.tuple.Tuple4) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 2 with ReactiveSeq

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

the class SyncZippingTest method testUnzip3WithLimits.

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

Example 3 with ReactiveSeq

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

the class SyncZippingTest method testUnzip3.

@Test
public void testUnzip3() {
    Supplier<ReactiveSeq<Tuple3<Integer, String, Long>>> s = () -> of(new Tuple3(1, "a", 2l), new Tuple3(2, "b", 3l), new Tuple3(3, "c", 4l));
    Tuple3<ReactiveSeq<Integer>, ReactiveSeq<String>, ReactiveSeq<Long>> u1 = ReactiveSeq.unzip3(s.get());
    assertTrue(u1._1().toList().containsAll(Arrays.asList(1, 2, 3)));
    assertTrue(u1._2().toList().containsAll(asList("a", "b", "c")));
    assertTrue(u1._3().toList().containsAll(asList(2l, 3l, 4l)));
}
Also used : Tuple3(cyclops.data.tuple.Tuple3) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 4 with ReactiveSeq

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

the class FutureStreamImplTest method subcribeOnce.

public void subcribeOnce() {
    AtomicBoolean called1 = new AtomicBoolean(false);
    AtomicBoolean called2 = new AtomicBoolean(false);
    ReactiveSeq<Integer> initialStream = ReactiveSeq.of(1, 2, 3, 4, 5, 6);
    ReactiveSubscriber<Integer> sub1 = Spouts.reactiveSubscriber();
    ReactiveSubscriber<Integer> sub2 = Spouts.reactiveSubscriber();
    FutureStream<Integer> futureStream = FutureStream.builder().fromStream(initialStream).map(v -> v - 1);
    futureStream.subscribe(sub1);
    futureStream.subscribe(sub2);
    CompletableFuture future1 = CompletableFuture.runAsync(() -> sub1.reactiveStream().peek(a -> called1.set(true)).forEach(v -> System.out.println("1 -> " + v)));
    CompletableFuture future2 = CompletableFuture.runAsync(() -> sub2.reactiveStream().peek(a -> called2.set(true)).forEach(v -> System.out.println("2 -> " + v)));
    try {
        future1.get();
        future2.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    assertThat(called1.get(), equalTo(true));
    assertThat(called1.get(), equalTo(false));
}
Also used : Spouts(cyclops.reactive.Spouts) ReactiveSubscriber(com.oath.cyclops.types.reactive.ReactiveSubscriber) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Topic(com.oath.cyclops.async.adapters.Topic) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException) ReactiveSeq(cyclops.reactive.ReactiveSeq) Assert.assertFalse(org.junit.Assert.assertFalse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) FutureStreamImpl(com.oath.cyclops.internal.react.FutureStreamImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with ReactiveSeq

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

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