use of org.eclipse.scout.rt.server.IServerSession 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.server.IServerSession 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.server.IServerSession in project scout.rt by eclipse.
the class ServerSessionLifecycleHandler method create.
@Override
public IServerSession create() {
LOG.debug("Creating scout server session id={}", m_sessionId);
IServerSession session = BEANS.get(ServerSessionProvider.class).provide(m_sessionId, m_serverRunContext.copy());
if (m_clientNodeId != null) {
BEANS.get(IClientNotificationService.class).registerSession(m_clientNodeId, m_sessionId, session.getUserId());
}
return session;
}
use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.
the class ServerSessionCacheTest method testParallelRequestsWithdifferentIds.
@Test
public void testParallelRequestsWithdifferentIds() {
final TestHttpSession httpSession1 = new TestHttpSession();
final TestHttpSession httpSession2 = new TestHttpSession();
final ServerSessionCache cache = BEANS.get(ServerSessionCache.class);
final IBlockingCondition bc = Jobs.newBlockingCondition(true);
final IServerSessionLifecycleHandler handler1 = new TestServerSessionLifecycleHandler("id1") {
@Override
public IServerSession create() {
bc.waitFor(1, TimeUnit.SECONDS);
return super.create();
}
};
final IServerSessionLifecycleHandler handler2 = new TestServerSessionLifecycleHandler("id2") {
@Override
public IServerSession create() {
bc.setBlocking(false);
return super.create();
}
};
IFuture<IServerSession> f1 = Jobs.schedule(new Callable<IServerSession>() {
@Override
public IServerSession call() throws Exception {
return cache.getOrCreate(handler1, httpSession1);
}
}, Jobs.newInput());
IFuture<IServerSession> f2 = Jobs.schedule(new Callable<IServerSession>() {
@Override
public IServerSession call() throws Exception {
return cache.getOrCreate(handler2, httpSession2);
}
}, Jobs.newInput());
IServerSession session2 = f2.awaitDoneAndGet();
IServerSession session1 = f1.awaitDoneAndGet();
assertNotNull(session2);
assertNotNull(session1);
assertNotSame(session1, session2);
}
use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.
the class ServerRunContextTest method testRunContextsCopyCurrent.
@Test
public void testRunContextsCopyCurrent() {
final IServerSession session = mock(IServerSession.class);
final UserAgent userAgent = UserAgents.create().build();
final Locale locale = Locale.CANADA_FRENCH;
final Subject subject = new Subject();
ServerRunContexts.empty().withSession(session).withUserAgent(userAgent).withLocale(locale).withSubject(subject).run(new IRunnable() {
@Override
public void run() throws Exception {
RunContext runContext = RunContexts.copyCurrent();
assertThat(runContext, CoreMatchers.instanceOf(ServerRunContext.class));
ServerRunContext serverCtx = (ServerRunContext) runContext;
assertSame(session, serverCtx.getSession());
assertSame(userAgent, serverCtx.getUserAgent());
assertSame(locale, serverCtx.getLocale());
assertSame(subject, serverCtx.getSubject());
}
});
}
Aggregations