use of org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError 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.platform.util.concurrent.ThreadInterruptedError 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.platform.util.concurrent.ThreadInterruptedError in project scout.rt by eclipse.
the class FutureAwaitTest method testAwaitDoneAndGet_Interrupted.
@Test(timeout = 5000)
public void testAwaitDoneAndGet_Interrupted() throws java.lang.InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Init
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
setupLatch.countDownAndBlock();
return "result";
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
// Run the test in a separate thread
IFuture<Void> controller = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
Thread.currentThread().interrupt();
try {
future.awaitDoneAndGet();
fail("interruption expected");
} catch (ThreadInterruptedError e) {
assertTrue(Thread.currentThread().isInterrupted());
}
}
}, Jobs.newInput());
controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
setupLatch.unblock();
future.awaitDone(10, TimeUnit.SECONDS);
}
use of org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError in project scout.rt by eclipse.
the class FutureAwaitTest method testJobManagerAwaitDone_Interrupted.
// (timeout = 5000)
@Test
public void testJobManagerAwaitDone_Interrupted() throws java.lang.InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Init
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
setupLatch.countDownAndBlock();
return "result";
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
// Run the test in a separate thread
IFuture<Void> controller = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
Thread.currentThread().interrupt();
try {
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 10, TimeUnit.SECONDS);
fail("interruption expected");
} catch (ThreadInterruptedError e) {
assertTrue(Thread.currentThread().isInterrupted());
}
}
}, Jobs.newInput());
controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
setupLatch.unblock();
future.awaitDone(10, TimeUnit.SECONDS);
}
use of org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError in project scout.rt by eclipse.
the class FutureAwaitTest method testAwaitDone_Interrupted.
@Test(timeout = 5000)
public void testAwaitDone_Interrupted() throws java.lang.InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
// Init
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
setupLatch.countDownAndBlock();
return "result";
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
// Run the test in a separate thread
IFuture<Void> controller = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
Thread.currentThread().interrupt();
try {
future.awaitDone();
fail("interruption expected");
} catch (ThreadInterruptedError e) {
assertTrue(Thread.currentThread().isInterrupted());
}
}
}, Jobs.newInput());
controller.awaitDoneAndGet(10, TimeUnit.SECONDS);
setupLatch.unblock();
future.awaitDone(10, TimeUnit.SECONDS);
}
Aggregations