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