Search in sources :

Example 6 with Times

use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.

the class ExecutionSemaphoreTest method testThreePermitsAndBlocking.

/**
 * Tests execution semaphore with 3 permits and with a blocking condition involved.
 * <p>
 * In total, 7 jobs are scheduled. Thereby, job-1 and job-3 never finish, and job-2 enters a blocking condition.
 * <p>
 * This test tests, that because job-2 enters a blocking condition, job-4 starts running. Once job-4 completed, job-5
 * starts running. Then, job-5 unblocks the conditions, with lets job-2 to continue after job-5 finished. After job-2
 * finished, job-6, and then job-7 start running.
 * <p>
 */
@Test
// regression
@Times(500)
public void testThreePermitsAndBlocking() throws InterruptedException {
    final IExecutionSemaphore semaphore = Jobs.newExecutionSemaphore(3);
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
    final BlockingCountDownLatch finishLatch = new BlockingCountDownLatch(2);
    final BlockingCountDownLatch latchJob2 = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch latchJob5 = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch latchJob6 = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch latchJob7 = new BlockingCountDownLatch(1);
    // job-1
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add("job-1-running");
            setupLatch.countDownAndBlock();
            finishLatch.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 (a)");
            condition.waitFor(30, TimeUnit.SECONDS);
            protocol.add("job-2-running (b)");
            latchJob2.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();
            finishLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-3").withExecutionSemaphore(semaphore));
    // job-4
    Jobs.schedule(new IRunnable() {

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

        @Override
        public void run() throws Exception {
            protocol.add("job-5-running");
            condition.setBlocking(false);
            // Wait until job-2 is competing for a permit anew.
            // Otherwise, job-6 might get the permit before job-2.
            // permit-owners: job-1, job-3, job-5, queue: job-2 (RE-ACQUIRE), job-6, job-7
            JobTestUtil.waitForPermitCompetitors(semaphore, 6);
            latchJob5.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-5").withExecutionSemaphore(semaphore));
    // job-6
    Jobs.schedule(new IRunnable() {

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

        @Override
        public void run() throws Exception {
            protocol.add("job-7-running");
            latchJob7.countDownAndBlock();
        }
    }, Jobs.newInput().withName("job-7").withExecutionSemaphore(semaphore));
    // verify
    assertTrue(setupLatch.await());
    assertEquals(CollectionUtility.hashSet("job-1-running", "job-2-running (a)", "job-3-running", "job-4-running"), protocol);
    protocol.clear();
    setupLatch.unblock();
    assertTrue(latchJob5.await());
    assertEquals(CollectionUtility.hashSet("job-5-running"), protocol);
    protocol.clear();
    latchJob5.unblock();
    try {
        assertTrue(latchJob2.await());
    } catch (AssertionError e) {
        System.out.println(protocol);
        throw e;
    }
    assertEquals(CollectionUtility.hashSet("job-2-running (b)"), protocol);
    protocol.clear();
    latchJob2.unblock();
    assertTrue(latchJob6.await());
    assertEquals(CollectionUtility.hashSet("job-6-running"), protocol);
    protocol.clear();
    latchJob6.unblock();
    assertTrue(latchJob7.await());
    assertEquals(CollectionUtility.hashSet("job-7-running"), protocol);
    latchJob7.unblock();
    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) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 7 with Times

use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.

the class MisfireTest method testFinalRunAfterMisfire.

/**
 * Tests that a last round is scheduled upon a 'misfire' with the end-time arrived.
 */
@Test
// regression
@Times(10)
public void testFinalRunAfterMisfire() {
    final int endsIn = 20;
    final int schedulingInterval = 1;
    // synchronized because modified/read by different threads.
    final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
    final AtomicInteger roundCounter = new AtomicInteger();
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            int round = roundCounter.incrementAndGet();
            writeProtocol("BEFORE-SLEEP", (JobFutureTask<?>) IFuture.CURRENT.get(), round, protocol);
            if (round == 1) {
                SleepUtil.sleepSafe(endsIn + 50, TimeUnit.MILLISECONDS);
            }
            writeProtocol("AFTER-SLEEP", (JobFutureTask<?>) IFuture.CURRENT.get(), round, protocol);
        }
    }, Jobs.newInput().withExecutionTrigger(Jobs.newExecutionTrigger().withEndIn(endsIn, TimeUnit.MILLISECONDS).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(schedulingInterval).repeatForever())));
    // Wait until done
    try {
        future.awaitDone(10, TimeUnit.SECONDS);
    } catch (TimedOutError e) {
        future.cancel(true);
        fail("Job is hanging because no last round scheduled upon misfire with end-time arrived");
    }
    // Verify
    List<String> expectedProtocol = new ArrayList<>();
    expectedProtocol.add("BEFORE-SLEEP: has-next-execution [round=1]");
    expectedProtocol.add("AFTER-SLEEP: has-next-execution [round=1]");
    expectedProtocol.add("BEFORE-SLEEP: final-run [round=2]");
    expectedProtocol.add("AFTER-SLEEP: final-run [round=2]");
    assertEquals(expectedProtocol, protocol);
    assertTrue(((JobFutureTask<?>) future).isFinalRun());
    assertFalse(((JobFutureTask<?>) future).hasNextExecution());
}
Also used : ArrayList(java.util.ArrayList) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 8 with Times

