Search in sources :

Example 21 with JobEvent

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

the class JobListenerTest method testCancel.

@Test
public void testCancel() throws Exception {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final BooleanHolder hasStarted = new BooleanHolder(Boolean.FALSE);
    IFuture<Void> future = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            hasStarted.setValue(Boolean.TRUE);
        }
    }, Jobs.newInput().withRunContext(RunContexts.empty()).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.HOURS)));
    future.cancel(true);
    Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchFuture(future).toFilter(), 10, TimeUnit.SECONDS);
    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.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, 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());
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) BooleanHolder(org.eclipse.scout.rt.platform.holders.BooleanHolder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 22 with JobEvent

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

the class JobListenerTest method testLocalListener2a.

@Test
public void testLocalListener2a() throws InterruptedException {
    // Schedule job-1
    IFuture<Void> future1 = Jobs.getJobManager().schedule(mock(IRunnable.class), Jobs.newInput());
    future1.awaitDone();
    // schedule job-2, and install listener once started running
    final BlockingCountDownLatch job2RunningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            job2RunningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(job2RunningLatch.await());
    // install listener
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(Jobs.newEventFilterBuilder().andMatchFuture(future1, future2).toFilter(), captureListener);
    job2RunningLatch.unblock();
    future2.awaitDone();
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    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) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 23 with JobEvent

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

the class JobListenerTest method testLocalListener4.

@Test
public void testLocalListener4() 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(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 24 with JobEvent

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

the class JobEventFilterBuilderTest method testFutureExclusion.

@Test
public void testFutureExclusion() {
    IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    IFuture<?> future1 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future2 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future3 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future4 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future5 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future6 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future7 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future8 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future9 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future10 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex).withExecutionHint(JOB_IDENTIFIER));
    // One future exclusion with no other criteria
    IFilter<JobEvent> filter = Jobs.newEventFilterBuilder().andMatchNotFuture(future8).toFilter();
    assertTrue(filter.accept(newJobStateChangedEvent(future1)));
    assertTrue(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertTrue(filter.accept(newJobStateChangedEvent(future4)));
    assertTrue(filter.accept(newJobStateChangedEvent(future5)));
    assertTrue(filter.accept(newJobStateChangedEvent(future6)));
    assertTrue(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertTrue(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // Multiple future exclusions with no other criteria
    filter = Jobs.newEventFilterBuilder().andMatchNotFuture(future8, future9).toFilter();
    assertTrue(filter.accept(newJobStateChangedEvent(future1)));
    assertTrue(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertTrue(filter.accept(newJobStateChangedEvent(future4)));
    assertTrue(filter.accept(newJobStateChangedEvent(future5)));
    assertTrue(filter.accept(newJobStateChangedEvent(future6)));
    assertTrue(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertFalse(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // One future exclusion with other criterion (mutex)
    filter = Jobs.newEventFilterBuilder().andMatchExecutionSemaphore(mutex).andMatchNotFuture(future8).toFilter();
    assertFalse(filter.accept(newJobStateChangedEvent(future1)));
    assertFalse(filter.accept(newJobStateChangedEvent(future2)));
    assertFalse(filter.accept(newJobStateChangedEvent(future3)));
    assertFalse(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertTrue(filter.accept(newJobStateChangedEvent(future6)));
    assertTrue(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertTrue(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // Multiple future exclusion with other criterion (mutex)
    filter = Jobs.newEventFilterBuilder().andMatchExecutionSemaphore(mutex).andMatchNotFuture(future8, future9).toFilter();
    assertFalse(filter.accept(newJobStateChangedEvent(future1)));
    assertFalse(filter.accept(newJobStateChangedEvent(future2)));
    assertFalse(filter.accept(newJobStateChangedEvent(future3)));
    assertFalse(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertTrue(filter.accept(newJobStateChangedEvent(future6)));
    assertTrue(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertFalse(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 25 with JobEvent

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

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