Search in sources :

Example 6 with JobEvent

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

the class JobEventFilterBuilderTest method test.

@Test
public void test() {
    IExecutionSemaphore mutex1 = Jobs.newExecutionSemaphore(1);
    IExecutionSemaphore mutex2 = Jobs.newExecutionSemaphore(1);
    IFuture<?> future1 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("A").withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future2 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("B").withRunContext(RunContexts.empty()).withExecutionSemaphore(mutex1).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future3 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("C").withRunContext(new P_RunContext()).withExecutionSemaphore(mutex1).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future4 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("D").withExecutionHint(JOB_IDENTIFIER).withExecutionTrigger(Jobs.newExecutionTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever())));
    IFuture<?> future5 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("E").withExecutionHint(JOB_IDENTIFIER).withExecutionTrigger(Jobs.newExecutionTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever())));
    IFuture<?> future6 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("E").withRunContext(new P_RunContext()).withExecutionHint(JOB_IDENTIFIER).withExecutionTrigger(Jobs.newExecutionTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever())));
    IFuture<?> future7 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("F").withExecutionSemaphore(mutex1).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future8 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("G").withExecutionSemaphore(mutex1).withExecutionHint(JOB_IDENTIFIER));
    IFuture<?> future9 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("H").withExecutionSemaphore(mutex2));
    IFuture<?> future10 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("I").withRunContext(new P_RunContext()).withExecutionSemaphore(mutex1).withExecutionHint(JOB_IDENTIFIER));
    // with filtering for futures
    IFilter<JobEvent> filter = new JobEventFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).toFilter();
    assertTrue(filter.accept(newJobStateChangedEvent(future1)));
    assertTrue(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertTrue(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertFalse(filter.accept(newJobStateChangedEvent(future6)));
    assertFalse(filter.accept(newJobStateChangedEvent(future7)));
    assertTrue(filter.accept(newJobStateChangedEvent(future8)));
    assertTrue(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // additionally with filtering for single executing jobs
    filter = new JobEventFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().toFilter();
    assertTrue(filter.accept(newJobStateChangedEvent(future1)));
    assertTrue(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertFalse(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertFalse(filter.accept(newJobStateChangedEvent(future6)));
    assertFalse(filter.accept(newJobStateChangedEvent(future7)));
    assertTrue(filter.accept(newJobStateChangedEvent(future8)));
    assertTrue(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // additionally with filtering for mutex
    filter = new JobEventFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).toFilter();
    assertFalse(filter.accept(newJobStateChangedEvent(future1)));
    assertTrue(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertFalse(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertFalse(filter.accept(newJobStateChangedEvent(future6)));
    assertFalse(filter.accept(newJobStateChangedEvent(future7)));
    assertTrue(filter.accept(newJobStateChangedEvent(future8)));
    assertFalse(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // additionally with filtering for jobs running on behalf of a RunContext
    filter = new JobEventFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(RunContext.class).toFilter();
    assertFalse(filter.accept(newJobStateChangedEvent(future1)));
    assertTrue(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertFalse(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertFalse(filter.accept(newJobStateChangedEvent(future6)));
    assertFalse(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertFalse(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // additionally with filtering for jobs running on behalf of a specific P_RunContext
    filter = new JobEventFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(P_RunContext.class).toFilter();
    assertFalse(filter.accept(newJobStateChangedEvent(future1)));
    assertFalse(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertFalse(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertFalse(filter.accept(newJobStateChangedEvent(future6)));
    assertFalse(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertFalse(filter.accept(newJobStateChangedEvent(future9)));
    assertTrue(filter.accept(newJobStateChangedEvent(future10)));
    // additionally with filtering for names
    filter = new JobEventFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(P_RunContext.class).andMatchName("A", "B", "C").toFilter();
    assertFalse(filter.accept(newJobStateChangedEvent(future1)));
    assertFalse(filter.accept(newJobStateChangedEvent(future2)));
    assertTrue(filter.accept(newJobStateChangedEvent(future3)));
    assertFalse(filter.accept(newJobStateChangedEvent(future4)));
    assertFalse(filter.accept(newJobStateChangedEvent(future5)));
    assertFalse(filter.accept(newJobStateChangedEvent(future6)));
    assertFalse(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertFalse(filter.accept(newJobStateChangedEvent(future9)));
    assertFalse(filter.accept(newJobStateChangedEvent(future10)));
    // additionally with filtering for other names
    filter = new JobEventFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(P_RunContext.class).andMatchName("D", "E", "F").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)));
    assertFalse(filter.accept(newJobStateChangedEvent(future6)));
    assertFalse(filter.accept(newJobStateChangedEvent(future7)));
    assertFalse(filter.accept(newJobStateChangedEvent(future8)));
    assertFalse(filter.accept(newJobStateChangedEvent(future9)));
    assertFalse(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 7 with JobEvent

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

the class JobEventFilterTest method test2.

@Test
public void test2() {
    JobEventFilter filter = new JobEventFilter(JobEventType.JOB_STATE_CHANGED, JobEventType.JOB_MANAGER_SHUTDOWN);
    assertTrue(filter.accept(new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData())));
    assertTrue(filter.accept(new JobEvent(mock(IJobManager.class), JobEventType.JOB_MANAGER_SHUTDOWN, new JobEventData())));
    assertFalse(filter.accept(new JobEvent(mock(IJobManager.class), JobEventType.JOB_EXECUTION_HINT_ADDED, new JobEventData())));
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) JobEventData(org.eclipse.scout.rt.platform.job.listener.JobEventData) Test(org.junit.Test)

Example 8 with JobEvent

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

the class JobEventFilterTest method test1.

@Test
public void test1() {
    JobEventFilter filter = new JobEventFilter(JobEventType.JOB_STATE_CHANGED);
    assertTrue(filter.accept(new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData())));
    assertFalse(filter.accept(new JobEvent(mock(IJobManager.class), JobEventType.JOB_MANAGER_SHUTDOWN, new JobEventData())));
    assertFalse(filter.accept(new JobEvent(mock(IJobManager.class), JobEventType.JOB_EXECUTION_HINT_ADDED, new JobEventData())));
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) JobEventData(org.eclipse.scout.rt.platform.job.listener.JobEventData) Test(org.junit.Test)

Example 9 with JobEvent

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

the class SessionJobEventFilterTest method test.

@Test
public void test() {
    IServerSession session1 = mock(IServerSession.class);
    IServerSession session2 = mock(IServerSession.class);
    SessionJobEventFilter filter = new SessionJobEventFilter(session1);
    // Tests JobEvent of an event without a job associated
    JobEvent event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(null));
    assertFalse(filter.accept(event));
    // Tests JobEvent with job without RunContext
    event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput())));
    assertFalse(filter.accept(event));
    // Tests JobEvent with job with RunContext
    event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(RunContexts.empty()))));
    assertFalse(filter.accept(event));
    // Tests JobEvent with job with ClientRunContext without session
    event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(ServerRunContexts.empty()))));
    assertFalse(filter.accept(event));
    // Tests JobEvent with job with ClientRunContext with correct session
    event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(ServerRunContexts.empty().withSession(session1)))));
    assertTrue(filter.accept(event));
    // Tests JobEvent with job with ClientRunContext with wrong session
    event = new JobEvent(mock(IJobManager.class), JobEventType.JOB_STATE_CHANGED, new JobEventData().withFuture(Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withRunContext(ServerRunContexts.empty().withSession(session2)))));
    assertFalse(filter.accept(event));
    // Tests adaptable to the session
    assertSame(session1, filter.getAdapter(ISession.class));
}
Also used : ISession(org.eclipse.scout.rt.shared.ISession) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) SessionJobEventFilter(org.eclipse.scout.rt.shared.job.filter.event.SessionJobEventFilter) IServerSession(org.eclipse.scout.rt.server.IServerSession) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) JobEventData(org.eclipse.scout.rt.platform.job.listener.JobEventData) Test(org.junit.Test)

