Search in sources :

Example 6 with SimpleReact

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

the class SimpleReactTest method testReactExceptionRecovery.

@Test
public void testReactExceptionRecovery() {
    List<String> result = new SimpleReact().ofAsync(() -> {
        throw new RuntimeException();
    }, () -> "Hello").onFail(e -> {
        System.out.println(e);
        return "World";
    }).block();
    assertThat(result.size(), is(2));
}
Also used : 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 7 with SimpleReact

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

the class StreamTest method testStreamOf.

@Test
public void testStreamOf() throws InterruptedException, ExecutionException {
    Stream<CompletableFuture<String>> stream = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).then(it -> "*" + it).streamCompletableFutures();
    List<String> strings = new SimpleReact().<String>fromStream(stream).then(it -> it + "*").block();
    assertThat(strings.size(), is(3));
    assertThat(strings, hasItem("*1*"));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleReact(cyclops.futurestream.SimpleReact) Test(org.junit.Test)

Example 8 with SimpleReact

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

the class StreamTest method testStreamFrom.

@Test
public void testStreamFrom() throws InterruptedException, ExecutionException {
    List<String> strings = new SimpleReact().<String>fromStream(new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).with(it -> "*" + it).stream()).then(it -> it + "*").block();
    assertThat(strings.size(), is(3));
    assertThat(strings, hasItem("*1*"));
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Stream(java.util.stream.Stream) SimpleReact(cyclops.futurestream.SimpleReact) Matchers.is(org.hamcrest.Matchers.is) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) SimpleReact(cyclops.futurestream.SimpleReact) Test(org.junit.Test)

Example 9 with SimpleReact

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

the class ReactPoolTest method testUnboundedRoundRobin.

@Test
public void testUnboundedRoundRobin() {
    SimpleReact react1 = Mockito.mock(SimpleReact.class);
    SimpleReact react2 = Mockito.mock(SimpleReact.class);
    SimpleReact react3 = Mockito.mock(SimpleReact.class);
    ReactPool<SimpleReact> pool = ReactPool.unboundedPool(asList(react1, react2));
    pool.populate(react3);
    List<Supplier<String>> suppliers = Arrays.asList(() -> "hello", () -> "world");
    pool.react((er) -> er.fromIterableAsync(suppliers));
    pool.react((er) -> er.fromIterableAsync(suppliers));
    pool.react((er) -> er.fromIterableAsync(suppliers));
    Mockito.verify(react1, Mockito.times(1)).fromIterableAsync(suppliers);
    Mockito.verify(react2, Mockito.times(1)).fromIterableAsync(suppliers);
    Mockito.verify(react3, Mockito.times(1)).fromIterableAsync(suppliers);
}
Also used : SimpleReact(cyclops.futurestream.SimpleReact) Supplier(java.util.function.Supplier) Test(org.junit.Test)

Example 10 with SimpleReact

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

the class AnyOfTest method testAnyOfCompletableOnFail.

@Test
public void testAnyOfCompletableOnFail() {
    List<String> urls = Arrays.asList("hello", "world", "2");
    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)).anyOf(data -> {
        System.out.println(data);
        return data;
    }).block().firstValue(null);
    assertThat(urls, hasItem(result));
}
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) 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