use of org.eclipse.scout.rt.platform.holders.Holder in project scout.rt by eclipse.
the class WhenDoneTest method testSuccessWithJobAlreadyCompleted.
@Test
public void testSuccessWithJobAlreadyCompleted() throws InterruptedException {
// synchronized because modified/read by different threads.
final List<String> protocol = Collections.synchronizedList(new ArrayList<String>());
final Holder<DoneEvent<String>> eventHolder = new Holder<>();
final IFuture<String> future = Jobs.schedule(new Callable<String>() {
@Override
public String call() throws Exception {
protocol.add("1");
return "result";
}
}, Jobs.newInput().withRunContext(RunContexts.copyCurrent()));
future.awaitDoneAndGet();
final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(1);
future.whenDone(new IDoneHandler<String>() {
@Override
public void onDone(DoneEvent<String> event) {
protocol.add("2");
if (future.isDone()) {
protocol.add("done");
}
if (future.isCancelled()) {
protocol.add("cancelled");
}
eventHolder.setValue(event);
verifyLatch.countDown();
}
}, RunContexts.copyCurrent());
assertTrue(verifyLatch.await());
assertEquals(CollectionUtility.arrayList("1", "2", "done"), protocol);
assertNull(eventHolder.getValue().getException());
assertEquals("result", eventHolder.getValue().getResult());
assertFalse(eventHolder.getValue().isCancelled());
assertFalse(eventHolder.getValue().isFailed());
}
use of org.eclipse.scout.rt.platform.holders.Holder 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.holders.Holder 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();
}
use of org.eclipse.scout.rt.platform.holders.Holder in project scout.rt by eclipse.
the class AbstractJaxWsClientTest method testAcquirePortConcurrentlyInDifferentTransactions.
@Test
public void testAcquirePortConcurrentlyInDifferentTransactions() throws InterruptedException {
final CountDownLatch txn1InitLatch = new CountDownLatch(1);
final CountDownLatch txn2InitLatch = new CountDownLatch(1);
final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
JaxWsConsumerTestServicePortType port0 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
assertSendEcho(port0, 0);
txn1PortHolder.setValue(port0);
txn1InitLatch.countDown();
txn2InitLatch.await();
JaxWsConsumerTestServicePortType port1 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
assertSamePort(port0, port1);
assertSendEcho(port1, 1);
}
}, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
txn1InitLatch.await();
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
JaxWsConsumerTestServicePortType port0 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
assertSendEcho(port0, 0);
txn2PortHolder.setValue(port0);
txn2InitLatch.countDown();
JaxWsConsumerTestServicePortType port1 = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
assertSamePort(port0, port1);
assertSendEcho(port1, 1);
}
}, Jobs.newInput().withRunContext(ServerRunContexts.empty()));
txn2InitLatch.await();
assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
use of org.eclipse.scout.rt.platform.holders.Holder in project scout.rt by eclipse.
the class AbstractJaxWsClientTest method testAcquirePortInDifferentTransactions.
/*
* ************************************************************
* Test acquire port in different transactions
* ************************************************************/
@Test
public void testAcquirePortInDifferentTransactions() throws InterruptedException {
final Holder<JaxWsConsumerTestServicePortType> txn1PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
final Holder<JaxWsConsumerTestServicePortType> txn2PortHolder = new Holder<>(JaxWsConsumerTestServicePortType.class);
// This test case expects at most one port in the pool. It is guaranteed by discarding all pooled entries.
BEANS.get(JaxWsConsumerTestClient.class).discardAllPoolEntries();
ServerRunContexts.copyCurrent().run(new IRunnable() {
@Override
public void run() throws Exception {
JaxWsConsumerTestServicePortType port = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
assertSendEcho(port, 0);
txn1PortHolder.setValue(port);
}
});
ServerRunContexts.copyCurrent().run(new IRunnable() {
@Override
public void run() throws Exception {
JaxWsConsumerTestServicePortType port = BEANS.get(JaxWsConsumerTestClient.class).newInvocationContext().getPort();
assertSendEcho(port, 0);
txn2PortHolder.setValue(port);
}
});
if (BEANS.get(JaxWsImplementorSpecifics.class).isPoolingSupported()) {
assertSamePort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
} else {
assertDifferentPort(txn1PortHolder.getValue(), txn2PortHolder.getValue());
}
}
Aggregations