Search in sources :

Example 66 with SimpleReact

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

the class SimpleReactTest method testSeparatedChains.

@Test
public void testSeparatedChains() {
    // .split(2);
    BaseSimpleReactStream<String> orgBuilder = new SimpleReact().ofAsync(() -> "Hello", () -> "World");
    BaseSimpleReactStream builder = orgBuilder;
    for (int i = 0; i < 1000; i++) {
        builder = builder.then(input -> input + " " + counter++);
    }
    List<String> results = orgBuilder.block();
    assertThat(results.get(0), is("Hello"));
    List<String> completeResults = builder.block();
    assertThat(completeResults.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 67 with SimpleReact

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

the class SimpleReactTest method testBlockInterruption.

@Test
public void testBlockInterruption() {
    final AtomicBoolean isRunning = new AtomicBoolean(true);
    final CountDownLatch startBarier = new CountDownLatch(1);
    final BaseSimpleReactStream<Integer> stage = new SimpleReact().<Integer>ofAsync(() -> 1, () -> 2, () -> 3).then((it) -> {
        try {
            Thread.sleep(it * 5000);
        } catch (InterruptedException e) {
            System.err.println("InterruptedException");
            Thread.currentThread().interrupt();
        }
        return it * 100;
    });
    Thread t = new Thread(() -> {
        while (isRunning.get()) {
            // worker thread termination condition
            startBarier.countDown();
            try {
                while (true) {
                    // random condition
                    stage.block();
                // Thread.sleep(2 * 5000);
                }
            } catch (Exception e) {
                System.err.println("InterruptedException " + e.getMessage());
            }
        }
    });
    t.start();
    try {
        startBarier.await();
        isRunning.getAndSet(false);
        t.interrupt();
        t.join();
    } catch (InterruptedException e) {
    // you know I don't care
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpleReact(cyclops.futurestream.SimpleReact) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 68 with SimpleReact

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

the class SimpleReactTest method doOnEach.

@Test
public void doOnEach() {
    String[] found = { "" };
    String res = new SimpleReact().ofAsync(() -> "hello").peek(it -> {
        found[0] = it;
    }).then(it -> it + "!").block().firstValue(null);
    assertThat(found[0], is("hello"));
    assertThat(res, is("hello!"));
}
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 69 with SimpleReact

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

the class SimpleReactTest method testThenNull.

@Test
public void testThenNull() {
    List<String> result = new SimpleReact().ofAsync(() -> "World", () -> "Hello").then(in -> (String) null).block();
    assertThat(result.size(), is(2));
    assertThat(result.get(0), is(nullValue()));
}
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 70 with SimpleReact

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

the class ReactPoolTest method testSyncrhonous.

@Test
public void testSyncrhonous() {
    ReactPool<SimpleReact> pool = ReactPool.syncrhonousPool();
    new SimpleReact().ofAsync(() -> populate(pool));
    List<String> result = pool.react((sr) -> sr.ofAsync(() -> "hello", () -> "world").peek(System.out::println).block());
    assertThat(result.size(), is(2));
}
Also used : 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