Search in sources :

Example 1 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobStateTest method testWaitingForMutexAndBlockingCondition.

@Test
// regression
@Times(100)
public void testWaitingForMutexAndBlockingCondition() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final BlockingCountDownLatch job1RunningLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch job2UnblockedLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch job3RunningLatch = new BlockingCountDownLatch(1);
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    // Schedule job-1
    IFuture<Void> future1 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            job1RunningLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-1").withExecutionSemaphore(mutex));
    // wait until running (for idempotent event assertion)
    assertTrue(job1RunningLatch.await());
    // Schedule job-2
    IFuture<Void> future2 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            job2RunningLatch.countDownAndBlock();
            condition.waitFor();
            job2UnblockedLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-2").withExecutionSemaphore(mutex));
    // Wait until competing for a permit.
    // That is for idempotent event assertion, because permit is acquired asynchronously in another thread.
    // However, permit acquisition is guaranteed to be in the 'as-scheduled' order. Nevertheless, the SCHEDULING and PENDING event of the next job would possibly interfere.
    JobTestUtil.waitForPermitCompetitors(mutex, 2);
    // Schedule job-3
    IFuture<Void> future3 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            job3RunningLatch.countDownAndBlock();
            JobTestUtil.waitForPermitCompetitors(mutex, 2);
        }
    }, Jobs.newInput().withName("job-3").withExecutionSemaphore(mutex));
    // Wait until competing for a permit.
    // That is for idempotent event assertion, because permit is acquired asynchronously in another thread.
    // However, permit acquisition is guaranteed to be in the 'as-scheduled' order. Nevertheless, the SCHEDULING and PENDING event of the next job would possibly interfere.
    JobTestUtil.waitForPermitCompetitors(mutex, 3);
    assertEquals(JobState.RUNNING, future1.getState());
    assertEquals(JobState.WAITING_FOR_PERMIT, future2.getState());
    assertEquals(JobState.WAITING_FOR_PERMIT, future3.getState());
    job1RunningLatch.unblock();
    job2RunningLatch.await();
    assertEquals(JobState.DONE, future1.getState());
    assertEquals(JobState.RUNNING, future2.getState());
    assertEquals(JobState.WAITING_FOR_PERMIT, future3.getState());
    job2RunningLatch.unblock();
    job3RunningLatch.await();
    assertEquals(JobState.DONE, future1.getState());
    assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, future2.getState());
    assertEquals(JobState.RUNNING, future3.getState());
    condition.setBlocking(false);
    // job-2 and job-3
    JobTestUtil.waitForPermitCompetitors(mutex, 2);
    assertEquals(JobState.DONE, future1.getState());
    assertEquals(JobState.WAITING_FOR_PERMIT, future2.getState());
    assertEquals(JobState.RUNNING, future3.getState());
    job3RunningLatch.unblock();
    job2UnblockedLatch.await();
    assertEquals(JobState.DONE, future1.getState());
    assertEquals(JobState.RUNNING, future2.getState());
    assertEquals(JobState.DONE, future3.getState());
    job2UnblockedLatch.unblock();
    future2.awaitDoneAndGet(5, TimeUnit.SECONDS);
    future3.awaitDoneAndGet(5, TimeUnit.SECONDS);
    assertEquals(JobState.DONE, future1.getState());
    assertEquals(JobState.DONE, future2.getState());
    assertEquals(JobState.DONE, future3.getState());
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future1, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future2, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future2, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future3, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future3, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future2, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertBlockedStateEvent(future2, condition, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future3, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future2, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future3, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future2, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future2, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 2 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobStateTest method testScheduleWithFixedDelay.

@Test
public void testScheduleWithFixedDelay() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
        // NOOP
        }
    }, Jobs.newInput().withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.MILLISECONDS).withSchedule(FixedDelayScheduleBuilder.repeatForTotalCount(3, 1, TimeUnit.MILLISECONDS))));
    future.awaitDone(5, TimeUnit.SECONDS);
    // 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.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    // first round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    // second round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    // third round
    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));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 3 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobListenerTest method testLocalListener5.

@Test
public void testLocalListener5() throws InterruptedException {
    final BlockingCountDownLatch jobRunningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            jobRunningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(jobRunningLatch.await());
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    future.addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.DONE).toFilter(), captureListener);
    jobRunningLatch.unblock();
    future.awaitDone();
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 4 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent 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 5 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobManager method shutdown.

@Override
public final void shutdown() {
    LOG.debug("JobManager shutting down.");
    m_shutdownLock.writeLock().lock();
    try {
        m_shutdown = true;
    } finally {
        m_shutdownLock.writeLock().unlock();
    }
    // Dispose Futures.
    m_futures.dispose();
    // Shutdown the Executor.
    shutdownExecutor(m_executor);
    // Fire event that job manager was shutdown.
    fireEvent(new JobEvent(this, JobEventType.JOB_MANAGER_SHUTDOWN, new JobEventData()));
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) JobEventData(org.eclipse.scout.rt.platform.job.listener.JobEventData)

Aggregations

JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)27 Test (org.junit.Test)23 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)19 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)7 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)6 JobEventData (org.eclipse.scout.rt.platform.job.listener.JobEventData)6 IClientSession (org.eclipse.scout.rt.client.IClientSession)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Times (org.eclipse.scout.rt.testing.platform.runner.Times)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)2 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)2 IFuture (org.eclipse.scout.rt.platform.job.IFuture)2 JobInput (org.eclipse.scout.rt.platform.job.JobInput)2 JobState (org.eclipse.scout.rt.platform.job.JobState)2 IJobListener (org.eclipse.scout.rt.platform.job.listener.IJobListener)2 IRegistrationHandle (org.eclipse.scout.rt.platform.util.IRegistrationHandle)2 ISession (org.eclipse.scout.rt.shared.ISession)2 SessionJobEventFilter (org.eclipse.scout.rt.shared.job.filter.event.SessionJobEventFilter)2 HashSet (java.util.HashSet)1