use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneTest method testErrorWithJobAlreadyCompleted.
@Test
public void testErrorWithJobAlreadyCompleted() 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 Exception error = 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 error;
}
}, Jobs.newInput().withExceptionHandling(null, false));
try {
future.awaitDoneAndGet();
fail("exception expected");
} catch (ProcessingException e) {
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(error, 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 testSuccessWithJobAlreadyCompleted.
@Test
public void testSuccessWithJobAlreadyCompleted() 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();
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());
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class WhenDoneTest method testCancelButStillRunning.
/**
* Tests that 'Future.whenDone' returns once the Future is cancelled, even if that job is still runnning.
*/
@Test
public void testCancelButStillRunning() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch continueRunningLatch = new BlockingCountDownLatch(1);
final IFuture<Void> future = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
// ensure the thread's interrupted status to be cleared in order to continue the test.
Thread.interrupted();
// continue running
continueRunningLatch.countDownAndBlock();
}
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
assertTrue(setupLatch.await());
// run the test.
future.cancel(true);
// verify that whenDone immediately returns even if not completed yet.
final AtomicBoolean onDone = new AtomicBoolean(false);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
future.whenDone(new IDoneHandler<Void>() {
@Override
public void onDone(DoneEvent<Void> event) {
onDone.set(true);
verifyLatch.countDown();
}
}, RunContexts.copyCurrent());
assertTrue(verifyLatch.await());
assertTrue(onDone.get());
continueRunningLatch.release();
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testCancelledWithEmptyRunContextAndCancellationOnMonitor.
@Test
public void testCancelledWithEmptyRunContextAndCancellationOnMonitor() {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch assertLatch = new BlockingCountDownLatch(1);
final AtomicBoolean cancelled = new AtomicBoolean();
// Run test within RunContext to ensure a current RunMonitor
RunContexts.empty().run(new IRunnable() {
@Override
public void run() throws Exception {
RunContext emptyRunContext = RunContexts.empty();
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDownAndBlock();
cancelled.set(RunMonitor.CURRENT.get().isCancelled());
assertLatch.countDown();
}
}, Jobs.newInput().withRunContext(emptyRunContext));
setupLatch.await();
emptyRunContext.getRunMonitor().cancel(false);
setupLatch.unblock();
assertLatch.await();
assertTrue("cancellation expected", cancelled.get());
}
});
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testCancelSoft.
@Test
public void testCancelSoft() throws InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("cancelled-before");
}
try {
setupLatch.countDownAndBlock(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
protocol.add("interrupted");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("cancelled-after");
}
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
assertTrue(setupLatch.await());
// RUN THE TEST
assertTrue(future.cancel(false));
assertTrue(verifyLatch.await());
// VERIFY
assertEquals(Arrays.asList("cancelled-after"), protocol);
assertTrue(future.isCancelled());
future.awaitDone(1, TimeUnit.SECONDS);
assertTrue(future.isCancelled());
}
Aggregations