Search in sources :

Example 51 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JmsMomImplementorTest method testSerialMessageConsumption.

@Test
public void testSerialMessageConsumption() throws InterruptedException {
    IDestination<Object> queue = MOM.newDestination("test/mom/testSerialMessageConsumption", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    // 1. Publish some messages
    int msgCount = 10;
    for (int i = 0; i < msgCount; i++) {
        MOM.publish(JmsTestMom.class, queue, "hello");
    }
    // 2. Consume the messages
    final BlockingCountDownLatch latch = new BlockingCountDownLatch(msgCount, 3, TimeUnit.SECONDS);
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<Object>() {

        @Override
        public void onMessage(IMessage<Object> message) {
            try {
                // timeout must be greater than the default latch timeout
                latch.countDownAndBlock(1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
            }
        }
    }, MOM.newSubscribeInput().withAcknowledgementMode(SubscribeInput.ACKNOWLEDGE_AUTO_SINGLE_THREADED)));
    try {
        assertFalse("messages not expected to be consumed concurrently", latch.await());
        assertEquals("one message expected to be consumed", msgCount - 1, latch.getCount());
    } finally {
        latch.unblock();
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IMessage(org.eclipse.scout.rt.mom.api.IMessage) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test)

Example 52 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JmsMomImplementorTest method testRequestReplyTimeoutInternal.

private void testRequestReplyTimeoutInternal(final IBiDestination<String, String> destination) throws InterruptedException {
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(2);
    final AtomicBoolean requestorTimedOut = new AtomicBoolean();
    final AtomicBoolean replierInterrupted = new AtomicBoolean();
    // Subscribe for the destination
    m_disposables.add(MOM.reply(JmsTestMom.class, destination, new IRequestListener<String, String>() {

        @Override
        public String onRequest(IMessage<String> request) {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                replierInterrupted.set(true);
            } finally {
                verifyLatch.countDown();
            }
            return request.getTransferObject().toUpperCase();
        }
    }));
    // Initiate 'request-reply' communication
    try {
        MOM.request(JmsTestMom.class, destination, "hello world", MOM.newPublishInput().withRequestReplyTimeout(1, TimeUnit.SECONDS));
    } catch (TimedOutError e) {
        requestorTimedOut.set(true);
    } finally {
        verifyLatch.countDown();
    }
    assertTrue(verifyLatch.await());
    // Verify
    assertTrue(requestorTimedOut.get());
    assertTrue(replierInterrupted.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IRequestListener(org.eclipse.scout.rt.mom.api.IRequestListener) IMessage(org.eclipse.scout.rt.mom.api.IMessage) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)

Example 53 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JmsMomImplementorTest method testConcurrentMessageConsumption.

@Test
public void testConcurrentMessageConsumption() throws InterruptedException {
    IDestination<Object> queue = MOM.newDestination("test/mom/testConcurrentMessageConsumption", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    // 1. Publish some messages
    int msgCount = 10;
    for (int i = 0; i < msgCount; i++) {
        MOM.publish(JmsTestMom.class, queue, "hello");
    }
    // 1. Publish some messages
    final BlockingCountDownLatch latch = new BlockingCountDownLatch(msgCount, 3, TimeUnit.SECONDS);
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<Object>() {

        @Override
        public void onMessage(IMessage<Object> message) {
            try {
                // timeout must be greater than the default latch timeout
                latch.countDownAndBlock(1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
            }
        }
    }));
    try {
        assertTrue("messages expected to be consumed concurrently", latch.await());
    } finally {
        latch.unblock();
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) IMessage(org.eclipse.scout.rt.mom.api.IMessage) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test)

Example 54 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobStateTest method testRunningAndDone.

@Test
public void testRunningAndDone() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final BlockingCountDownLatch runningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            runningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(runningLatch.await());
    assertEquals(JobState.RUNNING, future.getState());
    runningLatch.unblock();
    future.awaitDone(5, TimeUnit.SECONDS);
    assertEquals(JobState.DONE, future.getState());
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 55 with BlockingCountDownLatch

use of org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch in project scout.rt by eclipse.

the class JobStateTest method testPending.

@Test
public void testPending() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final BlockingCountDownLatch runningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            runningLatch.countDownAndBlock();
        }
    }, Jobs.newInput().withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(2, TimeUnit.SECONDS)));
    JobTestUtil.waitForState(future, JobState.PENDING);
    assertEquals(JobState.PENDING, future.getState());
    assertTrue(runningLatch.await());
    assertEquals(JobState.RUNNING, future.getState());
    runningLatch.unblock();
    future.awaitDone(5, TimeUnit.SECONDS);
    assertEquals(JobState.DONE, future.getState());
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Aggregations

BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)93 Test (org.junit.Test)89 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)66 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)30 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)14 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)12 ArrayList (java.util.ArrayList)10 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)10 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)9 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)9 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)7 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)7 Times (org.eclipse.scout.rt.testing.platform.runner.Times)7 Holder (org.eclipse.scout.rt.platform.holders.Holder)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 IMessage (org.eclipse.scout.rt.mom.api.IMessage)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IMessageListener (org.eclipse.scout.rt.mom.api.IMessageListener)3 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)3