Search in sources :

Example 21 with IExecutionSemaphore

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)));
}
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 22 with IExecutionSemaphore

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));
}
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 23 with IExecutionSemaphore

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));
}
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 24 with IExecutionSemaphore

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

Example 25 with IExecutionSemaphore

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

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