Search in sources :

Example 11 with Times

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

the class JmsMomImplementorTest method testQueuePublishSubscribeMultipleSubscriptions.

@Test
// regression
@Times(10)
public void testQueuePublishSubscribeMultipleSubscriptions() throws InterruptedException {
    IDestination<String> queue = MOM.newDestination("test/mom/testQueuePublishSubscribeMultipleSubscriptions", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    final AtomicInteger msgCounter = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(1);
    // Subscribe for the destination
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {

        @Override
        public void onMessage(IMessage<String> message) {
            msgCounter.incrementAndGet();
            latch.countDown();
        }
    }));
    // Subscribe for the destination
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {

        @Override
        public void onMessage(IMessage<String> message) {
            msgCounter.incrementAndGet();
            latch.countDown();
        }
    }));
    // Publish a message
    MOM.publish(JmsTestMom.class, queue, "hello world");
    // Verify
    latch.await(5, TimeUnit.SECONDS);
    Thread.sleep(50);
    assertEquals(1, msgCounter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 12 with Times

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

the class JmsMomImplementorTest method testQueueRequestReplyMultipleSubscriptions.

@Test(timeout = 200_000)
// regression
@Times(10)
public void testQueueRequestReplyMultipleSubscriptions() throws InterruptedException {
    IBiDestination<String, String> queue = MOM.newBiDestination("test/mom/testQueueRequestReplyMultipleSubscriptions", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    final CountDownLatch msgLatch = new CountDownLatch(2);
    // Subscribe for the destination
    m_disposables.add(MOM.reply(JmsTestMom.class, queue, new IRequestListener<String, String>() {

        @Override
        public String onRequest(IMessage<String> request) {
            msgLatch.countDown();
            return request.getTransferObject().toUpperCase();
        }
    }));
    // Subscribe for the destination
    m_disposables.add(MOM.reply(JmsTestMom.class, queue, new IRequestListener<String, String>() {

        @Override
        public String onRequest(IMessage<String> request) {
            msgLatch.countDown();
            return request.getTransferObject().toUpperCase();
        }
    }));
    // Initiate 'request-reply' communication
    String testee = MOM.request(JmsTestMom.class, queue, "hello world");
    // Verify
    assertEquals("HELLO WORLD", testee);
    assertFalse(msgLatch.await(50, TimeUnit.MILLISECONDS));
}
Also used : IRequestListener(org.eclipse.scout.rt.mom.api.IRequestListener) IMessage(org.eclipse.scout.rt.mom.api.IMessage) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 13 with Times

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

the class JmsMomImplementorTest method testQueuePublishFirst.

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

        @Override
        public void onMessage(IMessage<String> message) {
            latch.countDown();
        }
    }));
    // Verify
    assertTrue(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 14 with Times

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

the class JobStateTest method testBlockedAndInterrupted.

@Test
// regression
@Times(100)
public void testBlockedAndInterrupted() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final AtomicReference<Thread> workerThread = new AtomicReference<>();
    final IFuture<Void> future1 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            workerThread.set(Thread.currentThread());
            assertSame(JobState.RUNNING, IFuture.CURRENT.get().getState());
            try {
                condition.waitFor("ABC");
                fail("interruption expected");
            } catch (ThreadInterruptedError e) {
                assertTrue(Thread.interrupted());
                // Restore interrupted status
                Thread.currentThread().interrupt();
                assertTrue(mutex.isPermitOwner(IFuture.CURRENT.get()));
                assertSame(JobState.RUNNING, IFuture.CURRENT.get().getState());
            }
        }
    }, Jobs.newInput().withName("job-1").withExecutionSemaphore(mutex));
    // Wait until job-1 is running
    JobTestUtil.waitForState(future1, JobState.WAITING_FOR_BLOCKING_CONDITION);
    // Interrupt worker thread
    workerThread.get().interrupt();
    future1.awaitDoneAndGet();
    assertTrue(future1.isDone());
    assertFalse(future1.isCancelled());
    // wait because permit is released just after done (max 30s)
    JobTestUtil.waitForPermitCompetitors(mutex, 0);
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future1, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertHintChangedEvent(JobEventType.JOB_EXECUTION_HINT_ADDED, future1, "ABC", capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertBlockedStateEvent(future1, condition, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertHintChangedEvent(JobEventType.JOB_EXECUTION_HINT_REMOVED, future1, "ABC", capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    // due to interruption
    assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) AtomicReference(java.util.concurrent.atomic.AtomicReference) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) Test(org.junit.Test) Times(org.eclipse.scout.rt.testing.platform.runner.Times)

Example 15 with Times

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

the class JobStateTest method testBlockedAndTimeout.

@Test
// regression
@Times(100)
public void testBlockedAndTimeout() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
    final IBlockingCondition condition = Jobs.newBlockingCondition(true);
    final AtomicReference<Thread> workerThread = new AtomicReference<>();
    final IFuture<Void> future1 = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            workerThread.set(Thread.currentThread());
            assertSame(JobState.RUNNING, IFuture.CURRENT.get().getState());
            try {
                condition.waitFor(1, TimeUnit.MILLISECONDS, "ABC");
                fail("timeout expected");
            } catch (TimedOutError e) {
                assertTrue(mutex.isPermitOwner(IFuture.CURRENT.get()));
                assertSame(JobState.RUNNING, IFuture.CURRENT.get().getState());
            }
        }
    }, Jobs.newInput().withName("job-1").withExecutionSemaphore(mutex));
    // Wait until job-1 completed
    future1.awaitDoneAndGet(10, TimeUnit.SECONDS);
    assertTrue(future1.isDone());
    assertFalse(future1.isCancelled());
    // wait because permit is released just after done (max 30s)
    JobTestUtil.waitForPermitCompetitors(mutex, 0);
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future1, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertHintChangedEvent(JobEventType.JOB_EXECUTION_HINT_ADDED, future1, "ABC", capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertBlockedStateEvent(future1, condition, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_BLOCKING_CONDITION, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    assertHintChangedEvent(JobEventType.JOB_EXECUTION_HINT_REMOVED, future1, "ABC", capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    i++;
    // due to timeout
    assertStateChangedEvent(future1, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future1, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) 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