use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore 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)));
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class FutureFilterBuilderTest 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().withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(mutex));
IFuture<?> future7 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(mutex));
IFuture<?> future8 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(mutex));
IFuture<?> future9 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(mutex));
IFuture<?> future10 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionHint(JOB_IDENTIFIER).withExecutionSemaphore(mutex));
// One future exclusion with not other criteria
IFilter<IFuture<?>> filter = Jobs.newFutureFilterBuilder().andMatchNotFuture(future8).toFilter();
assertTrue(filter.accept(future1));
assertTrue(filter.accept(future2));
assertTrue(filter.accept(future3));
assertTrue(filter.accept(future4));
assertTrue(filter.accept(future5));
assertTrue(filter.accept(future6));
assertTrue(filter.accept(future7));
assertFalse(filter.accept(future8));
assertTrue(filter.accept(future9));
assertTrue(filter.accept(future10));
// Multiple future exclusions with not other criteria
filter = Jobs.newFutureFilterBuilder().andMatchNotFuture(future8, future9).toFilter();
assertTrue(filter.accept(future1));
assertTrue(filter.accept(future2));
assertTrue(filter.accept(future3));
assertTrue(filter.accept(future4));
assertTrue(filter.accept(future5));
assertTrue(filter.accept(future6));
assertTrue(filter.accept(future7));
assertFalse(filter.accept(future8));
assertFalse(filter.accept(future9));
assertTrue(filter.accept(future10));
// One future exclusion with other criterion (mutex)
filter = Jobs.newFutureFilterBuilder().andMatchExecutionSemaphore(mutex).andMatchNotFuture(future8).toFilter();
assertFalse(filter.accept(future1));
assertFalse(filter.accept(future2));
assertFalse(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertTrue(filter.accept(future6));
assertTrue(filter.accept(future7));
assertFalse(filter.accept(future8));
assertTrue(filter.accept(future9));
assertTrue(filter.accept(future10));
// Multiple future exclusion with other criterion (mutex)
filter = Jobs.newFutureFilterBuilder().andMatchExecutionSemaphore(mutex).andMatchNotFuture(future8, future9).toFilter();
assertFalse(filter.accept(future1));
assertFalse(filter.accept(future2));
assertFalse(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertTrue(filter.accept(future6));
assertTrue(filter.accept(future7));
assertFalse(filter.accept(future8));
assertFalse(filter.accept(future9));
assertTrue(filter.accept(future10));
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class MutexFutureFilterTest method test.
@Test
public void test() {
IExecutionSemaphore mutex1 = Jobs.newExecutionSemaphore(1);
IExecutionSemaphore mutex2 = Jobs.newExecutionSemaphore(1);
IFuture<Void> future1 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex1));
IFuture<Void> future2 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex1));
IFuture<Void> future3 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withExecutionSemaphore(mutex2));
IFilter<IFuture<?>> filter = new ExecutionSemaphoreFutureFilter(mutex1);
assertTrue(filter.accept(future1));
assertTrue(filter.accept(future2));
assertFalse(filter.accept(future3));
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class PermitAcquisitionOrderTest method testOrderIfStartingAtSameTime.
/**
* Tests that schedule aware jobs scheduled in sequence but start at a specific point in time respect the scheduling
* order when acquiring a permit.
*/
@Test
public void testOrderIfStartingAtSameTime() {
// regression
int regressionCount = 100;
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final String jobIdentifier = UUID.randomUUID().toString();
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1);
final Date date = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(2));
// Schedule 100 jobs to start at the same time
for (int i = 0; i < regressionCount; i++) {
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
protocol.add(IFuture.CURRENT.get().getJobInput().getName());
}
}, Jobs.newInput().withName("job-{}", i).withExecutionSemaphore(semaphore).withExecutionTrigger(Jobs.newExecutionTrigger().withStartAt(date)).withExecutionHint(jobIdentifier));
}
Jobs.getJobManager().awaitDone(Jobs.newFutureFilterBuilder().andMatchExecutionHint(jobIdentifier).toFilter(), 10, TimeUnit.SECONDS);
List<String> expectedProtocol = new ArrayList<>();
for (int i = 0; i < regressionCount; i++) {
expectedProtocol.add("job-" + i);
}
assertEquals(expectedProtocol, protocol);
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class ThreadNameDecoratorTest method testThreadName.
@Test
public void testThreadName() throws InterruptedException {
final AtomicReference<Thread> workerThread = new AtomicReference<>();
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
final BlockingCountDownLatch latch1 = new BlockingCountDownLatch(1);
final BlockingCountDownLatch latch2 = new BlockingCountDownLatch(1);
IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
workerThread.set(Thread.currentThread());
latch1.countDownAndBlock();
condition.waitFor(10, TimeUnit.SECONDS);
latch2.countDownAndBlock();
}
}, Jobs.newInput().withExecutionSemaphore(semaphore).withThreadName("test-thread").withName("job-1"));
// Test while running
assertTrue(latch1.await());
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
latch1.unblock();
// Test while blocked
JobTestUtil.waitForPermitCompetitors(semaphore, 0);
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
// Test while waiting for permit
semaphore.withPermits(0);
condition.setBlocking(false);
JobTestUtil.waitForPermitCompetitors(semaphore, 1);
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
// Test while running
semaphore.withPermits(1);
assertTrue(latch2.await());
assertTrue("actual=" + workerThread.get().getName(), m_threadNamePattern.matcher(workerThread.get().getName()).matches());
latch2.unblock();
}
Aggregations