Search in sources :

Example 26 with FastFuture

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

the class FastFutureTest method onCompleteWithNoPipeline_notCompleted_race.

@Test
public void onCompleteWithNoPipeline_notCompleted_race() throws InterruptedException {
    for (int i = 0; i < TIMES; i++) {
        CountDownLatch race = new CountDownLatch(1);
        CountDownLatch init = new CountDownLatch(1);
        called = false;
        FastFuture f = future.build();
        Thread t1 = new Thread(() -> {
            init.countDown();
            try {
                race.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            f.onComplete(event -> called = true);
        });
        t1.start();
        race.countDown();
        f.set(10);
        t1.join();
        assertTrue(f.isDone());
        assertTrue(called);
    }
}
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 27 with FastFuture

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

the class FastFutureTest method onFailRecovered3.

@Test
public void onFailRecovered3() {
    FastFuture f = future.onFail(t -> failed = t).exceptionally(e -> {
        if (e instanceof IOException)
            return "hello world";
        throw (RuntimeException) e;
    }).exceptionally(e -> {
        if (e instanceof FileNotFoundException)
            return "hello world2";
        throw (RuntimeException) e;
    }).exceptionally(e -> {
        if (e instanceof RuntimeException)
            return "hello world3";
        throw (RuntimeException) e;
    }).thenApply(v -> {
        throw new RuntimeException();
    }).build();
    f.set("boo!");
    assertThat(f.join(), equalTo("hello world3"));
}
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) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Test(org.junit.Test)

Example 28 with FastFuture

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

the class FastFutureTest method onCompleteReversed_notCompleted_race.

@Test
public void onCompleteReversed_notCompleted_race() throws InterruptedException {
    for (int i = 0; i < TIMES; i++) {
        CountDownLatch race = new CountDownLatch(1);
        CountDownLatch init = new CountDownLatch(1);
        called = false;
        FastFuture f = future.<Integer, Integer>thenApply(x -> x + 2).build();
        Thread t1 = new Thread(() -> {
            init.countDown();
            try {
                race.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            f.set(10);
        });
        t1.start();
        race.countDown();
        f.onComplete(event -> called = true);
        t1.join();
        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) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 29 with FastFuture

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

the class FastFutureTest method onFailRecovered2.

@Test
public void onFailRecovered2() {
    FastFuture f = future.onFail(t -> failed = t).exceptionally(e -> {
        if (e instanceof IOException)
            return "hello world";
        throw (RuntimeException) e;
    }).exceptionally(e -> {
        if (e instanceof RuntimeException)
            return "hello world2";
        throw (RuntimeException) e;
    }).thenApply(v -> {
        throw new RuntimeException();
    }).build();
    f.set("boo!");
    assertThat(f.join(), equalTo("hello world2"));
}
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) Test(org.junit.Test)

Example 30 with FastFuture

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

the class FastFutureTest method firstRecoverOrder.

@Test
public void firstRecoverOrder() {
    FastFuture f = future.thenApply(v -> {
        throw new RuntimeException();
    }).exceptionally(e -> "hello world").build();
    f.set("boo!");
    assertThat(f.join(), equalTo("hello world"));
}
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)

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