use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class JmsMomImplementorTest method testSubscriptionDisposeSynchronized.
/**
* In case of single threaded subscription, {@link ISubscription#dispose()} waits for any ongoing processing of this
* subscription to finish.
*/
private void testSubscriptionDisposeSynchronized(SubscribeInput subscribeInput) throws InterruptedException {
final Capturer<String> capturer = new Capturer<>();
final CountDownLatch latch = new CountDownLatch(1);
IDestination<String> queue = MOM.newDestination("test/mom/testPublishText", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(TextMarshaller.class)));
MOM.publish(JmsTestMom.class, queue, "hello world");
final ISubscription subscription = MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<String>() {
@Override
public void onMessage(IMessage<String> message) {
capturer.set(message.getTransferObject());
try {
latch.await();
} catch (InterruptedException e) {
throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
}
}
}, subscribeInput);
assertEquals("hello world", capturer.get());
IFuture<Void> disposeFuture = Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
subscription.dispose();
}
}, Jobs.newInput().withName("dispose subscription").withExecutionHint(m_testJobExecutionHint).withExceptionHandling(null, false));
// Verify
try {
disposeFuture.awaitDoneAndGet(200, TimeUnit.MILLISECONDS);
assertEquals(SubscribeInput.ACKNOWLEDGE_AUTO, subscribeInput.getAcknowledgementMode());
} catch (TimedOutError e) {
assertEquals(SubscribeInput.ACKNOWLEDGE_AUTO_SINGLE_THREADED, subscribeInput.getAcknowledgementMode());
} finally {
latch.countDown();
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class TimeoutRunContextStatement method evaluate.
@Override
public void evaluate() throws Throwable {
final SafeStatementInvoker invoker = new SafeStatementInvoker(m_next);
final IFuture<Void> future = Jobs.schedule(invoker, Jobs.newInput().withRunContext(// Run in new TX, because the same TX is not allowed to be used by multiple threads.
RunContext.CURRENT.get().copy().withTransactionScope(TransactionScope.REQUIRES_NEW)).withName("Running test with support for JUnit timeout"));
try {
future.awaitDone(m_timeoutMillis, TimeUnit.MILLISECONDS);
} catch (ThreadInterruptedError | TimedOutError e) {
// NOSONAR
future.cancel(true);
// JUnit timeout exception
throw new TestTimedOutException(m_timeoutMillis, TimeUnit.MILLISECONDS);
}
invoker.throwOnError();
}
use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class ClientSessionTest method testStopWithBlockingMessageBox.
@Test
public void testStopWithBlockingMessageBox() throws Exception {
TestingUtility.registerBean(new BeanMetaData(TestEnvironmentClientSession.class));
TestingUtility.registerBean(new BeanMetaData(JobCompletionDelayOnSessionShutdown.class).withProducer(new IBeanInstanceProducer<JobCompletionDelayOnSessionShutdown>() {
@Override
public JobCompletionDelayOnSessionShutdown produce(IBean<JobCompletionDelayOnSessionShutdown> bean) {
return new JobCompletionDelayOnSessionShutdown() {
@Override
protected Long getDefaultValue() {
return 1000L;
}
};
}
}));
session = BEANS.get(ClientSessionProvider.class).provide(ClientRunContexts.empty().withUserAgent(UserAgents.createDefault()));
// show a messagebox
IFuture<Integer> f = ModelJobs.schedule(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
messageBox = MessageBoxes.createYesNo();
return messageBox.show();
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(session, true)));
try {
f.awaitDoneAndGet(1, TimeUnit.SECONDS);
fail("must throw a " + TimedOutError.class.getName());
} catch (TimedOutError e) {
// nop
}
assertFalse(f.isDone());
// close from ui
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
session.getDesktop().getUIFacade().closeFromUI(true);
}
}, ModelJobs.newInput(ClientRunContexts.empty().withSession(session, true))).awaitDone();
assertEquals(JobState.DONE, f.getState());
}
use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class BlockingCondition method awaitUntilSignaledOrTimeout.
/**
* Waits until signaled or the timeout elapses. If <code>awaitInterruptibly</code> is set to <code>true</code>, this
* method returns with an {@link ThreadInterruptedError} upon interruption. For either case, when this method
* finally returns, the thread's interrupted status will still be set.
*/
protected void awaitUntilSignaledOrTimeout(final long timeout, final TimeUnit unit, final boolean awaitInterruptibly) {
boolean interrupted = false;
m_lock.lock();
try {
if (timeout == TIMEOUT_INDEFINITELY) {
while (m_blocking) {
// while-loop to address spurious wake-ups
if (awaitInterruptibly) {
m_unblockedCondition.await();
} else {
m_unblockedCondition.awaitUninterruptibly();
}
}
} else {
long nanos = unit.toNanos(timeout);
while (m_blocking && nanos > 0L) {
// while-loop to address spurious wake-ups
if (awaitInterruptibly) {
nanos = m_unblockedCondition.awaitNanos(nanos);
} else {
try {
// NOSONAR
nanos = m_unblockedCondition.awaitNanos(nanos);
} catch (final InterruptedException e) {
// remember interruption
interrupted = true;
// clear the interrupted status to continue waiting
Thread.interrupted();
}
}
}
if (nanos <= 0L) {
throw new TimedOutError("Timeout elapsed while waiting for a blocking condition to fall").withContextInfo("blockingCondition", this).withContextInfo("timeout", "{}ms", unit.toMillis(timeout)).withContextInfo("thread", Thread.currentThread().getName());
}
}
} catch (final InterruptedException e) {
interrupted = true;
throw new ThreadInterruptedError("Interrupted while waiting for a blocking condition to fall").withContextInfo("blockingCondition", this).withContextInfo("thread", Thread.currentThread().getName());
} finally {
m_lock.unlock();
if (interrupted) {
// restore interruption status
Thread.currentThread().interrupt();
}
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.TimedOutError in project scout.rt by eclipse.
the class JmsMomImplementorTest method testPublishTransactional.
@Test
public void testPublishTransactional() throws InterruptedException {
final Capturer<Person> capturer = new Capturer<>();
Person person = new Person();
person.setLastname("smith");
person.setFirstname("anna");
ITransaction tx = BEANS.get(ITransaction.class);
ITransaction.CURRENT.set(tx);
IDestination<Person> queue = MOM.newDestination("test/mom/testPublishTransactional", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queue, BEANS.get(ObjectMarshaller.class)));
MOM.publish(JmsTestMom.class, queue, person, MOM.newPublishInput().withTransactional(true));
m_disposables.add(MOM.subscribe(JmsTestMom.class, queue, new IMessageListener<Person>() {
@Override
public void onMessage(IMessage<Person> message) {
capturer.set(message.getTransferObject());
}
}));
try {
capturer.get(2, TimeUnit.SECONDS);
fail();
} catch (TimedOutError e) {
// expected
}
tx.commitPhase1();
tx.commitPhase2();
try {
capturer.get(5, TimeUnit.SECONDS);
} catch (TimedOutError e) {
fail();
} finally {
tx.release();
}
// Verify
Person testee = capturer.get();
assertEquals("smith", testee.getLastname());
assertEquals("anna", testee.getFirstname());
}
Aggregations