use of org.eclipse.scout.rt.platform.context.RunMonitor in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testCancellationOfSharedRunMonitor.
@Test
public void testCancellationOfSharedRunMonitor() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
RunMonitor sharedRunMonitor = BEANS.get(RunMonitor.class);
// 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).withRunContext(RunContexts.empty().withRunMonitor(sharedRunMonitor)).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().withRunContext(RunContexts.empty().withRunMonitor(sharedRunMonitor)).withExecutionHint(JOB_MARKER));
assertTrue(setupLatch.await());
sharedRunMonitor.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.platform.context.RunMonitor in project scout.rt by eclipse.
the class WhenDoneScheduleTest method testCancellationOfFutureRunMonitor.
@Test
public void testCancellationOfFutureRunMonitor() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
RunMonitor runMonitor = BEANS.get(RunMonitor.class);
// 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).withRunContext(RunContexts.empty().withRunMonitor(runMonitor)).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());
runMonitor.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.platform.context.RunMonitor 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.platform.context.RunMonitor 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.platform.context.RunMonitor in project scout.rt by eclipse.
the class AbstractServiceTunnel method checkAlreadyCancelled.
/**
* Will throw a CancellationException if the future is already cancelled.
*
* @throws ThreadInterruptedError
* if the current thread is cancelled
*/
protected void checkAlreadyCancelled(ServiceTunnelRequest serviceRequest) {
final RunMonitor monitor = RunMonitor.CURRENT.get();
if (monitor != null && monitor.isCancelled()) {
final StringBuilder cancellationExceptionText = new StringBuilder();
cancellationExceptionText.append("RunMonitor is already cancelled.");
if (serviceRequest != null) {
cancellationExceptionText.append(" (Request was '");
cancellationExceptionText.append(serviceRequest.getServiceInterfaceClassName());
cancellationExceptionText.append(".");
cancellationExceptionText.append(serviceRequest.getOperation());
cancellationExceptionText.append("(..)')");
}
throw new ThreadInterruptedError(cancellationExceptionText.toString());
}
}
Aggregations