Search in sources :

Example 16 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.

the class ExecutionSemaphoreTest method testChangePermits2.

/**
 * Tests no permit available upon schedule
 */
@Test
public void testChangePermits2() throws InterruptedException {
    IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(10);
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch latch = new BlockingCountDownLatch(1);
    // job-1
    IFuture<Void> future1 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-1-running");
            latch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-1").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
    assertTrue(latch.await());
    assertEquals(CollectionUtility.hashSet("job-1-running"), protocol);
    protocol.clear();
    // Change permits to zero.
    semaphore.withPermits(0);
    latch.unblock();
    future1.awaitDone();
    // job-2
    IFuture<Void> future2 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-2-running");
        }
    }, Jobs.newInput().withName("job-2").withExecutionSemaphore(semaphore));
    SleepUtil.sleepSafe(1, TimeUnit.SECONDS);
    assertTrue(protocol.isEmpty());
    assertFalse(future2.isDone());
    // Change permits to 1 --> job should run
    semaphore.withPermits(1);
    future2.awaitDone(10, TimeUnit.SECONDS);
    assertEquals(CollectionUtility.hashSet("job-2-running"), protocol);
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 17 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.

the class ExecutionSemaphoreTest method testZeroPermits.

@Test
public void testZeroPermits() {
    IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(0);
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-running");
        }
    }, Jobs.newInput().withName("job").withExecutionSemaphore(semaphore));
    SleepUtil.sleepSafe(1, TimeUnit.SECONDS);
    assertTrue(protocol.isEmpty());
    assertFalse(future.isDone());
    // Change permits to 1 --> job should start running
    semaphore.withPermits(1);
    future.awaitDone(10, TimeUnit.SECONDS);
    assertEquals(CollectionUtility.hashSet("job-running"), protocol);
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 18 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.

the class ExecutionSemaphoreTest method testSealSemaphore.

@Test(expected = AssertionException.class)
public void testSealSemaphore() {
    IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(1).seal();
    semaphore.withPermits(2);
}
Also used : IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) Test(org.junit.Test)

Example 19 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.

the class ExecutionSemaphoreTest method testChangePermits4.

/**
 * Test with change of permits to serial execution.
 */
@Test
// regression
@Times(500)
public void testChangePermits4() throws InterruptedException {
    IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(3);
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
    // job-1
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-1-running");
            setupLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-1").withExecutionSemaphore(semaphore));
    // job-2
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-2-running");
            setupLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-2").withExecutionSemaphore(semaphore));
    // job-3
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-3-running");
            setupLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-3").withExecutionSemaphore(semaphore));
    // job-4
    final BlockingCountDownLatch latchJob4 = new BlockingCountDownLatch(1);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-4-running");
            latchJob4.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-4").withExecutionSemaphore(semaphore));
    // job-5
    final BlockingCountDownLatch latchJob5 = new BlockingCountDownLatch(1);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-5-running");
            latchJob5.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-5").withExecutionSemaphore(semaphore));
    // job-6
    final BlockingCountDownLatch latchJob6 = new BlockingCountDownLatch(1);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-6-running");
            latchJob6.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-6").withExecutionSemaphore(semaphore));
    assertTrue(setupLatch.await());
    assertEquals(CollectionUtility.hashSet("job-1-running", "job-2-running", "job-3-running"), protocol);
    protocol.clear();
    // serial execution
    semaphore.withPermits(1);
    setupLatch.unblock();
    latchJob4.await();
    assertEquals(CollectionUtility.hashSet("job-4-running"), protocol);
    protocol.clear();
    latchJob4.unblock();
    latchJob5.await();
    assertEquals(CollectionUtility.hashSet("job-5-running"), protocol);
    protocol.clear();
    latchJob5.unblock();
    latchJob6.await();
    assertEquals(CollectionUtility.hashSet("job-6-running"), protocol);
    protocol.clear();
    latchJob6.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 20 with IExecutionSemaphore

use of org.eclipse.scout.rt.platform.job.IExecutionSemaphore in project scout.rt by eclipse.

the class ExecutionSemaphoreTest method testChangePermits3.

/**
 * Tests multiple permits available after permit change.
 */
@Test
public void testChangePermits3() throws InterruptedException {
    IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(0);
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch finishLatch = new BlockingCountDownLatch(3);
    // job-1
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-1-running");
            finishLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-1").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
    // job-2
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-2-running");
            finishLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-2").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
    // job-3
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-3-running");
            finishLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-3").withExceptionHandling(null, true).withExecutionSemaphore(semaphore));
    // job-1, job-2, job-3
    JobTestUtil.waitForPermitCompetitors(semaphore, 3);
    semaphore.withPermits(3);
    assertTrue(finishLatch.await());
    assertEquals(CollectionUtility.hashSet("job-1-running", "job-2-running", "job-3-running"), protocol);
    finishLatch.unblock();
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IExecutionSemaphore(org.eclipse.scout.rt.platform.job.IExecutionSemaphore) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) 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