Search in sources :

Example 1 with Eval

use of cyclops.control.Eval in project cyclops by aol.

the class EvalTest method recoverWithAsync.

@Test
public void recoverWithAsync() throws InterruptedException {
    CompletableEval<Integer, Integer> async = Eval.eval();
    long mainThread = Thread.currentThread().getId();
    AtomicLong processingThread = new AtomicLong(-1l);
    System.out.println("Main " + Thread.currentThread().getId());
    Eval<Integer> error = async.<Integer>map(i -> {
        throw new RuntimeException();
    }).recoverWith(Throwable.class, i -> Eval.now(120)).peek(i -> System.out.println("T " + Thread.currentThread().getId())).peek(i -> processingThread.set(Thread.currentThread().getId())).peek(System.out::println);
    Thread t = new Thread(() -> async.complete(10));
    error.forEach(e -> {
    });
    t.start();
    t.join();
    // error.get();
    Thread.sleep(100);
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) Eval.eval(cyclops.control.Eval.eval) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Future(cyclops.control.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Executors(java.util.concurrent.Executors) Seq(cyclops.data.Seq) Eval(cyclops.control.Eval) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) Tuple(cyclops.data.tuple.Tuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableEval(cyclops.control.Eval.CompletableEval) Try(cyclops.control.Try) NoSuchElementException(java.util.NoSuchElementException) Before(org.junit.Before) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 2 with Eval

use of cyclops.control.Eval in project cyclops by aol.

the class EvalTest method onErrorRestartAsync.

@Test
public void onErrorRestartAsync() throws InterruptedException {
    count = 0;
    AtomicLong processingThread = new AtomicLong(-1l);
    long mainThread = Thread.currentThread().getId();
    CompletableEval<Integer, Integer> async = Eval.eval();
    Eval<Integer> res = async.map(i -> {
        System.out.println("Count " + count);
        count++;
        if (count < 1000)
            throw new RuntimeException();
        return count;
    }).peek(i -> System.out.println("T " + Thread.currentThread().getId())).peek(i -> processingThread.set(Thread.currentThread().getId())).onErrorRestart(100000);
    Thread t = new Thread(() -> async.complete(1));
    res.forEach(e -> {
    });
    t.start();
    t.join();
    assertThat(res.get(), equalTo(count));
    assertThat(res.get(), equalTo(1000));
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) Eval.eval(cyclops.control.Eval.eval) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Future(cyclops.control.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Executors(java.util.concurrent.Executors) Seq(cyclops.data.Seq) Eval(cyclops.control.Eval) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) Tuple(cyclops.data.tuple.Tuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableEval(cyclops.control.Eval.CompletableEval) Try(cyclops.control.Try) NoSuchElementException(java.util.NoSuchElementException) Before(org.junit.Before) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 3 with Eval

use of cyclops.control.Eval in project cyclops by aol.

the class EvalTest method testForEachWithErrorNoErrors.

@Test
public void testForEachWithErrorNoErrors() throws InterruptedException {
    count = 0;
    AtomicReference<Throwable> error = new AtomicReference<>(null);
    AtomicInteger result = new AtomicInteger(-1);
    AtomicInteger values = new AtomicInteger(0);
    AtomicLong processingThread = new AtomicLong(-1l);
    long mainThread = Thread.currentThread().getId();
    CompletableEval<Integer, Integer> async = Eval.eval();
    Eval<Integer> res = async.map(i -> {
        System.out.println("Count " + count);
        count++;
        if (count < 1000)
            throw new RuntimeException();
        return count;
    }).peek(i -> System.out.println("T " + Thread.currentThread().getId())).peek(i -> processingThread.set(Thread.currentThread().getId())).onErrorRestart(100000);
    Thread t = new Thread(() -> async.complete(1));
    res.forEach(c -> {
        values.incrementAndGet();
        result.set(c);
    }, e -> {
        error.set(e);
    });
    t.start();
    t.join();
    assertThat(res.get(), equalTo(count));
    assertThat(res.get(), equalTo(1000));
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
    assertThat(values.get(), equalTo(1));
    assertThat(result.get(), equalTo(1000));
    assertThat(error.get(), equalTo(null));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) Eval.eval(cyclops.control.Eval.eval) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Future(cyclops.control.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Executors(java.util.concurrent.Executors) Seq(cyclops.data.Seq) Eval(cyclops.control.Eval) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) Tuple(cyclops.data.tuple.Tuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableEval(cyclops.control.Eval.CompletableEval) Try(cyclops.control.Try) NoSuchElementException(java.util.NoSuchElementException) Before(org.junit.Before) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 4 with Eval

use of cyclops.control.Eval in project cyclops by aol.

the class EvalTest method testForEach.

@Test
public void testForEach() throws InterruptedException {
    count = 0;
    AtomicInteger result = new AtomicInteger(-1);
    AtomicInteger values = new AtomicInteger(0);
    AtomicLong processingThread = new AtomicLong(-1l);
    long mainThread = Thread.currentThread().getId();
    CompletableEval<Integer, Integer> async = Eval.eval();
    Eval<Integer> res = async.map(i -> {
        System.out.println("Count " + count);
        count++;
        if (count < 1000)
            throw new RuntimeException();
        return count;
    }).peek(i -> System.out.println("T " + Thread.currentThread().getId())).peek(i -> processingThread.set(Thread.currentThread().getId())).onErrorRestart(100000);
    Thread t = new Thread(() -> async.complete(1));
    System.out.println(res.getClass());
    res.forEach(c -> {
        values.incrementAndGet();
        result.set(c);
    });
    t.start();
    t.join();
    assertThat(res.get(), equalTo(count));
    assertThat(res.get(), equalTo(1000));
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
    assertThat(values.get(), equalTo(1));
    assertThat(result.get(), equalTo(1000));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) Eval.eval(cyclops.control.Eval.eval) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Future(cyclops.control.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Executors(java.util.concurrent.Executors) Seq(cyclops.data.Seq) Eval(cyclops.control.Eval) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) Tuple(cyclops.data.tuple.Tuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableEval(cyclops.control.Eval.CompletableEval) Try(cyclops.control.Try) NoSuchElementException(java.util.NoSuchElementException) Before(org.junit.Before) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with Eval

use of cyclops.control.Eval in project cyclops by aol.

the class EvalTest method recoverAsync.

@Test
public void recoverAsync() throws InterruptedException {
    CompletableEval<Integer, Integer> async = Eval.eval();
    long mainThread = Thread.currentThread().getId();
    AtomicLong processingThread = new AtomicLong(-1l);
    System.out.println("Main " + Thread.currentThread().getId());
    Eval<Integer> error = async.<Integer>map(i -> {
        throw new RuntimeException();
    }).recover(Throwable.class, i -> 120).peek(i -> System.out.println("T " + Thread.currentThread().getId())).peek(i -> processingThread.set(Thread.currentThread().getId())).peek(System.out::println);
    Thread t = new Thread(() -> async.complete(10));
    error.forEach(e -> {
    });
    t.start();
    t.join();
    // error.get();
    Thread.sleep(100);
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) Eval.eval(cyclops.control.Eval.eval) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Future(cyclops.control.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Executors(java.util.concurrent.Executors) Seq(cyclops.data.Seq) Eval(cyclops.control.Eval) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) Tuple(cyclops.data.tuple.Tuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableEval(cyclops.control.Eval.CompletableEval) Try(cyclops.control.Try) NoSuchElementException(java.util.NoSuchElementException) Before(org.junit.Before) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Aggregations

Eval (cyclops.control.Eval)8 CompletableEval (cyclops.control.Eval.CompletableEval)8 Eval.eval (cyclops.control.Eval.eval)8 Future (cyclops.control.Future)8 Try (cyclops.control.Try)8 Seq (cyclops.data.Seq)8 Tuple (cyclops.data.tuple.Tuple)8 Tuple2 (cyclops.data.tuple.Tuple2)8 Tuple3 (cyclops.data.tuple.Tuple3)8 ReactiveSeq (cyclops.reactive.ReactiveSeq)8 NoSuchElementException (java.util.NoSuchElementException)8 Executors (java.util.concurrent.Executors)8 TimeUnit (java.util.concurrent.TimeUnit)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Supplier (java.util.function.Supplier)8 Matchers (org.hamcrest.Matchers)8 Assert.assertNotNull (org.junit.Assert.assertNotNull)8