Search in sources :

Example 11 with IExecutionSemaphore

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));
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IFuture(org.eclipse.scout.rt.platform.job.IFuture) Test(org.junit.Test)

Example 12 with IExecutionSemaphore

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);
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore)

Example 13 with IExecutionSemaphore

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));
}
Also used : ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) Test(org.junit.Test)

Example 14 with IExecutionSemaphore

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();
    }
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) VetoException(org.eclipse.scout.rt.platform.exception.VetoException) Test(org.junit.Test)

Example 15 with IExecutionSemaphore

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();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Aggregations

IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)28 Test (org.junit.Test)26 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)23 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)11 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)8 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)6 IFuture (org.eclipse.scout.rt.platform.job.IFuture)5 Times (org.eclipse.scout.rt.testing.platform.runner.Times)5 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)4 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)2 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JMSException (javax.jms.JMSException)1 NamingException (javax.naming.NamingException)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1