use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class JmsMomImplementorTest method testRequestReplyWithBlockingCondition.
@Test(timeout = 200_000)
public void testRequestReplyWithBlockingCondition() throws InterruptedException {
final IBiDestination<String, String> queue = MOM.newBiDestination("test/mom/testRequestReplyWithBlockingCondition", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
// semaphore for jobs
IExecutionSemaphore mutex = Jobs.newExecutionSemaphore(1);
// request should block with IBlockingCondition.waitFor in order to release semaphore
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 (T)").withExecutionHint(m_testJobExecutionHint).withExceptionHandling(null, false).withExecutionSemaphore(mutex));
// test if semaphore was released (with waitFor)
IFuture<Void> otherFuture = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
// nop
}
}, Jobs.newInput().withName("null job").withExecutionHint(m_testJobExecutionHint).withExecutionSemaphore(mutex));
// Verify
try {
otherFuture.awaitDone(2000, TimeUnit.MILLISECONDS);
assertSame(JobState.WAITING_FOR_BLOCKING_CONDITION, requestFuture.getState());
} catch (TimedOutError e) {
fail();
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError 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.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class JmsMomImplementorTest method after.
@After
public void after() throws Exception {
// Dispose resources
dispose(m_disposables);
// Cancel jobs
IFilter<IFuture<?>> testJobsFilter = Jobs.newFutureFilterBuilder().andMatchExecutionHint(m_testJobExecutionHint).toFilter();
Set<IFuture<?>> futures = Jobs.getJobManager().getFutures(testJobsFilter);
if (futures.size() > 0) {
LOG.info("Cancelling {} jobs: {}", futures.size(), futures);
Jobs.getJobManager().cancel(Jobs.newFutureFilterBuilder().andMatchFuture(futures).andMatchNotState(JobState.DONE).toFilter(), true);
long t0 = System.nanoTime();
try {
Jobs.getJobManager().awaitDone(testJobsFilter, 10, TimeUnit.SECONDS);
LOG.info("All jobs have finished after {} ms", StringUtility.formatNanos(System.nanoTime() - t0));
} catch (TimedOutError e) {
LOG.warn("Some cancelled jobs are still running after {} ms! Please check their implementation.", StringUtility.formatNanos(System.nanoTime() - t0));
}
}
uninstallTestMom();
// ensure activeMQ is stopped
BrokerService brokerService = BrokerRegistry.getInstance().findFirst();
if (brokerService != null) {
brokerService.stop();
brokerService.waitUntilStopped();
}
LOG.info("Finished test in {} ms", StringUtility.formatNanos(System.nanoTime() - m_t0));
LOG.info("</{}>", m_testName.getMethodName());
}
use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class JmsMomImplementor method request.
@Override
public <REQUEST, REPLY> REPLY request(final IBiDestination<REQUEST, REPLY> destination, final REQUEST requestObject, final PublishInput input) {
assertTrue(m_requestReplyEnabled, "'request-reply' messaging is not enabled for this MOM");
assertNotNull(destination, "destination not specified");
assertNotNull(input, "publishInput not specified");
assertFalse(input.isTransactional(), "transactional mode not supported for 'request-reply' communication");
// JMS message ID not applicable because unknown until sent
final String replyId = String.format("scout.mom.requestreply.uid-%s", UUID.randomUUID());
final IBlockingCondition condition = Jobs.newBlockingCondition(true);
IFuture<Message> requestFuture = Jobs.schedule(new Callable<Message>() {
@Override
public Message call() throws Exception {
try {
return requestImpl(destination, requestObject, input, replyId);
} finally {
condition.setBlocking(false);
}
}
}, newJobInput().withName("request on {}", destination.getName()).withExceptionHandling(BEANS.get(MomExceptionHandler.class), false).withRunContext(RunContexts.copyCurrent(true).withDiagnostics(BEANS.all(IJmsRunContextDiagnostics.class))));
try {
long timeout = input.getRequestReplyTimeout();
if (timeout == PublishInput.INFINITELY) {
condition.waitFor();
} else {
condition.waitFor(timeout, TimeUnit.MILLISECONDS);
}
Message responseMessage = requestFuture.awaitDoneAndGet();
return transform(responseMessage, replyId, resolveMarshaller(destination));
} catch (JMSException e) {
throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
} catch (ThreadInterruptedError | TimedOutError e) {
// send cancel to replier
cancelRequest(replyId);
// cancel request job
if (requestFuture.cancel(true)) {
requestFuture.awaitDone();
}
throw e;
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError 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());
}
Aggregations