use of org.eclipse.scout.rt.platform.job.internal.JobManager 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.platform.job.internal.JobManager in project scout.rt by eclipse.
the class JobFutureVisitTest method before.
@Before
public void before() throws InterruptedException {
m_jobManagerBean = JobTestUtil.replaceCurrentJobManager(new JobManager() {
});
m_mutex1 = Jobs.newExecutionSemaphore(1);
m_mutex2 = Jobs.newExecutionSemaphore(1);
m_mutex3 = Jobs.newExecutionSemaphore(1);
// prepare the test-case
// synchronized because modified/read by different threads.
protocol = Collections.synchronizedSet(new HashSet<String>());
bc1 = Jobs.newBlockingCondition(true);
bc2 = Jobs.newBlockingCondition(true);
m_latch = new BlockingCountDownLatch(3);
// SESSION 1 (JOB-1)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
bc1.waitFor();
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job1").withExecutionSemaphore(m_mutex1).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
// SESSION 1 (JOB-2)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
m_latch.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job2").withExecutionSemaphore(m_mutex1).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
// SESSION 1 (JOB-3)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex1_job3").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex1));
// =========
// SESSION 2 (JOB-1)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job1").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex2));
// SESSION 2 (JOB-2)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
bc2.waitFor();
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job2").withExecutionSemaphore(m_mutex2).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
// SESSION 2 (JOB-3)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
bc2.setBlocking(false);
// Wait until job 'mutex2_job2' is re-acquiring the mutex. [3=job-2, job-3, job-4]
JobTestUtil.waitForPermitCompetitors(m_mutex2, 3);
m_latch.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job3").withExecutionSemaphore(m_mutex2).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
// SESSION 2 (JOB-4)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex2_job4").withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(m_mutex2));
// =========
// SESSION 3 (JOB-1)
Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
m_latch.countDownAndBlock();
}
}, Jobs.newInput().withRunContext(RunContexts.empty()).withName("mutex3_job1").withExecutionSemaphore(m_mutex3).withExecutionHint(JOB_IDENTIFIER).withExceptionHandling(null, false));
assertTrue(m_latch.await());
}
Aggregations