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();
}
}
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());
}
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();
}
}
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());
}
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());
}
Aggregations