use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testCancelChildJob.
/**
* Cancel of a job that has a child job.
*/
@Test
public void testCancelChildJob() throws Exception {
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(2);
final BlockingCountDownLatch job2DoneLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(2);
final AtomicReference<IFuture<?>> childFutureRef = new AtomicReference<>();
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
// attach to child runmonitor -> nested cancel
IFuture<?> childFuture = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job-2-interrupted");
}
if (IFuture.CURRENT.get().isCancelled()) {
protocol.add("job-2-cancelled (future)");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("job-2-cancelled (monitor)");
}
job2DoneLatch.countDown();
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-2").withExceptionHandling(null, false));
childFutureRef.set(childFuture);
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job-1-interrupted");
}
if (IFuture.CURRENT.get().isCancelled()) {
protocol.add("job-1-cancelled (future)");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("job-1-cancelled (monitor)");
}
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-1"));
assertTrue(setupLatch.await());
childFutureRef.get().cancel(true);
assertTrue(job2DoneLatch.await());
setupLatch.unblock();
assertTrue(verifyLatch.await());
assertEquals(CollectionUtility.hashSet("job-2-interrupted", "job-2-cancelled (future)", "job-2-cancelled (monitor)"), protocol);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testCancelledWithDetachedRunMonitor.
@Test
public void testCancelledWithDetachedRunMonitor() {
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 {
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDownAndBlock();
cancelled.set(RunMonitor.CURRENT.get().isCancelled());
assertLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.empty().withRunMonitor(BEANS.get(RunMonitor.class))));
setupLatch.await();
RunMonitor.CURRENT.get().cancel(false);
setupLatch.unblock();
assertLatch.await();
assertFalse(cancelled.get());
}
});
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testCancelParentJob.
/**
* Cancel of a job that has a nested job.
*/
@Test
public void testCancelParentJob() throws Exception {
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
final BlockingCountDownLatch job1DoneLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(3);
IFuture<Void> future1 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
// re-use runmonitor -> nested cancel
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job-2-interrupted");
}
if (IFuture.CURRENT.get().isCancelled()) {
protocol.add("job-2-cancelled (future)");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("job-2-cancelled (monitor)");
}
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-2").withExceptionHandling(null, false));
// does not re-use runmonitor -> no nested cancel
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job-3-interrupted");
}
if (IFuture.CURRENT.get().isCancelled()) {
protocol.add("job-3-cancelled (future)");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("job-3-cancelled (monitor)");
}
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent().withRunMonitor(new RunMonitor())).withName("job-3").withExceptionHandling(null, false));
try {
setupLatch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("job-1-interrupted");
}
if (IFuture.CURRENT.get().isCancelled()) {
protocol.add("job-1-cancelled (future)");
}
if (RunMonitor.CURRENT.get().isCancelled()) {
protocol.add("job-1-cancelled (monitor)");
}
job1DoneLatch.countDown();
verifyLatch.countDown();
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withName("job-1"));
assertTrue(setupLatch.await());
future1.cancel(true);
assertTrue(job1DoneLatch.await());
setupLatch.unblock();
assertTrue(verifyLatch.await());
assertEquals(CollectionUtility.hashSet("job-1-interrupted", "job-1-cancelled (future)", "job-1-cancelled (monitor)", "job-2-interrupted", "job-2-cancelled (future)", "job-2-cancelled (monitor)"), protocol);
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testShutdownJobManagerAndSchedule.
@Test
public void testShutdownJobManagerAndSchedule() throws InterruptedException {
// Use dedicated job manager because job manager is shutdown in tests.
IBean<IJobManager> jobManagerBean = JobTestUtil.replaceCurrentJobManager(new JobManager() {
});
try {
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
final BlockingCountDownLatch latch = new BlockingCountDownLatch(2);
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("running-1");
try {
latch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("interrupted-1");
} finally {
protocol.add("done-1");
}
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("running-2");
try {
latch.countDownAndBlock();
} catch (InterruptedException e) {
protocol.add("interrupted-2");
} finally {
protocol.add("done-2");
}
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
assertTrue(latch.await());
// SHUTDOWN
Jobs.getJobManager().shutdown();
try {
Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
fail("AssertionError expected");
} catch (AssertionException e) {
// NOOP
}
// VERIFY
assertEquals(CollectionUtility.hashSet("running-1", "running-2", "interrupted-1", "interrupted-2", "done-1", "done-2"), protocol);
future2.awaitDone(1, TimeUnit.SECONDS);
assertTrue(future2.isCancelled());
} finally {
JobTestUtil.unregisterAndShutdownJobManager(jobManagerBean);
}
}
use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.
the class JobCancelTest method testCancelledWithNullRunContext.
@Test
public void testCancelledWithNullRunContext() {
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 {
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.countDownAndBlock();
cancelled.set(RunMonitor.CURRENT.get().isCancelled());
assertLatch.countDown();
}
}, Jobs.newInput().withRunContext(null));
setupLatch.await();
RunMonitor.CURRENT.get().cancel(false);
setupLatch.unblock();
assertLatch.await();
assertFalse("no nested cancellation expected", cancelled.get());
}
});
}
Aggregations