Search in sources :

Example 1 with JobInput

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IFuture(org.eclipse.scout.rt.platform.job.IFuture) JobInput(org.eclipse.scout.rt.platform.job.JobInput) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IClientSession(org.eclipse.scout.rt.client.IClientSession) JobState(org.eclipse.scout.rt.platform.job.JobState) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 2 with JobInput

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);
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor)

Example 3 with JobInput

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);
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 4 with JobInput

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));
    }
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 5 with JobInput

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);
    }
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Aggregations

JobInput (org.eclipse.scout.rt.platform.job.JobInput)10 Test (org.junit.Test)8 CallableChain (org.eclipse.scout.rt.platform.chain.callable.CallableChain)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ExceptionHandler (org.eclipse.scout.rt.platform.exception.ExceptionHandler)2 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)2 IFuture (org.eclipse.scout.rt.platform.job.IFuture)2 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)2 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)2 HashSet (java.util.HashSet)1 IClientSession (org.eclipse.scout.rt.client.IClientSession)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 RunContext (org.eclipse.scout.rt.platform.context.RunContext)1 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 IFilter (org.eclipse.scout.rt.platform.filter.IFilter)1 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)1 JobState (org.eclipse.scout.rt.platform.job.JobState)1 IJobListener (org.eclipse.scout.rt.platform.job.listener.IJobListener)1 IRegistrationHandle (org.eclipse.scout.rt.platform.util.IRegistrationHandle)1