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)));
}
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())));
}
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())));
}
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));
}
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);
}
Aggregations