use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class JobScheduleTest method testRuntimeExceptionWithCallable.
@Test
public void testRuntimeExceptionWithCallable() {
final RuntimeException exception = new RuntimeException("expected JUnit test exception");
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
throw exception;
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
try {
future.awaitDoneAndGet();
fail("Exception expected");
} catch (RuntimeException e) {
assertSame(exception, e);
assertTrue(future.isDone());
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class JobScheduleTest method testProcessingExceptionWithRunnable.
@Test
public void testProcessingExceptionWithRunnable() {
final ProcessingException exception = new ProcessingException("expected JUnit test exception");
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
throw exception;
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
try {
future.awaitDoneAndGet();
fail("Exception expected");
} catch (Exception e) {
assertSame(exception, e);
assertTrue(future.isDone());
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class AwaitDoneTest method testAwaitDoneOrWaiting.
@Test
public void testAwaitDoneOrWaiting() {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("before-1");
condition.waitFor();
protocol.add("after-2");
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
try {
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 100, TimeUnit.MILLISECONDS);
fail("timeout expected");
} catch (TimedOutError e) {
// NOOP
}
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).andMatchNotState(JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter(), 10, TimeUnit.SECONDS);
assertEquals(Arrays.asList("before-1"), protocol);
// Cleanup
condition.setBlocking(false);
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 10, TimeUnit.SECONDS);
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class AwaitDoneTest method testAwaitAllDone.
@Test
public void testAwaitAllDone() {
// synchronized because modified/read by different threads.
final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("run-1");
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
future.awaitDone(10, TimeUnit.SECONDS);
assertEquals(CollectionUtility.hashSet("run-1"), protocol);
assertTrue(future.isDone());
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class BlockingConditionInterruptionTest method runTest.
private static void runTest(final List<String> protocol, final WaitMethod waitForMethod, final InterruptionAction interruptionAction) {
final String HINT_BLOCKED = "blocked";
final AtomicReference<Thread> runnerThread = new AtomicReference<>();
final IBlockingCondition bc = Jobs.newBlockingCondition(true);
// Schedule job to enter blocking condition
final IFuture<Void> future = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
runnerThread.set(Thread.currentThread());
if (InterruptionAction.INTERRUPT_BEFORE_ENTERING.equals(interruptionAction)) {
Thread.currentThread().interrupt();
}
try {
protocol.add("beforeBlockingCondition");
switch(waitForMethod) {
case WAIT_INTERRUPTIBLY:
bc.waitFor(HINT_BLOCKED);
break;
case WAIT_FOR_INTERRUPTIBLY_WITH_TIMEOUT:
bc.waitFor(0, TimeUnit.MILLISECONDS, HINT_BLOCKED);
break;
case WAIT_FOR_UNINTERRUPTIBLY:
bc.waitForUninterruptibly(HINT_BLOCKED);
break;
case WAIT_FOR_UNINTERRUPTIBLY_WITH_TIMEOUT:
bc.waitForUninterruptibly(0, TimeUnit.MILLISECONDS, HINT_BLOCKED);
break;
default:
throw new UnsupportedOperationException();
}
protocol.add("afterBlockingCondition");
} catch (ThreadInterruptedError e) {
protocol.add("InterruptedException");
} catch (TimedOutError e) {
protocol.add("TimeoutException");
}
if (Thread.currentThread().isInterrupted()) {
protocol.add("threadInterrupted");
}
}
}, Jobs.newInput().withName("test job").withExecutionHint(JOB_IDENTIFIER));
// Wait until the job enters blocking condition, or is done.
JobTestUtil.waitForCondition(new ICondition() {
@Override
public boolean isFulfilled() {
return future.containsExecutionHint(HINT_BLOCKED) || future.isDone();
}
});
if (InterruptionAction.INTERRUPT_WHILE_BLOCKING.equals(interruptionAction)) {
runnerThread.get().interrupt();
// Sleep some time so that the runner is interrupted.
// That is because a thread's interruption is asynchronous, meaning that once a thread (a) interrupts another threads
// (b), and in turn thread (a) unblocks the condition which thread (b) is waiting for, it is not ensured that thread
// (b) exists with an {@link InterruptedException}. However, many tests expect exactly that behavior.
SleepUtil.sleepSafe(100, TimeUnit.MILLISECONDS);
}
bc.setBlocking(false);
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
}
Aggregations