Search in sources :

Example 1 with ICacheEntry

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));
}
Also used : HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) ICacheEntry(org.eclipse.scout.rt.server.commons.cache.ICacheEntry) IFuture(org.eclipse.scout.rt.platform.job.IFuture) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 IFuture (org.eclipse.scout.rt.platform.job.IFuture)1 ICacheEntry (org.eclipse.scout.rt.server.commons.cache.ICacheEntry)1 ServerRunContext (org.eclipse.scout.rt.server.context.ServerRunContext)1 Test (org.junit.Test)1