use of org.eclipse.scout.rt.platform.job.JobState in project scout.rt by eclipse.
the class JobListenerBlockedFutureTest method testEventsForBlockingJob.
@Test(timeout = 10000)
public void testEventsForBlockingJob() {
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
IClientSession clientSession = mock(IClientSession.class);
when(clientSession.getModelJobSemaphore()).thenReturn(Jobs.newExecutionSemaphore(1));
JobEventCaptureListener captureListener = new JobEventCaptureListener();
Jobs.getJobManager().addListener(ModelJobs.newEventFilterBuilder().toFilter(), captureListener);
IFuture<Void> outerFuture = null;
final AtomicReference<IFuture<?>> innerFuture = new AtomicReference<>();
final JobInput modelJobInput = ModelJobs.newInput(ClientRunContexts.empty().withSession(clientSession, true));
// start recording of events
outerFuture = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
innerFuture.set(Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
condition.setBlocking(false);
// Wait until the outer future is re-acquiring the mutex.
// 2=outer-job + inner-job
JobTestUtil.waitForPermitCompetitors(modelJobInput.getExecutionSemaphore(), 2);
}
}, modelJobInput.copy().withName("inner").withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(2, TimeUnit.SECONDS))));
condition.waitFor();
}
}, modelJobInput.copy().withName("outer"));
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(outerFuture).toFilter(), 1, TimeUnit.MINUTES);
Jobs.getJobManager().shutdown();
// verify events
int i = -1;
List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
// outer
i++;
assertStateChangedEvent(outerFuture, JobState.SCHEDULED, capturedEvents.get(i));
assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
// outer
i++;
assertStateChangedEvent(outerFuture, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
// outer
i++;
assertStateChangedEvent(outerFuture, JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
// inner
i++;
assertStateChangedEvent(innerFuture.get(), JobState.SCHEDULED, capturedEvents.get(i));
assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
// inner
i++;
assertStateChangedEvent(innerFuture.get(), JobState.PENDING, capturedEvents.get(i));
assertEquals(JobState.PENDING, capturedFutureStates.get(i));
// outer
i++;
assertStateChangedEvent(outerFuture, JobState.WAITING_FOR_BLOCKING_CONDITION, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, capturedFutureStates.get(i));
// inner
i++;
assertStateChangedEvent(innerFuture.get(), JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
// inner
i++;
assertStateChangedEvent(innerFuture.get(), JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
// outer
i++;
assertStateChangedEvent(outerFuture, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
// inner
i++;
assertStateChangedEvent(innerFuture.get(), JobState.DONE, capturedEvents.get(i));
assertEquals(JobState.DONE, capturedFutureStates.get(i));
// outer
i++;
assertStateChangedEvent(outerFuture, JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
// outer
i++;
assertStateChangedEvent(outerFuture, JobState.DONE, capturedEvents.get(i));
assertEquals(JobState.DONE, capturedFutureStates.get(i));
assertEquals(i + 1, capturedEvents.size());
}
use of org.eclipse.scout.rt.platform.job.JobState in project scout.rt by eclipse.
the class JobListenerBlockedFutureTest method testEvents.
@Test
public void testEvents() throws Exception {
JobEventCaptureListener captureListener = new JobEventCaptureListener();
Jobs.getJobManager().addListener(captureListener);
IClientSession clientSession = mock(IClientSession.class);
IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {
@Override
public void run() throws Exception {
// NOOP
}
}, Jobs.newInput().withRunContext(ClientRunContexts.empty().withSession(clientSession, true)));
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 1, TimeUnit.MINUTES);
Jobs.getJobManager().shutdown();
// verify events
int i = -1;
List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
i++;
assertStateChangedEvent(future, JobState.SCHEDULED, capturedEvents.get(i));
assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
i++;
assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
assertEquals(JobState.DONE, capturedFutureStates.get(i));
i++;
assertJobManagerShutdownEvent(capturedEvents.get(i));
assertNull(capturedFutureStates.get(i));
assertEquals(i + 1, capturedEvents.size());
}
Aggregations