Search in sources :

Example 51 with SimpleReact

use of cyclops.futurestream.SimpleReact 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)

Example 52 with SimpleReact

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

the class SignalTest method signalDiscrete1.

@Test
public void signalDiscrete1() {
    for (int i = 0; i < 100; i++) {
        resetFound();
        try {
            Signal<Integer> q = Signal.queueBackedSignal();
            new SimpleReact().ofAsync(() -> q.set(1), () -> q.set(1), () -> {
                sleep(200);
                return q.set(1);
            }, () -> {
                sleep(40);
                q.close();
                return 1;
            });
            parallel().fromStreamOfFutures(q.getDiscrete().streamCompletableFutures()).then(it -> "*" + it).peek(it -> incrementFound()).peek(it -> System.out.println(it)).block();
        } finally {
            assertThat(found, is(1));
        }
    }
}
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 53 with SimpleReact

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

the class SignalTest method signalContinuous3.

@Test
public void signalContinuous3() {
    for (int i = 0; i < 10; i++) {
        System.out.println(i);
        resetFound();
        try {
            Signal<Integer> q = Signal.queueBackedSignal();
            new SimpleReact().ofAsync(() -> q.set(1), () -> q.set(1), () -> {
                sleep(1);
                return q.set(1);
            }, () -> {
                sleep(150);
                q.close();
                return 1;
            });
            parallel().fromStream(q.getContinuous().streamCompletableFutures()).then(it -> "*" + it).peek(it -> incrementFound()).peek(it -> System.out.println(it)).block();
        } finally {
            assertThat(getFound(), 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 54 with SimpleReact

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

the class TopicTest method simpleMergingAndSplittingSimpleReact.

@Test
public void simpleMergingAndSplittingSimpleReact() {
    Topic<Integer> topic = new Topic<>();
    BaseSimpleReactStream<Collection<String>> stage = new SimpleReact(new ForkJoinPool(2)).ofAsync(() -> parallel().fromStream(topic.stream()).then(it -> it + "*").block(Collectors.toList()), () -> parallel().fromStream(topic.stream()).then(it -> it + "!").block(Collectors.toSet()));
    // make sure streams are set up
    sleep(50);
    topic.offer(count);
    topic.offer(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) Test(org.junit.Test)

Example 55 with SimpleReact

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

the class AnyOfTest method testAnyOf.

@Test
public void testAnyOf() throws InterruptedException, ExecutionException {
    boolean[] blocked = { false };
    new SimpleReact().<Integer>ofAsync(() -> 1).then(it -> {
        try {
            Thread.sleep(50);
        } catch (Exception e) {
        }
        blocked[0] = true;
        return 10;
    }).anyOf(it -> it);
    assertThat(blocked[0], is(false));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) Arrays(java.util.Arrays) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Ignore(org.junit.Ignore) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) SimpleReact(cyclops.futurestream.SimpleReact) ExecutionException(java.util.concurrent.ExecutionException) 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