Search in sources :

Example 16 with Times

use of org.eclipse.scout.rt.testing.platform.runner.Times 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)

Example 17 with Times

use of org.eclipse.scout.rt.testing.platform.runner.Times 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)

Aggregations

Times (org.eclipse.scout.rt.testing.platform.runner.Times)17 Test (org.junit.Test)17 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)12 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)12 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 IMessage (org.eclipse.scout.rt.mom.api.IMessage)5 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 IMessageListener (org.eclipse.scout.rt.mom.api.IMessageListener)4 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)4 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)4 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JMSException (javax.jms.JMSException)1 NamingException (javax.naming.NamingException)1