use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testCancellationOfFuture.
@Test
public void testCancellationOfFuture() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Schedule future
IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
setupLatch.countDownAndBlock();
return "abc";
}
}, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
true).withExecutionHint(JOB_MARKER));
// Schedule function
final AtomicBoolean functionExecuted = new AtomicBoolean(false);
IFuture<Void> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, Void>() {
@Override
public Void apply(String result, Throwable error) {
functionExecuted.set(true);
return null;
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
assertTrue(setupLatch.await());
future.cancel(true);
// Wait until the job jobs are done
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), 5, TimeUnit.SECONDS);
// Verify that the function is not executed
assertFalse(functionExecuted.get());
// Verify that both futures are cancelled
assertFutureCancelled(future);
assertFutureCancelled(functionFuture);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testExecutionHints.
@Test
public void testExecutionHints() throws InterruptedException {
// Schedule future
IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
return "abc";
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
final BlockingCountDownLatch hint1AddedLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch hint1RemovedLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch hint2AddedLach = new BlockingCountDownLatch(1);
final BlockingCountDownLatch hint2RemovedLatch = new BlockingCountDownLatch(1);
// Schedule function
IFuture<String> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, String>() {
@Override
public String apply(String result, Throwable error) {
assertTrue(IFuture.CURRENT.get().containsExecutionHint(JOB_MARKER));
try {
// Make hint changes to the future
IFuture.CURRENT.get().addExecutionHint("HINT1");
hint1AddedLatch.countDownAndBlock();
IFuture.CURRENT.get().removeExecutionHint("HINT1");
hint1RemovedLatch.countDownAndBlock();
// Verify that external hint changes are reflected
assertTrue(hint2AddedLach.await());
assertTrue(IFuture.CURRENT.get().containsExecutionHint("HINT2"));
hint2AddedLach.unblock();
assertTrue(hint2RemovedLatch.await());
assertFalse(IFuture.CURRENT.get().containsExecutionHint("HINT2"));
hint2RemovedLatch.unblock();
} catch (InterruptedException e) {
throw new ThreadInterruptedError("", e);
}
return result.toUpperCase();
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
try {
assertTrue(functionFuture.containsExecutionHint(JOB_MARKER));
assertTrue(hint1AddedLatch.await());
// Verify that internal hint changes are reflected
assertTrue(functionFuture.containsExecutionHint("HINT1"));
hint1AddedLatch.unblock();
assertTrue(hint1RemovedLatch.await());
assertFalse(functionFuture.containsExecutionHint("HINT1"));
hint1RemovedLatch.unblock();
// Make hint changes to the future
functionFuture.addExecutionHint("HINT2");
assertTrue(hint2AddedLach.countDownAndBlock());
functionFuture.removeExecutionHint("HINT2");
assertTrue(hint2RemovedLatch.countDownAndBlock());
assertEquals("abc", future.awaitDoneAndGet());
assertEquals("ABC", functionFuture.awaitDoneAndGet());
} finally {
Jobs.getJobManager().cancel(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), true);
}
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testCancellationOfFunctionFuture.
@Test
public void testCancellationOfFunctionFuture() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Schedule future
IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
setupLatch.countDownAndBlock();
return "abc";
}
}, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
true).withExecutionHint(JOB_MARKER));
// Schedule function
final AtomicBoolean functionExecuted = new AtomicBoolean(false);
IFuture<Void> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, Void>() {
@Override
public Void apply(String result, Throwable error) {
functionExecuted.set(true);
return null;
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
assertTrue(setupLatch.await());
functionFuture.cancel(true);
setupLatch.unblock();
// Wait until the job jobs are done
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_MARKER).toFilter(), 5, TimeUnit.SECONDS);
// Verify that the function is not executed
assertFalse(functionExecuted.get());
// Verify that future is not cancelled
assertEquals("abc", future.awaitDoneAndGet());
assertFalse(future.isCancelled());
assertTrue(future.isDone());
// Verify that function future is cancelled
assertFutureCancelled(functionFuture);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testPostCancellation.
@Test
public void testPostCancellation() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Schedule future
IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
return "abc";
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
// Schedule function
IFuture<String> functionFuture = future.whenDoneSchedule(new IBiFunction<String, Throwable, String>() {
@Override
public String apply(String result, Throwable error) {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
throw new ThreadInterruptedError("", e);
}
return result.toUpperCase();
}
}, Jobs.newInput().withExecutionHint(JOB_MARKER));
assertTrue(setupLatch.await());
// Cancel future which already completed
future.cancel(true);
setupLatch.unblock();
assertEquals("abc", future.awaitDoneAndGet());
assertEquals("ABC", functionFuture.awaitDoneAndGet());
assertFalse(future.isCancelled());
assertFalse(functionFuture.isCancelled());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneTest method testSuccess.
@Test
public void testSuccess() throws InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final Holder<DoneEvent<String>> eventHolder = new Holder<>();
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
protocol.add("1");
return "result";
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.SECONDS)));
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
future.whenDone(new IDoneHandler<String>() {
@Override
public void onDone(DoneEvent<String> event) {
protocol.add("2");
if (future.isDone()) {
protocol.add("done");
}
if (future.isCancelled()) {
protocol.add("cancelled");
}
eventHolder.setValue(event);
verifyLatch.countDown();
}
}, RunContexts.copyCurrent());
assertTrue(verifyLatch.await());
assertEquals(CollectionUtility.arrayList("1", "2", "done"), protocol);
assertNull(eventHolder.getValue().getException());
assertEquals("result", eventHolder.getValue().getResult());
assertFalse(eventHolder.getValue().isCancelled());
assertFalse(eventHolder.getValue().isFailed());
}
Aggregations