Search in sources :

Example 6 with CompletableEval

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

the class EvalTest method testForEachWithErrors.

@Test
public void testForEachWithErrors() 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.peek(i -> processingThread.set(Thread.currentThread().getId())).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()));
    Thread t = new Thread(() -> async.complete(1));
    res.forEach(c -> {
        values.incrementAndGet();
        result.set(c);
    }, e -> {
        error.set(e);
    });
    t.start();
    t.join();
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
    assertThat(values.get(), equalTo(0));
    assertThat(result.get(), equalTo(-1));
    assertThat(error.get(), instanceOf(RuntimeException.class));
}
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 7 with CompletableEval

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

the class EvalTest method testForEachWithOnComplete.

@Test
public void testForEachWithOnComplete() 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);
    AtomicBoolean onComplete = new AtomicBoolean(false);
    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 < 10000)
            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);
    }, () -> onComplete.set(true));
    assertThat(onComplete.get(), equalTo(false));
    t.start();
    t.join();
    assertThat(res.get(), equalTo(count));
    assertThat(res.get(), equalTo(10000));
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
    assertThat(values.get(), equalTo(1));
    assertThat(result.get(), equalTo(10000));
    assertThat(error.get(), equalTo(null));
    assertThat(onComplete.get(), equalTo(true));
}
Also used : 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 8 with CompletableEval

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

the class EvalTest method testForEachWithOnCompleteWithErrors.

@Test
public void testForEachWithOnCompleteWithErrors() 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);
    AtomicBoolean onComplete = new AtomicBoolean(false);
    long mainThread = Thread.currentThread().getId();
    CompletableEval<Integer, Integer> async = Eval.eval();
    Eval<Integer> res = async.peek(i -> processingThread.set(Thread.currentThread().getId())).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()));
    Thread t = new Thread(() -> async.complete(1));
    res.forEach(c -> {
        values.incrementAndGet();
        result.set(c);
    }, e -> {
        error.set(e);
    }, () -> onComplete.set(true));
    assertThat(onComplete.get(), equalTo(false));
    t.start();
    t.join();
    assertThat(mainThread, not(equalTo(processingThread.get())));
    assertThat(-1, not(equalTo(processingThread.get())));
    assertThat(values.get(), equalTo(0));
    assertThat(result.get(), equalTo(-1));
    assertThat(error.get(), instanceOf(RuntimeException.class));
    assertThat(onComplete.get(), equalTo(false));
}
Also used : 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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