Search in sources :

Example 1 with BaseSimpleReactStream

use of com.oath.cyclops.types.futurestream.BaseSimpleReactStream in project cyclops by aol.

the class SimpleReactTest method testLargeChain.

@Test
public void testLargeChain() {
    BaseSimpleReactStream builder = new SimpleReact().ofAsync(() -> "Hello", () -> "World");
    for (int i = 0; i < 1000; i++) {
        builder = builder.then(input -> input + " " + counter++);
    }
    List<String> results = builder.block();
    assertThat(results.get(0).length(), greaterThan(100));
}
Also used : BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Arrays(java.util.Arrays) LazyReact(cyclops.futurestream.LazyReact) Matchers(org.mockito.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Matchers.any(org.mockito.Matchers.any) ExecutionException(java.util.concurrent.ExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) ForkJoinPool(java.util.concurrent.ForkJoinPool) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with BaseSimpleReactStream

use of com.oath.cyclops.types.futurestream.BaseSimpleReactStream in project cyclops by aol.

the class SimpleReactTest method asyncTest.

@Test
public void asyncTest() {
    BaseSimpleReactStream stream = BaseSimpleReactStream.of(1, 2, 3, 4).async();
    assertThat(stream.isAsync(), is(true));
}
Also used : BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Test(org.junit.Test)

Example 3 with BaseSimpleReactStream

use of com.oath.cyclops.types.futurestream.BaseSimpleReactStream in project cyclops by aol.

the class SimpleReactTest method syncTest.

@Test
public void syncTest() {
    BaseSimpleReactStream stream = BaseSimpleReactStream.of(1, 2, 3, 4).sync();
    assertThat(stream.isAsync(), is(false));
}
Also used : BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Test(org.junit.Test)

Example 4 with BaseSimpleReactStream

use of com.oath.cyclops.types.futurestream.BaseSimpleReactStream 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 5 with BaseSimpleReactStream

use of com.oath.cyclops.types.futurestream.BaseSimpleReactStream in project cyclops by aol.

the class CompletableFutureTest method asyncEventRecieverTest.

@Test
public void asyncEventRecieverTest() throws InterruptedException, ExecutionException {
    Queue<CompletableFuture<Integer>> queue = buildQueueOfAsyncEvents();
    BaseSimpleReactStream<String> convertedToStrings = new SimpleReact().fromStream(queue.stream()).<String>then(it -> it + "*");
    convertedToStrings.streamCompletableFutures().forEach(f -> assertFalse(f.isDone()));
    new SimpleReact(new ForkJoinPool(3)).ofAsync(() -> 100, () -> 200, () -> 400).then(it -> sleep(it)).then(it -> queue.poll().complete(it));
    List<String> result = convertedToStrings.block();
    assertThat(result.size(), is(3));
    assertThat(result, hasItem("400*"));
}
Also used : Arrays(java.util.Arrays) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleReact(cyclops.futurestream.SimpleReact) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Aggregations

BaseSimpleReactStream (com.oath.cyclops.types.futurestream.BaseSimpleReactStream)9 Test (org.junit.Test)9 SimpleReact (cyclops.futurestream.SimpleReact)4 Arrays (java.util.Arrays)4 List (java.util.List)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ExecutionException (java.util.concurrent.ExecutionException)4 ForkJoinPool (java.util.concurrent.ForkJoinPool)4 Matchers.hasItem (org.hamcrest.Matchers.hasItem)4 Matchers.is (org.hamcrest.Matchers.is)4 Assert.assertThat (org.junit.Assert.assertThat)4 LazyReact (cyclops.futurestream.LazyReact)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Set (java.util.Set)2