Search in sources :

Example 16 with SimpleReact

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

the class SignalTest method signalDiscrete3.

@Test
public void signalDiscrete3() {
    try {
        Signal<Integer> q = Signal.queueBackedSignal();
        new SimpleReact().ofAsync(() -> q.set(1), () -> q.set(2), () -> {
            sleep(20);
            return q.set(4);
        }, () -> {
            sleep(400);
            q.getDiscrete().close();
            return 1;
        });
        parallel().fromStream(q.getDiscrete().streamCompletableFutures()).then(it -> "*" + it).peek(it -> incrementFound()).peek(it -> System.out.println(it)).block();
    } finally {
        assertThat(found, is(3));
    }
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) Stream(java.util.stream.Stream) BaseSimpleReactStream.parallel(com.oath.cyclops.types.futurestream.BaseSimpleReactStream.parallel) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) Test(org.junit.Test) Before(org.junit.Before) SimpleReact(cyclops.futurestream.SimpleReact) Test(org.junit.Test)

Example 17 with SimpleReact

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

the class TopicTest method multipleSubscribersGetSameMessagesSimpleReact.

@Test
public void multipleSubscribersGetSameMessagesSimpleReact() throws InterruptedException, ExecutionException {
    Topic<String> topic = new Topic<>(new Queue<>());
    Stream<String> input = Stream.of("hello", "world");
    // read from the topic concurrently in 2 threads
    BaseSimpleReactStream<Collection<String>> stage = new SimpleReact(new ForkJoinPool(2)).ofAsync(() -> parallel().fromStream(topic.stream()).then(it -> it + "*").block(), () -> parallel().fromStream(topic.stream()).then(it -> it + "!").peek(// make sure takes slightly longer to complete
    it -> sleep(10)).block(Collectors.toSet()));
    // make sure streams are set up
    sleep(50);
    topic.fromStream(input);
    // wait until Topic has been read from
    sleep(400);
    topic.close();
    List<Collection<String>> result = stage.block();
    assertThat(result.get(0), instanceOf(List.class));
    assertThat(result.get(0), hasItem("hello*"));
    assertThat(result.get(0), hasItem("world*"));
    assertThat(result.get(1), instanceOf(HashSet.class));
    assertThat(result.get(1), hasItem("hello!"));
    assertThat(result.get(1), hasItem("world!"));
}
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) SimpleReact(cyclops.futurestream.SimpleReact) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ForkJoinPool(java.util.concurrent.ForkJoinPool) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with SimpleReact

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

the class TopicTest method mergingAndSplittingSimpleReact.

// too non-deterministic to run regularly - relying on population from competing threads
@Test
// too non-deterministic to run regularly - relying on population from competing threads
@Ignore
public void mergingAndSplittingSimpleReact() {
    Topic<Integer> topic = new Topic<>();
    BaseSimpleReactStream<Collection<String>> stage = new SimpleReact(new ForkJoinPool(2)).ofAsync(() -> parallel().fromStream(topic.streamCompletableFutures()).then(it -> it + "*").block(Collectors.toList()), () -> parallel().fromStream(topic.streamCompletableFutures()).then(it -> it + "!").block(Collectors.toSet()));
    // make sure streams are set up
    sleep(50);
    new SimpleReact(new ForkJoinPool(1)).ofAsync(() -> topic.fromStream(Stream.generate(() -> count++)));
    new SimpleReact(new ForkJoinPool(1)).ofAsync(() -> topic.fromStream(Stream.generate(() -> count1++)));
    // wait until Topic has been read from
    sleep(40);
    System.out.println("Closing!");
    topic.close();
    System.out.println("Closed! Blocking..");
    List<Collection<String>> result = stage.block();
    System.out.println("Completed " + result.size());
    assertThat(extract1(result), hasItem("0*"));
    assertThat(extract1(result), hasItem("100000*"));
    assertThat(extract2(result), hasItem("0!"));
    assertThat(extract2(result), hasItem("100000!"));
}
Also used : SimpleReact(cyclops.futurestream.SimpleReact) Collection(java.util.Collection) ForkJoinPool(java.util.concurrent.ForkJoinPool) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with SimpleReact

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

the class TopicTest method mergingAndSplitting.

// too non-deterministic to run regularly - relying on population from competing threads
@Test
// too non-deterministic to run regularly - relying on population from competing threads
@Ignore
public void mergingAndSplitting() {
    Topic<Integer> topic = new Topic<>();
    Stream<Integer> stream1 = topic.stream();
    Stream<Integer> stream2 = topic.stream();
    new SimpleReact().ofAsync(() -> topic.fromStream(Stream.generate(() -> count++)));
    new SimpleReact().ofAsync(() -> topic.fromStream(Stream.generate(() -> count1++)));
    for (Stream<Integer> stream : Arrays.asList(stream1, stream2)) {
        List<Integer> result = stream.limit(1000).peek(it -> System.out.println(it)).collect(Collectors.toList());
        assertThat(result, hasItem(100000));
        assertThat(result, hasItem(0));
    }
}
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) SimpleReact(cyclops.futurestream.SimpleReact) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with SimpleReact

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

the class QueueTest method mergingTestEagerStreamMerge.

@Test
@Ignore
public // competing threads
void mergingTestEagerStreamMerge() {
    count = 0;
    count1 = 100000;
    Queue<Integer> q = new Queue(new LinkedBlockingQueue());
    new SimpleReact().ofAsync(() -> q.fromStream(Stream.generate(() -> count++)));
    new SimpleReact().ofAsync(() -> q.fromStream(Stream.generate(() -> count1++)));
    List<Integer> result = q.stream().limit(1000).peek(it -> System.out.println(it)).collect(Collectors.toList());
    assertThat(result, hasItem(100000));
    assertThat(result, hasItem(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Matchers.not(org.hamcrest.Matchers.not) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Before(org.junit.Before) ReactiveSeq.of(cyclops.reactive.ReactiveSeq.of) Collection(java.util.Collection) BaseSimpleReactStream.parallel(com.oath.cyclops.types.futurestream.BaseSimpleReactStream.parallel) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) SimpleReact(cyclops.futurestream.SimpleReact) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SimpleReact (cyclops.futurestream.SimpleReact)71 Test (org.junit.Test)70 Assert.assertThat (org.junit.Assert.assertThat)54 Matchers.is (org.hamcrest.Matchers.is)52 List (java.util.List)50 ExecutionException (java.util.concurrent.ExecutionException)46 Collectors (java.util.stream.Collectors)36 Set (java.util.Set)33 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)31 Matchers.hasItem (org.hamcrest.Matchers.hasItem)30 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)25 Arrays (java.util.Arrays)24 CompletableFuture (java.util.concurrent.CompletableFuture)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Stream (java.util.stream.Stream)21 LazyReact (cyclops.futurestream.LazyReact)20 BaseSimpleReactStream (com.oath.cyclops.types.futurestream.BaseSimpleReactStream)17 ArrayList (java.util.ArrayList)16 ForkJoinPool (java.util.concurrent.ForkJoinPool)16 Ignore (org.junit.Ignore)14