use of org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError in project scout.rt by eclipse.
the class MutualExclusionTest method testBlockingCondition_InterruptedWhileBeingBlocked.
/**
* We have 3 jobs that are scheduled simultaneously. Thereby, job1 enters a blocking condition which in turn lets
* job-2 run. Job-2 goes to sleep until released, meaning that job-3 will not start running. The test verifies, that
* when job-1 is interrupted, job-1 competes for the mutex anew and continues execution upon job-2 terminates, but
* before job-3 commence execution.
*/
@Test
public void testBlockingCondition_InterruptedWhileBeingBlocked() throws java.lang.InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
final IExecutionSemaphore semaphore = ClientRunContexts.copyCurrent().getSession().getModelJobSemaphore();
final IFuture<Void> future1 = ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("running-1");
try {
condition.waitFor();
} catch (ThreadInterruptedError e) {
protocol.add("interrupted-1 (a)");
if (Thread.interrupted()) {
protocol.add("interrupted-1 (b)");
// Restore the interruption status
Thread.currentThread().interrupt();
}
if (ModelJobs.isModelThread()) {
protocol.add("model-thread-1");
}
}
verifyLatch.countDown();
}
}, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("running-2");
job2RunningLatch.countDownAndBlock();
}
}, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExceptionHandling(null, false).withExecutionHint(JOB_IDENTIFIER));
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add("running-3");
}
}, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
assertTrue(job2RunningLatch.await());
assertEquals(future1.getState(), JobState.WAITING_FOR_BLOCKING_CONDITION);
// RUN THE TEST
// job-2 and job-3
assertEquals(2, semaphore.getCompetitorCount());
future1.cancel(true);
// wait until job-1 is re-acquiring the mutex
JobTestUtil.waitForPermitCompetitors(semaphore, 3);
// Release job-2
job2RunningLatch.unblock();
// VERIFY
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(JOB_IDENTIFIER).toFilter(), 10, TimeUnit.SECONDS);
assertEquals(Arrays.asList("running-1", "running-2", "interrupted-1 (a)", "interrupted-1 (b)", "model-thread-1", "running-3"), protocol);
}
Aggregations