use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class RunContextTest method testHardCancellation.
@Test
public void testHardCancellation() throws InterruptedException {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
final AtomicBoolean interrupted = new AtomicBoolean();
final RunMonitor monitor = BEANS.get(RunMonitor.class);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
RunContexts.empty().withRunMonitor(monitor).run(new IRunnable() {
@Override
public void run() throws Exception {
try {
assertTrue(setupLatch.countDownAndBlock(10, TimeUnit.SECONDS));
} catch (InterruptedException e) {
interrupted.set(true);
} finally {
verifyLatch.countDown();
}
}
});
}
}, Jobs.newInput());
assertTrue(setupLatch.await());
monitor.cancel(true);
assertTrue(verifyLatch.await());
assertTrue(interrupted.get());
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class ThreadInterruptionTest method testThreadInterrupted.
@Test
public void testThreadInterrupted() {
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
Thread.currentThread().interrupt();
assertTrue(Thread.currentThread().isInterrupted());
IRestorer interruption = ThreadInterruption.clear();
assertFalse(Thread.currentThread().isInterrupted());
interruption.restore();
assertTrue(Thread.currentThread().isInterrupted());
}
}, Jobs.newInput().withExceptionHandling(null, false)).awaitDoneAndGet();
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class InvocationContextTest method testCancel.
@Test(timeout = 5000)
public void testCancel() {
final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(1);
final IBlockingCondition processingCondition = Jobs.newBlockingCondition(true);
final InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
invocationContext.withEndpointUrl("http://localhost");
// Make Stub.webMethod to block until cancelled.
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
setupLatch.countDown();
processingCondition.waitForUninterruptibly(10, TimeUnit.SECONDS);
return null;
}
}).when(m_port).webMethod();
final RunMonitor runMonitor = new RunMonitor();
// Cancel the 'webMethod' once blocking.
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
setupLatch.await();
runMonitor.cancel(true);
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
// Run the test by invoking the web service with a specific RunMonitor to test cancellation.
try {
RunContexts.empty().withRunMonitor(runMonitor).run(new IRunnable() {
@Override
public void run() throws Exception {
try {
// this method blocks until cancelled.
invocationContext.getPort().webMethod();
fail("WebServiceRequestCancelledException expected");
} catch (WebServiceRequestCancelledException e) {
verify(m_implementorSpecifics).closeSocket(same(m_port), anyString());
}
}
});
} finally {
processingCondition.setBlocking(false);
}
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class InvocationContextTest method testWithException.
@Test
public void testWithException() {
// Unregister JUnit exception handler
BEANS.getBeanManager().unregisterBean(BEANS.getBeanManager().getBean(JUnitExceptionHandler.class));
final Holder<ITransaction> currentTransaction = new Holder<>();
final Holder<ITransaction> invocationTransaction = new Holder<>();
final Holder<IServerSession> invocationServerSession = new Holder<>();
final Holder<Exception> callableException = new Holder<>();
// simulate that 'webMethod' throws an exception.
final RuntimeException exception = new RuntimeException();
doThrow(exception).when(m_port).webMethod();
try {
ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
TransactionScope.REQUIRES_NEW).run(new IRunnable() {
@Override
public void run() throws Exception {
currentTransaction.setValue(ITransaction.CURRENT.get());
InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
invocationContext.withEndpointUrl("http://localhost");
invocationContext.whenCommit(m_commitListener);
invocationContext.whenRollback(m_rollbackListener);
invocationContext.whenInvoke(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
invocationTransaction.setValue(ITransaction.CURRENT.get());
invocationServerSession.setValue(ServerSessionProvider.currentSession());
return method.invoke(proxy, args);
}
});
// run the test
try {
invocationContext.getPort().webMethod();
} catch (Exception e) {
callableException.setValue(e);
throw e;
}
}
});
fail("RuntimeException expected");
} catch (RuntimeException e) {
// NOOP
}
assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
verify(m_port).webMethod();
verify(m_commitListener, never()).onCommitPhase1();
verify(m_commitListener, never()).onCommitPhase2();
verify(m_rollbackListener).onRollback();
assertSame(callableException.getValue(), exception);
}
use of org.eclipse.scout.rt.platform.util.concurrent.IRunnable in project scout.rt by eclipse.
the class InvocationContextTest method testWithSuccess.
@Test
public void testWithSuccess() {
final Holder<ITransaction> currentTransaction = new Holder<>();
final Holder<ITransaction> invocationTransaction = new Holder<>();
final Holder<IServerSession> invocationServerSession = new Holder<>();
ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
TransactionScope.REQUIRES_NEW).run(new IRunnable() {
@Override
public void run() throws Exception {
currentTransaction.setValue(ITransaction.CURRENT.get());
InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
invocationContext.withEndpointUrl("http://localhost");
invocationContext.whenCommit(m_commitListener);
invocationContext.whenRollback(m_rollbackListener);
invocationContext.whenInvoke(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
invocationTransaction.setValue(ITransaction.CURRENT.get());
invocationServerSession.setValue(ServerSessionProvider.currentSession());
return method.invoke(proxy, args);
}
});
// run the test
invocationContext.getPort().webMethod();
}
});
assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
verify(m_port).webMethod();
verify(m_commitListener).onCommitPhase1();
verify(m_commitListener).onCommitPhase2();
verify(m_rollbackListener, never()).onRollback();
}
Aggregations