use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class FutureFilterBuilderTest 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).withExecutionHint(JOB_IDENTIFIER));
IFuture<?> future10 = Jobs.schedule(mock(IRunnable.class), Jobs.newInput().withName("I").withRunContext(new P_RunContext()).withExecutionSemaphore(mutex1).withExecutionHint(JOB_IDENTIFIER));
assertTrue(new FutureFilterBuilder().toFilter().accept(future1));
assertTrue(new FutureFilterBuilder().toFilter().accept(future2));
assertTrue(new FutureFilterBuilder().toFilter().accept(future3));
assertTrue(new FutureFilterBuilder().toFilter().accept(future4));
assertTrue(new FutureFilterBuilder().toFilter().accept(future5));
assertTrue(new FutureFilterBuilder().toFilter().accept(future6));
assertTrue(new FutureFilterBuilder().toFilter().accept(future7));
assertTrue(new FutureFilterBuilder().toFilter().accept(future8));
assertTrue(new FutureFilterBuilder().toFilter().accept(future9));
assertTrue(new FutureFilterBuilder().toFilter().accept(future10));
// with filtering for futures
IFilter<IFuture<?>> filter = new FutureFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).toFilter();
assertTrue(filter.accept(future1));
assertTrue(filter.accept(future2));
assertTrue(filter.accept(future3));
assertTrue(filter.accept(future4));
assertFalse(filter.accept(future5));
assertFalse(filter.accept(future6));
assertFalse(filter.accept(future7));
assertTrue(filter.accept(future8));
assertTrue(filter.accept(future9));
assertTrue(filter.accept(future10));
// additionally with filtering for single executing jobs
filter = new FutureFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().toFilter();
assertTrue(filter.accept(future1));
assertTrue(filter.accept(future2));
assertTrue(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertFalse(filter.accept(future6));
assertFalse(filter.accept(future7));
assertTrue(filter.accept(future8));
assertTrue(filter.accept(future9));
assertTrue(filter.accept(future10));
// additionally with filtering for mutex
filter = new FutureFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).toFilter();
assertFalse(filter.accept(future1));
assertTrue(filter.accept(future2));
assertTrue(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertFalse(filter.accept(future6));
assertFalse(filter.accept(future7));
assertTrue(filter.accept(future8));
assertFalse(filter.accept(future9));
assertTrue(filter.accept(future10));
// additionally with filtering for jobs running on behalf of a RunContext
filter = new FutureFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(RunContext.class).toFilter();
assertFalse(filter.accept(future1));
assertTrue(filter.accept(future2));
assertTrue(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertFalse(filter.accept(future6));
assertFalse(filter.accept(future7));
assertFalse(filter.accept(future8));
assertFalse(filter.accept(future9));
assertTrue(filter.accept(future10));
// additionally with filtering for jobs running on behalf of a specific P_RunContext
filter = new FutureFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(P_RunContext.class).toFilter();
assertFalse(filter.accept(future1));
assertFalse(filter.accept(future2));
assertTrue(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertFalse(filter.accept(future6));
assertFalse(filter.accept(future7));
assertFalse(filter.accept(future8));
assertFalse(filter.accept(future9));
assertTrue(filter.accept(future10));
// additionally with filtering for names
filter = new FutureFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(P_RunContext.class).andMatchName("A", "B", "C").toFilter();
assertFalse(filter.accept(future1));
assertFalse(filter.accept(future2));
assertTrue(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertFalse(filter.accept(future6));
assertFalse(filter.accept(future7));
assertFalse(filter.accept(future8));
assertFalse(filter.accept(future9));
assertFalse(filter.accept(future10));
// additionally with filtering for other names
filter = new FutureFilterBuilder().andMatchFuture(future1, future2, future3, future4, future8, future9, future10).andAreSingleExecuting().andMatchExecutionSemaphore(mutex1).andMatchRunContext(P_RunContext.class).andMatchName("D", "E", "F").toFilter();
assertFalse(filter.accept(future1));
assertFalse(filter.accept(future2));
assertFalse(filter.accept(future3));
assertFalse(filter.accept(future4));
assertFalse(filter.accept(future5));
assertFalse(filter.accept(future6));
assertFalse(filter.accept(future7));
assertFalse(filter.accept(future8));
assertFalse(filter.accept(future9));
assertFalse(filter.accept(future10));
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class JobFutureTask method assertNotSameSemaphore.
/**
* Asserts that the current job (if present) is not assigned to the same {@link IExecutionSemaphore} as the job to be
* awaited for. Otherwise, that could end up in a deadlock.
*/
protected void assertNotSameSemaphore() {
final IFuture<?> currentFuture = IFuture.CURRENT.get();
if (currentFuture == null) {
// not running in a job.
return;
}
final IExecutionSemaphore currentSemaphore = currentFuture.getJobInput().getExecutionSemaphore();
if (currentSemaphore == null) {
// current job has no maximal concurrency restriction.
return;
}
if (!currentSemaphore.isPermitOwner(currentFuture)) {
// current job is not permit owner.
return;
}
if (isDone()) {
// job already in done state.
return;
}
Assertions.assertNotSame(currentSemaphore, m_executionSemaphore, "Potential deadlock detected: Cannot wait for a job which is assigned to the same semaphore as the current job [semaphore={}]", currentSemaphore);
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class ServerRunContextFutureFilterTest method testMutualExclusion.
@Test
public void testMutualExclusion() {
IExecutionSemaphore mutex1 = Jobs.newExecutionSemaphore(1);
IExecutionSemaphore mutex2 = Jobs.newExecutionSemaphore(1);
m_serverJobFuture.getJobInput().withExecutionSemaphore(mutex1);
assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).toFilter().accept(m_serverJobFuture));
assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).andMatchExecutionSemaphore(null).toFilter().accept(m_serverJobFuture));
assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).andMatchExecutionSemaphore(mutex1).toFilter().accept(m_serverJobFuture));
assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).andMatchExecutionSemaphore(mutex2).toFilter().accept(m_serverJobFuture));
m_serverJobFuture.getJobInput().withExecutionSemaphore(null);
assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).toFilter().accept(m_serverJobFuture));
assertTrue(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).andMatchExecutionSemaphore(null).toFilter().accept(m_serverJobFuture));
assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).andMatchExecutionSemaphore(mutex1).toFilter().accept(m_serverJobFuture));
assertFalse(Jobs.newFutureFilterBuilder().andMatchRunContext(ServerRunContext.class).andMatchExecutionSemaphore(mutex2).toFilter().accept(m_serverJobFuture));
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class JmsMomImplementorTest method testRequestReplyWithBlockingCondition.
@Test(timeout = 200_000)
public void testRequestReplyWithBlockingCondition() throws InterruptedException {
final IBiDestination<String, String> queue = MOM.newBiDestination("test/mom/testRequestReplyWithBlockingCondition", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
// semaphore for jobs
IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
// request should block with IBlockingCondition.waitFor in order to release semaphore
IFuture<String> requestFuture = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
return MOM.request(JmsTestMom.class, queue, "hello world");
}
}, Jobs.newInput().withName("requester (T)").withExecutionHint(m_testJobExecutionHint).withExceptionHandling(null, false).withExecutionSemaphore(mutex));
// test if semaphore was released (with waitFor)
IFuture<Void> otherFuture = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
// nop
}
}, Jobs.newInput().withName("null job").withExecutionHint(m_testJobExecutionHint).withExecutionSemaphore(mutex));
// Verify
try {
otherFuture.awaitDone(2000, TimeUnit.MILLISECONDS);
assertSame(JobState.WAITING_FOR_BLOCKING_CONDITION, requestFuture.getState());
} catch (TimedOutError e) {
fail();
}
}
use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.
the class DevelopmentThreadNameDecoratorTest method testThreadName.
@Test
// regression; do no remove
@Times(100)
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(), workerThread.get().getName().matches("test-thread-\\d+ job-1"));
latch1.unblock();
// Test while blocked
JobTestUtil.waitForPermitCompetitors(semaphore, 0);
assertTrue("actual=" + workerThread.get().getName(), workerThread.get().getName().matches("test-thread-\\d+ \\(WAITING_FOR_BLOCKING_CONDITION\\) job-1"));
// Test while waiting for permit
semaphore.withPermits(0);
condition.setBlocking(false);
JobTestUtil.waitForPermitCompetitors(semaphore, 1);
assertTrue("actual=" + workerThread.get().getName(), workerThread.get().getName().matches("test-thread-\\d+ \\(WAITING_FOR_PERMIT\\) job-1"));
// Test while running
semaphore.withPermits(1);
assertTrue(latch2.await());
assertTrue("actual=" + workerThread.get().getName(), workerThread.get().getName().matches("test-thread-\\d+ job-1"));
latch2.unblock();
}
Aggregations