Search in sources :

Example 31 with SimpleReact

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

the class AllOfTest method testAllOfParallelStreamsSameForkJoinPool.

@Test
public void testAllOfParallelStreamsSameForkJoinPool() throws InterruptedException, ExecutionException {
    Set<String> threadGroup = Collections.synchronizedSet(new TreeSet());
    Integer result = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3, () -> 5).<Integer>then(it -> {
        threadGroup.add(Thread.currentThread().getThreadGroup().getName());
        return it * 200;
    }).then((Integer it) -> {
        if (it == 1000)
            throw new RuntimeException("boo!");
        return it;
    }).onFail(e -> 100).allOf(it -> {
        return it.parallelStream().filter(f -> f > 300).map(m -> {
            threadGroup.add(Thread.currentThread().getThreadGroup().getName());
            return m - 5;
        }).reduce(0, (acc, next) -> acc + next);
    }).block(Collectors.reducing(0, (acc, next) -> next));
    assertThat(threadGroup.size(), is(1));
}
Also used : Arrays(java.util.Arrays) HashMap(cyclops.data.HashMap) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) SimpleReact(cyclops.futurestream.SimpleReact) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 32 with SimpleReact

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

the class AllOfTest method testAllOfCompletableOnFail.

@Test
public void testAllOfCompletableOnFail() {
    List<String> urls = Arrays.asList("hello", "world", "2");
    List<String> result = new SimpleReact().fromStream(urls.stream().<CompletableFuture<String>>map(it -> handle(it))).onFail(it -> "hello").capture(e -> e.printStackTrace()).peek(it -> System.out.println(it)).allOf((List<String> data) -> {
        System.out.println(data);
        return data;
    }).block().firstValue(null);
    assertThat(result.size(), is(3));
}
Also used : Arrays(java.util.Arrays) HashMap(cyclops.data.HashMap) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) SimpleReact(cyclops.futurestream.SimpleReact) List(java.util.List) Test(org.junit.Test)

Example 33 with SimpleReact

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

the class AllOfTest method testAllOf.

@Test
public void testAllOf() throws InterruptedException, ExecutionException {
    boolean[] blocked = { false };
    new SimpleReact().<Integer>ofAsync(() -> 1).then(it -> {
        try {
            Thread.sleep(10);
        } catch (Exception e) {
        }
        blocked[0] = true;
        return 10;
    }).allOf(it -> it.size());
    assertThat(blocked[0], is(false));
}
Also used : Arrays(java.util.Arrays) HashMap(cyclops.data.HashMap) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) SimpleReact(cyclops.futurestream.SimpleReact) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 34 with SimpleReact

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

the class AllOfTest method testAllOfParallelStreamsSkip.

@Test
public void testAllOfParallelStreamsSkip() throws InterruptedException, ExecutionException {
    List<Integer> result = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3, () -> 5).<Integer>then(it -> {
        return it * 200;
    }).then((Integer it) -> {
        if (it == 1000)
            throw new RuntimeException("boo!");
        return it;
    }).onFail(e -> 100).allOf(it -> {
        return it.parallelStream().skip(1).limit(3).collect(Collectors.toList());
    }).block().firstValue(null);
    assertThat(result.size(), is(3));
}
Also used : Arrays(java.util.Arrays) HashMap(cyclops.data.HashMap) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) SimpleReact(cyclops.futurestream.SimpleReact) Test(org.junit.Test)

Example 35 with SimpleReact

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

the class BlockingTest method testBreakoutAllCompleted.

@Test
public void testBreakoutAllCompleted() throws InterruptedException, ExecutionException {
    count = new AtomicInteger(0);
    List<Integer> results = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).then(it -> it * 100).then(it -> {
        if (it == 100)
            throw new RuntimeException("boo!");
        else
            sleep(it);
        return it;
    }).capture(e -> count.incrementAndGet()).block(status -> status.getAllCompleted() > 0);
    assertThat(results.size(), is(0));
    assertThat(count.get(), is(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) SimpleReactStream(com.oath.cyclops.types.futurestream.SimpleReactStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleReact(cyclops.futurestream.SimpleReact) Set(java.util.Set) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SimpleReact(cyclops.futurestream.SimpleReact) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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