Search in sources :

Example 11 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class FastFutureTest method testThenApplyAsync.

@Test
public void testThenApplyAsync() {
    try {
        future = future.<String, String>thenApplyAsync(String::toUpperCase, ForkJoinPool.commonPool()).peek(System.out::println).<String, Integer>thenApply(s -> s.length()).thenApply(l -> {
            sizes.add((Integer) l);
            return l;
        }).peek(System.out::println).<Integer, Integer>thenApplyAsync(l -> l + 2, ForkJoinPool.commonPool()).peek(System.out::println).<Integer, Integer>thenApply(l -> {
            sizes2.add(l);
            return l;
        }).<Integer, Integer>thenApplyAsync(l -> l + 2, ForkJoinPool.commonPool()).peek(System.out::println).<Integer, Integer>thenApply(l -> {
            sizes3.add(l);
            return l;
        });
        StringBuilder suffix = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            FastFuture f2 = future.build();
            f2.set("hello world" + suffix.toString());
            f2.join();
            FastFuture f3 = future.build();
            f3.set("hello world2" + suffix.toString());
            f3.join();
            suffix.append("" + i);
        }
        for (int i = 0; i < 11; i++) {
            assertFalse(sizes.contains(i));
        }
        for (int i = 11; i < 201; i++) {
            assertTrue(sizes.contains(i));
        }
        for (int i = 201; i < 211; i++) {
            assertFalse(sizes.contains(i));
        }
        System.out.println(sizes);
        System.out.println(sizes2);
        System.out.println(sizes3);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FinalPipeline(com.oath.cyclops.internal.react.async.future.FinalPipeline) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) PipelineBuilder(com.oath.cyclops.internal.react.async.future.PipelineBuilder) Before(org.junit.Before) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 12 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class FastFutureTest method testAnyOf.

@Test
public void testAnyOf() {
    for (int i = 0; i < TIMES; i++) {
        complete = false;
        FastFuture f1 = new FastFuture(FinalPipeline.empty(), a -> {
        });
        FastFuture f2 = new FastFuture(FinalPipeline.empty(), a -> {
        });
        CountDownLatch race = new CountDownLatch(1);
        FastFuture anyOf = FastFuture.anyOf(f1, f2);
        new Thread(() -> {
            race.countDown();
            f1.set("done");
            f2.set("with");
        }).start();
        try {
            race.await();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        anyOf.join();
    }
}
Also used : FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 13 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class FastFutureTest method onComplete_notCompleted.

@Test
public void onComplete_notCompleted() {
    called = false;
    FastFuture f = future.<Integer, Integer>thenApply(i -> i + 2).build();
    f.onComplete(event -> called = true);
    f.set(10);
    assertTrue(f.isDone());
    assertTrue(called);
}
Also used : FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FinalPipeline(com.oath.cyclops.internal.react.async.future.FinalPipeline) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) PipelineBuilder(com.oath.cyclops.internal.react.async.future.PipelineBuilder) Before(org.junit.Before) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Test(org.junit.Test)

Example 14 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class BatchingCollectorTest method testBuilder.

@Test
public void testBuilder() {
    collector = BatchingCollector.builder().blocking(LazyReact.sequentialBuilder().of(1)).maxActive(new MaxActive(2, 1)).results(new ArrayList<>()).build();
    FastFuture cf = Mockito.mock(FastFuture.class);
    BDDMockito.given(cf.isDone()).willReturn(true);
    for (int i = 0; i < 1000; i++) {
        collector.accept(cf);
    }
    Mockito.verify(cf, Mockito.times(999)).isDone();
}
Also used : FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Test(org.junit.Test)

Example 15 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class BatchingCollectorTest method testWithMaxActive.

@Test
public void testWithMaxActive() {
    collector = collector.withMaxActive(new MaxActive(10000, 5));
    FastFuture cf = Mockito.mock(FastFuture.class);
    BDDMockito.given(cf.isDone()).willReturn(true);
    for (int i = 0; i < 1000; i++) {
        collector.accept(cf);
    }
    Mockito.verify(cf, Mockito.times(0)).isDone();
}
Also used : FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Test(org.junit.Test)

Aggregations

FastFuture (com.oath.cyclops.internal.react.async.future.FastFuture)35 Test (org.junit.Test)30 FileNotFoundException (java.io.FileNotFoundException)21 IOException (java.io.IOException)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 ArrayList (java.util.ArrayList)19 Assert.assertThat (org.junit.Assert.assertThat)18 Assert.assertTrue (org.junit.Assert.assertTrue)18 Before (org.junit.Before)18 FinalPipeline (com.oath.cyclops.internal.react.async.future.FinalPipeline)17 PipelineBuilder (com.oath.cyclops.internal.react.async.future.PipelineBuilder)17 List (java.util.List)17 ForkJoinPool (java.util.concurrent.ForkJoinPool)16 Matchers.equalTo (org.hamcrest.Matchers.equalTo)16 Assert.assertFalse (org.junit.Assert.assertFalse)16 Assert.assertNotNull (org.junit.Assert.assertNotNull)16 Assert.assertNull (org.junit.Assert.assertNull)16 EmptyCollector (com.oath.cyclops.react.collectors.lazy.EmptyCollector)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Function (java.util.function.Function)2