use of cyclops.control.Eval.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())));
}
use of cyclops.control.Eval.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())));
}
use of cyclops.control.Eval.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));
}
use of cyclops.control.Eval.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));
}
use of cyclops.control.Eval.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())));
}
Aggregations