use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testSemaphore.
@Test
@Times(20)
public void testSemaphore() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1).seal();
// Schedule arbitrary job with same semaphore
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDownAndBlock();
}
}, Jobs.newInput().withExecutionSemaphore(mutex));
// Schedule future
IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
return "abc";
}
}, Jobs.newInput().withExceptionHandling(null, // to not work with JUnitExceptionHandler
true));
// Schedule function with same semaphore
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().withExecutionSemaphore(mutex));
assertEquals("abc", future.awaitDoneAndGet(5, TimeUnit.SECONDS));
JobTestUtil.waitForPermitCompetitors(mutex, 2);
// Function future must not have commenced execution yet.
try {
functionFuture.awaitDone(100, TimeUnit.MILLISECONDS);
fail("timeout expected");
} catch (TimedOutError e) {
// NOOP
}
assertFalse(functionExecuted.get());
setupLatch.unblock();
functionFuture.awaitDone(5, TimeUnit.SECONDS);
assertTrue(functionExecuted.get());
assertFalse(future.isCancelled());
assertTrue(future.isDone());
assertFalse(functionFuture.isCancelled());
assertTrue(functionFuture.isDone());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testCancellationOfFunctionRunMonitor.
@Test
public void testCancellationOfFunctionRunMonitor() 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);
RunMonitor functionRunMonitor = BEANS.get(RunMonitor.class);
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().withRunContext(RunContexts.empty().withRunMonitor(functionRunMonitor)).withExecutionHint(JOB_MARKER));
assertTrue(setupLatch.await());
functionRunMonitor.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 WhenDoneTest method testError.
@Test
public void testError() 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 ProcessingException pe = new ProcessingException("expected JUnit test exception");
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
protocol.add("1");
throw pe;
}
}, Jobs.newInput().withExceptionHandling(null, false).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);
assertSame(pe, eventHolder.getValue().getException());
assertNull(eventHolder.getValue().getResult());
assertFalse(eventHolder.getValue().isCancelled());
assertTrue(eventHolder.getValue().isFailed());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneTest method testCancel.
@Test
public void testCancel() 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)));
future.cancel(true);
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("2", "done", "cancelled"), protocol);
assertNull(eventHolder.getValue().getException());
assertNull(eventHolder.getValue().getResult());
assertTrue(eventHolder.getValue().isCancelled());
assertFalse(eventHolder.getValue().isFailed());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneTest method testCancelWithJobAlreadyCompleted.
@Test
public void testCancelWithJobAlreadyCompleted() 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()));
future.awaitDoneAndGet();
future.cancel(true);
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