Search in sources :

Example 26 with SimpleReact

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

the class ResultCollectionTest method testBreakoutAllCompleted.

@Test
public void testBreakoutAllCompleted() throws InterruptedException, ExecutionException {
    count = 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++).block(status -> status.getAllCompleted() > 0);
    assertThat(results.size(), is(0));
    assertThat(count, is(1));
}
Also used : Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) 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) Test(org.junit.Test)

Example 27 with SimpleReact

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

the class ResultCollectionTest method testBreakoutException.

@Test
public void testBreakoutException() throws InterruptedException, ExecutionException {
    Throwable[] error = { null };
    List<String> strings = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).then(it -> it * 100).<String>then(it -> {
        throw new RuntimeException("boo!");
    }).capture(e -> error[0] = e).block(status -> status.getCompleted() >= 1);
    assertThat(strings.size(), is(0));
    assertThat(error[0], instanceOf(RuntimeException.class));
}
Also used : Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) 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) Test(org.junit.Test)

Example 28 with SimpleReact

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

the class AlgorithmCompareTest method testFastest.

@Test
public void testFastest() throws InterruptedException, ExecutionException {
    ArrayList<Integer> arrayList = new ArrayList<>();
    LinkedList<Integer> linkedList = new LinkedList<>();
    for (int i = 0; i < 1001; i++) {
        arrayList.add(i);
        linkedList.add(i);
    }
    SimpleTimer timer = new SimpleTimer();
    Result result = new SimpleReact().<Result>ofAsync(() -> Result.builder().name("approach1").result(retrieval(arrayList)).build(), () -> Result.builder().name("approach2").result(retrieval(linkedList)).build()).then(it -> it.withTime(timer.getElapsedNanoseconds())).filter(it -> it.getResult() == 1000).block().firstValue(null);
    assertThat(result.getName(), is("approach1"));
}
Also used : Getter(lombok.Getter) Wither(lombok.experimental.Wither) Test(org.junit.Test) SimpleTimer(com.oath.cyclops.util.SimpleTimer) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Builder(lombok.experimental.Builder) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) LinkedList(java.util.LinkedList) AllArgsConstructor(lombok.AllArgsConstructor) SimpleTimer(com.oath.cyclops.util.SimpleTimer) SimpleReact(cyclops.futurestream.SimpleReact) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 29 with SimpleReact

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

the class BlockingTest method testFirstAllOf.

@Test
public void testFirstAllOf() throws InterruptedException, ExecutionException {
    Set<Integer> result = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3, () -> 5).then(it -> it * 100).<Set<Integer>, Set<Integer>>allOf(Collectors.toSet(), it -> {
        assertThat(it, is(Set.class));
        return it;
    }).block().firstValue(null);
    assertThat(result.size(), is(4));
}
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) Set(java.util.Set) SimpleReact(cyclops.futurestream.SimpleReact) Test(org.junit.Test)

Example 30 with SimpleReact

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

the class FilterTest method testFilterExceptions.

@Test
public void testFilterExceptions() throws InterruptedException, ExecutionException {
    List<String> result = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).filter(it -> 1 != it).<String>then(it -> "*" + it).capture(e -> fail("No exception should be captured")).block();
    assertThat(result.size(), is(2));
    assertThat(result, not(hasItem("*1")));
}
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) Matchers.not(org.hamcrest.Matchers.not) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SimpleReact(cyclops.futurestream.SimpleReact) 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