use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.

the class JmsMomImplementorTest method testQueueRequestReplyRequestFirst.

@Test(timeout = 200_000)
// regression
@Times(10)
public void testQueueRequestReplyRequestFirst() throws InterruptedException {
    final IBiDestination<String, String> queue = MOM.newBiDestination("test/mom/testQueueRequestReplyRequestFirst", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    // 1. Initiate 'request-reply' communication
    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 (Q)").withExecutionHint(m_testJobExecutionHint));
    // Wait some time to give request message publisher time to send message
    // If we wait not long enough, we get a false positive
    Thread.sleep(300);
    // 2. Subscribe for reply
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            // Subscribe replier
            m_disposables.add(MOM.reply(JmsTestMom.class, queue, new IRequestListener<String, String>() {

                @Override
                public String onRequest(IMessage<String> request) {
                    return request.getTransferObject().toUpperCase();
                }
            }));
        }
    }, Jobs.newInput().withName("replier (Q)").withExecutionHint(m_testJobExecutionHint));
    String testee = requestFuture.awaitDoneAndGet(10, TimeUnit.SECONDS);
    // Verify
    assertEquals("HELLO WORLD", testee);
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) 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) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 9 with Times

use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.

the class JmsMomImplementorTest method testTopicPublishFirst.

@Test
// regression
@Times(10)
public void testTopicPublishFirst() throws InterruptedException {
    IDestination<String> topic = MOM.newDestination("test/mom/testTopicPublishFirst", DestinationType.TOPIC, ResolveMethod.DEFINE, null);
    // Publish a message
    MOM.publish(JmsTestMom.class, topic, "hello world");
    // Subscribe for the destination
    final CountDownLatch latch = new CountDownLatch(1);
    m_disposables.add(MOM.subscribe(JmsTestMom.class, topic, new IMessageListener<String>() {

        @Override
        public void onMessage(IMessage<String> message) {
            latch.countDown();
        }
    }));
    // Verify
    assertFalse(latch.await(200, TimeUnit.MILLISECONDS));
}
Also used : IMessage(org.eclipse.scout.rt.mom.api.IMessage) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 10 with Times

use of org.eclipse.scout.rt.testing.platform.runner.Times in project scout.rt by eclipse.

the class JmsMomImplementorTest method testTimeToLive.

@Test
@Times(10)
public void testTimeToLive() throws InterruptedException {
    IDestination<String> queue = MOM.newDestination("test/mom/testTimeToLive", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    final CountDownLatch latch = new CountDownLatch(1);
    // Publish a message
    MOM.publish(JmsTestMom.class, queue, "hello world", MOM.newPublishInput().withTimeToLive(1, TimeUnit.MILLISECONDS));
    Thread.sleep(100);
    // Subscribe for the destination
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {

        @Override
        public void onMessage(IMessage<String> message) {
            latch.countDown();
        }
    }));
    // Verify
    // expect the message not to be received
    assertFalse(latch.await(50, TimeUnit.MILLISECONDS));
}
Also used : IMessage(org.eclipse.scout.rt.mom.api.IMessage) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) 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