Example 10 with JobEvent

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

the class BlockingTestUtility method runBlockingAction.

/**
 * Helper method to test code which will enter a blocking condition.
 * <p>
 * If <code>runnableOnceBlocked</code> throws an exception, it is given to {@link JUnitExceptionHandler} to make the
 * JUnit test fail.
 *
 * @param runnableGettingBlocked
 *          {@code IRunnable} that will enter a blocking condition.
 * @param runnableOnceBlocked
 *          {@code IRunnable} to be executed once the 'runnableGettingBlocked' enters a blocking condition.
 * @param awaitBackgroundJobs
 *          true waits for background jobs running in the same session to complete before runnableOnceBlocked is
 *          called
 */
public static void runBlockingAction(final IRunnable runnableGettingBlocked, final IRunnable runnableOnceBlocked, final boolean awaitBackgroundJobs) {
    final ClientRunContext runContext = ClientRunContexts.copyCurrent();
    final IBlockingCondition onceBlockedDoneCondition = Jobs.newBlockingCondition(true);
    // remember the list of client jobs before blocking
    final Set<IFuture<?>> jobsBefore = new HashSet<>();
    jobsBefore.addAll(BEANS.get(IJobManager.class).getFutures(new IFilter<IFuture<?>>() {

        @Override
        public boolean accept(IFuture<?> cand) {
            final RunContext candContext = cand.getJobInput().getRunContext();
            return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession();
        }
    }));
    final IRegistrationHandle listenerRegistration = IFuture.CURRENT.get().addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.WAITING_FOR_BLOCKING_CONDITION).andMatchExecutionHint(ModelJobs.EXECUTION_HINT_UI_INTERACTION_REQUIRED).toFilter(), new IJobListener() {

        @Override
        public void changed(final JobEvent event) {
            // waitFor was entered
            final IRunnable callRunnableOnceBlocked = new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        runnableOnceBlocked.run();
                    } finally {
                        event.getData().getBlockingCondition().setBlocking(false);
                        onceBlockedDoneCondition.setBlocking(false);
                    }
                }
            };
            final JobInput jobInputForRunnableOnceBlocked = ModelJobs.newInput(runContext).withExceptionHandling(BEANS.get(JUnitExceptionHandler.class), true).withName("JUnit: Handling blocked thread because waiting for a blocking condition");
            if (awaitBackgroundJobs) {
                // wait until all background jobs finished
                Jobs.schedule(new IRunnable() {

                    @Override
                    public void run() throws Exception {
                        jobsBefore.add(IFuture.CURRENT.get());
                        BEANS.get(IJobManager.class).awaitFinished(new IFilter<IFuture<?>>() {

                            @Override
                            public boolean accept(IFuture<?> f) {
                                RunContext candContext = f.getJobInput().getRunContext();
                                return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession() && !jobsBefore.contains(f);
                            }
                        }, 5, TimeUnit.MINUTES);
                        // call runnableOnceBlocked
                        ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
                    }
                }, Jobs.newInput().withName("wait until background jobs finished"));
            } else {
                // call runnableOnceBlocked directly
                ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
            }
        }
    });
    try {
        // this action will enter a blocking condition which causes the 'runnableOnceBlocked' to be executed.
        runnableGettingBlocked.run();
    } catch (final Exception e) {
        throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
    } finally {
        listenerRegistration.dispose();
    }
    // we need to wait until the runnableOnceBlocked is completed.
    // runnableOnceBlocked may, during its execution,  set the original blocking condition to non-blocking but still execute
    // important code afterwards. Therefore, the original blocking condition that starts runnableOnceBlocked is only used
    // to indicate the start of the runnableOnceBlocked, but this method returns only AFTER runnableOnceBlocked completes execution.
    onceBlockedDoneCondition.waitForUninterruptibly(120, TimeUnit.SECONDS);
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JUnitExceptionHandler(org.eclipse.scout.rt.testing.platform.runner.JUnitExceptionHandler) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) IFuture(org.eclipse.scout.rt.platform.job.IFuture) IJobListener(org.eclipse.scout.rt.platform.job.listener.IJobListener) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) JobInput(org.eclipse.scout.rt.platform.job.JobInput) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IFilter(org.eclipse.scout.rt.platform.filter.IFilter) RunContext(org.eclipse.scout.rt.platform.context.RunContext) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) HashSet(java.util.HashSet)

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