use of org.eclipse.scout.rt.platform.job.JobInput 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.JobInput in project scout.rt by eclipse.
the class JobManager method createJobFutureTask.
/**
* Creates the Future to be given to {@link ExecutorService} and registers it with this job manager.
*
* @param callable
* callable to be given to the executor for execution.
* @param input
* input that describes the job to be executed.
*/
protected <RESULT> JobFutureTask<RESULT> createJobFutureTask(final Callable<RESULT> callable, final JobInput input) {
final RunMonitor runMonitor = Assertions.assertNotNull(input.getRunContext() != null ? input.getRunContext().getRunMonitor() : BEANS.get(RunMonitor.class), "'RunMonitor' required if providing a 'RunContext'");
final JobInput inputCopy = ensureJobInputName(input, callable.getClass().getName());
return new JobFutureTask<>(this, runMonitor, inputCopy, new CallableChain<RESULT>(), callable);
}
use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.
the class ExceptionProcessorTest method testSuccess.
@Test
public void testSuccess() throws Exception {
JobInput jobInput = Jobs.newInput();
CallableChain<String> chain = new CallableChain<>();
chain.add(new ExceptionProcessor<String>(jobInput));
String result = chain.call(new Callable<String>() {
@Override
public String call() throws Exception {
return "result";
}
});
assertEquals("result", result);
}
use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.
the class ExceptionProcessorTest method testWithNullExceptionHandlerAndPropagate.
@Test
public void testWithNullExceptionHandlerAndPropagate() throws Exception {
final RuntimeException exception = new RuntimeException("expected JUnit test exception");
JobInput jobInput = Jobs.newInput().withExceptionHandling(null, false);
CallableChain<String> chain = new CallableChain<>();
chain.add(new ExceptionProcessor<String>(jobInput));
try {
chain.call(new Callable<String>() {
@Override
public String call() throws Exception {
throw exception;
}
});
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(m_exceptionHandler, never()).handle(eq(exception));
}
}
use of org.eclipse.scout.rt.platform.job.JobInput in project scout.rt by eclipse.
the class ExceptionProcessorTest method testDefaultSettingsWithException.
@Test
public void testDefaultSettingsWithException() throws Exception {
final RuntimeException exception = new RuntimeException("expected JUnit test exception");
JobInput jobInput = Jobs.newInput();
CallableChain<String> chain = new CallableChain<>();
chain.add(new CallableChainExceptionHandler<String>());
chain.add(new ExceptionProcessor<String>(jobInput));
try {
chain.call(new Callable<String>() {
@Override
public String call() throws Exception {
throw exception;
}
});
fail();
} catch (RuntimeException e) {
assertSame(exception, e);
verify(m_exceptionHandler, times(1)).handle(eq(exception));
verifyNoMoreInteractions(m_exceptionHandler);
}
}
Aggregations