use of org.eclipse.scout.rt.server.commons.cache.ICacheEntry in project scout.rt by eclipse.
the class ServiceTunnelServletTest method testLookupScoutServerSessionOnHttpSessionMultipleThreads.
/**
* Calls {@link ServiceTunnelServlet#lookupServerSessionOnHttpSession(ServerRunContext) in 4 different threads within
* the same HTTP session. Test ensures that the same server session is returned in all threads and that
* {@link ServerSessionProvider#provide(ServerRunContext)}} is called only once.
*/
@Test
public void testLookupScoutServerSessionOnHttpSessionMultipleThreads() throws ServletException {
final Map<String, IServerSession> cache = new HashMap<>();
final TestServerSession testServerSession = new TestServerSession();
testServerSession.start("testSessionId");
testServerSession.setSharedContextVariable("userId", String.class, "testUser");
HttpServletRequest requestMock = mock(HttpServletRequest.class);
HttpSession testHttpSession = mock(HttpSession.class);
when(requestMock.getSession()).thenReturn(testHttpSession);
when(requestMock.getSession(true)).thenReturn(testHttpSession);
ICacheEntry cacheEntryMock = mock(ICacheEntry.class);
when(cacheEntryMock.getValue()).thenReturn(testServerSession);
when(cacheEntryMock.isActive()).thenReturn(true);
doAnswer(putValueInCache(cache)).when(testHttpSession).setAttribute(ArgumentMatchers.eq(IServerSession.class.getName()), ArgumentMatchers.any());
when(testHttpSession.getAttribute(IServerSession.class.getName())).thenAnswer(getCachedValue(cache));
doAnswer(slowCreateTestsession(testServerSession)).when(m_serverSessionProviderSpy).provide(ArgumentMatchers.anyString(), ArgumentMatchers.any(ServerRunContext.class));
List<HttpSessionLookupCallable> jobs = new ArrayList<>();
for (int i = 0; i < 4; i++) {
jobs.add(new HttpSessionLookupCallable(m_testServiceTunnelServlet, requestMock, m_responseMock));
}
List<IFuture<?>> futures = scheduleAndJoinJobs(jobs);
Set<IServerSession> serverSessions = new HashSet<IServerSession>();
for (IFuture<?> future : futures) {
serverSessions.add((IServerSession) future.awaitDoneAndGet());
}
assertEquals(CollectionUtility.hashSet(testServerSession), serverSessions);
verify(m_serverSessionProviderSpy, times(1)).provide(ArgumentMatchers.anyString(), ArgumentMatchers.any(ServerRunContext.class));
}
Aggregations