@Test public final void testProcessRequestNotePresent() throws IOException, ServletException { _sessionTrackerValve.invoke( _request, _response ); verify( _service, never() ).backupSession( anyString(), anyBoolean(), anyString() ); verify(_request).setNote(eq(RequestTrackingHostValve.REQUEST_PROCESS), eq(Boolean.TRUE)); }
@Test public final void testBackupSessionNotInvokedWhenNoSessionIdPresent() throws IOException, ServletException { when( _request.getRequestedSessionId() ).thenReturn( null ); when( _response.getHeader( eq( "Set-Cookie" ) ) ).thenReturn( null ); _sessionTrackerValve.invoke( _request, _response ); verify( _service, never() ).backupSession( anyString(), anyBoolean(), anyString() ); }
private void backupSession( final Request request, final Response response, final boolean sessionIdChanged ) { /* * Do we have a session? */ final String sessionId = getSessionId(request, response); if ( sessionId != null ) { _statistics.requestWithSession(); _sessionBackupService.backupSession( sessionId, sessionIdChanged, getURIWithQueryString( request ) ); } else { _statistics.requestWithoutSession(); } }
@Test public final void testBackupSessionInvokedWhenResponseCookiePresent() throws IOException, ServletException { when( _request.getRequestedSessionId() ).thenReturn( null ); final Cookie cookie = new Cookie( _sessionTrackerValve.getSessionCookieName(), "foo" ); setupGetResponseSetCookieHeadersExpectations(_response, new String[]{generateCookieString( cookie )}); _sessionTrackerValve.invoke( _request, _response ); verify( _service ).backupSession( eq( "foo" ), eq( false), anyString() ); }
@Test public final void testChangeSessionIdForRelocatedSession() throws IOException, ServletException { final String sessionId = "bar"; final String newSessionId = "newId"; when(_request.getNote(eq(RequestTrackingHostValve.SESSION_ID_CHANGED))).thenReturn(Boolean.TRUE); when( _request.getRequestedSessionId() ).thenReturn( sessionId ); final Cookie cookie = new Cookie( _sessionTrackerValve.getSessionCookieName(), newSessionId ); setupGetResponseSetCookieHeadersExpectations(_response, new String[]{generateCookieString( cookie )}); _sessionTrackerValve.invoke( _request, _response ); verify( _service ).backupSession( eq( newSessionId ), eq( true ), anyString() ); }
/** * Tests sessionAttributeFilter attribute: only filtered/allowed attributes must be serialized. */ @SuppressWarnings( { "unchecked", "rawtypes" } ) @Test public void testOnlyFilteredAttributesAreIncludedInSessionBackup() throws InterruptedException, ExecutionException { final TranscoderService transcoderServiceMock = mock( TranscoderService.class ); final ConcurrentMap<String, Object> anyMap = any( ConcurrentMap.class ); when( transcoderServiceMock.serializeAttributes( any( MemcachedBackupSession.class ), anyMap ) ).thenReturn( new byte[0] ); _service.setTranscoderService( transcoderServiceMock ); final MemcachedBackupSession session = createSession( _service ); _service.setSessionAttributeFilter( "^(foo|bar)$" ); session.setAttribute( "foo", "foo" ); session.setAttribute( "bar", "bar" ); session.setAttribute( "baz", "baz" ); _service.backupSession( session.getIdInternal(), false, null ).get(); // capture the supplied argument, alternatively we could have used some Matcher (but there seems to be no MapMatcher). final ArgumentCaptor<ConcurrentMap> model = ArgumentCaptor.forClass( ConcurrentMap.class ); verify( transcoderServiceMock, times( 1 ) ).serializeAttributes( eq( session ), model.capture() ); // the serialized attributes must only contain allowed ones assertTrue( model.getValue().containsKey( "foo" ) ); assertTrue( model.getValue().containsKey( "bar" ) ); assertFalse( model.getValue().containsKey( "baz" ) ); }
/** * Test for issue #105: Make memcached node optional for single-node setup * http://code.google.com/p/memcached-session-manager/issues/detail?id=105 */ @Test public void testBackupSessionFailureWithoutMemcachedNodeIdConfigured105() throws Exception { _service.setMemcachedNodes( "127.0.0.1:11211" ); _service.setSessionBackupAsync(false); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); final MemcachedBackupSession session = createSession( _service ); session.access(); session.endAccess(); session.setAttribute( "foo", "bar" ); @SuppressWarnings( "unchecked" ) final OperationFuture<Boolean> futureMock = mock( OperationFuture.class ); when( futureMock.get( ) ).thenThrow(new ExecutionException(new RuntimeException("Simulated exception."))); when( futureMock.get( anyInt(), any( TimeUnit.class ) ) ).thenThrow(new ExecutionException(new RuntimeException("Simulated exception."))); when( _memcachedMock.set( eq( session.getId() ), anyInt(), any(), any( Transcoder.class ) ) ).thenReturn( futureMock ); final BackupResult backupResult = _service.backupSession( session.getIdInternal(), false, null ).get(); assertEquals(backupResult.getStatus(), BackupResultStatus.FAILURE); verify( _memcachedMock, times( 1 ) ).set( eq( session.getId() ), anyInt(), any(), any( Transcoder.class ) ); }
/** * Test that session attribute serialization and hash calculation is only * performed if the session and its attributes were accessed since the last backup/backup check. * Otherwise this computing time shall be saved for a better world :-) * @throws ExecutionException * @throws InterruptedException */ @Test public void testOnlyHashAttributesOfAccessedSessionsAndAttributes() throws InterruptedException, ExecutionException { final TranscoderService transcoderServiceMock = mock( TranscoderService.class ); @SuppressWarnings( "unchecked" ) final ConcurrentMap<String, Object> anyMap = any( ConcurrentMap.class ); when( transcoderServiceMock.serializeAttributes( any( MemcachedBackupSession.class ), anyMap ) ).thenReturn( new byte[0] ); _service.setTranscoderService( transcoderServiceMock ); final MemcachedBackupSession session = createSession( _service ); session.setAttribute( "foo", "bar" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 1 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); // we need some millis between last backup and next access (due to check in BackupSessionService) Thread.sleep(5L); session.access(); session.getAttribute( "foo" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 2 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); // we need some millis between last backup and next access (due to check in BackupSessionService) Thread.sleep(5L); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 2 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); }
/** * Tests sessionAttributeFilter attribute: when excluded attributes are accessed/put the session should * not be marked as touched. */ @SuppressWarnings( "unchecked" ) @Test public void testOnlyHashAttributesOfAccessedFilteredAttributes() throws InterruptedException, ExecutionException { final TranscoderService transcoderServiceMock = mock( TranscoderService.class ); _service.setTranscoderService( transcoderServiceMock ); final MemcachedBackupSession session = createSession( _service ); _service.setSessionAttributeFilter( "^(foo|bar)$" ); session.setAttribute( "baz", "baz" ); session.access(); session.endAccess(); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, never() ).serializeAttributes( (MemcachedBackupSession)any(), (ConcurrentMap)any() ); }
session.endAccess(); session.setAttribute( "foo", "bar" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( _memcachedMock, times( 1 ) ).set( eq( session.getId() ), anyInt(), any(), any( Transcoder.class ) ); session.setAttribute( "foo", "bar" ); session.setAttribute( "bar", "baz" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( _memcachedMock, times( 2 ) ).set( eq( session.getId() ), anyInt(), any(), any( Transcoder.class ) ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( _memcachedMock, times( 2 ) ).set( eq( session.getId() ), anyInt(), any(), any( Transcoder.class ) );
/** * Test that session attribute serialization and hash calculation is only * performed if session attributes were accessed since the last backup. * Otherwise this computing time shall be saved for a better world :-) * @throws ExecutionException * @throws InterruptedException */ @Test public void testOnlyHashAttributesOfAccessedAttributes() throws InterruptedException, ExecutionException { final TranscoderService transcoderServiceMock = mock( TranscoderService.class ); @SuppressWarnings( "unchecked" ) final ConcurrentMap<String, Object> anyMap = any( ConcurrentMap.class ); when( transcoderServiceMock.serializeAttributes( any( MemcachedBackupSession.class ), anyMap ) ).thenReturn( new byte[0] ); _service.setTranscoderService( transcoderServiceMock ); final MemcachedBackupSession session = createSession( _service ); session.access(); session.endAccess(); session.setAttribute( "foo", "bar" ); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 1 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); session.access(); session.endAccess(); _service.backupSession( session.getIdInternal(), false, null ).get(); verify( transcoderServiceMock, times( 1 ) ).serializeAttributes( eq( session ), eq( session.getAttributesInternal() ) ); }
@Test public void testInvalidNonStickySessionDoesNotCallOnBackupWithoutLoadedSessionIssue137() throws Exception { _service.setStickyInternal( false ); _service.setLockingMode( LockingMode.NONE, null, false ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); // we must put in our mock again final String sessionId = "nonStickySessionToTimeOut-n1"; // For findSession needed final Request requestMock = mock(Request.class); when(requestMock.getNote(eq(RequestTrackingContextValve.INVOKED))).thenReturn(Boolean.TRUE); _service.getTrackingHostValve().storeRequestThreadLocal(requestMock); final MemcachedBackupSession session = _service.findSession(sessionId); assertNull(session); _service.backupSession( sessionId, false, null ).get(); // check that validity info is not loaded - this would trigger the // WARNING: Found no validity info for session id ... final String validityKey = new SessionIdFormat().createValidityInfoKeyName( sessionId ); verify( _memcachedMock, times( 0 ) ).get( eq( validityKey ) ); }
when( _memcachedMock.add( any( String.class ), anyInt(), any(), any( Transcoder.class ) ) ).thenReturn( futureMock ); _service.backupSession( sessionId, false, "unused" ).get();
@Override public BackupResult call() throws Exception { final MemcachedBackupSession session3 = _service.findSession(session.getId()); assertSame(session3, session2); assertEquals(session3.getRefCount(), 2); // let the other thread proceed (or wait) barrier.await(); // and wait again so that the other thread can do some work barrier.await(); final Future<BackupResult> result = _service.backupSession(session.getId(), false, "unused"); _service.getTrackingHostValve().resetRequestThreadLocal(); assertEquals(result.get().getStatus(), BackupResultStatus.SUCCESS); // The session should be released now and no longer stored assertFalse(_service.getManager().getSessionsInternal().containsKey(session.getId())); // just some double checking on expectations... assertEquals(session2.getRefCount(), 0); return result.get(); }
_service.backupSession( session.getIdInternal(), false, "foo" ).get(); _service.backupSession( session.getIdInternal(), false, "foo" );
@Test public void testBackupSessionInCouchbase() throws InterruptedException, ExecutionException { final MemcachedSessionService service = _tomcat1.getService(); final MemcachedBackupSession session = createSession( service ); final String sessionId = "12345"; session.setId(sessionId); session.setAttribute( "foo", "bar" ); final BackupResult backupResult = service.backupSession( session.getIdInternal(), false, null ).get(); assertEquals(backupResult.getStatus(), BackupResultStatus.SUCCESS); final MemcachedBackupSession loadedSession = transcoderService.deserialize(mc.get(sessionId, ByteArrayTranscoder.INSTANCE), _tomcat1.getManager()); checkSession(loadedSession, session); }
@Test public void testBackupSessionInRedis() throws InterruptedException, ExecutionException, UnsupportedEncodingException, ClassNotFoundException, IOException { final MemcachedSessionService service = _tomcat1.getService(); final MemcachedBackupSession session = createSession( service ); final String sessionId = "12345"; session.setId(sessionId); session.setAttribute( "foo", "bar" ); final BackupResult backupResult = service.backupSession( session.getIdInternal(), false, null ).get(); assertEquals(backupResult.getStatus(), BackupResultStatus.SUCCESS); final MemcachedBackupSession loadedSession = transcoderService.deserialize( redisClient.get(sessionId.getBytes("UTF-8")), _tomcat1.getManager()); checkSession(loadedSession, session); }
final String sessionId = session.getId(); _service.backupSession( sessionId, false, "unused" ).get();
Future<BackupResult> result = _service.backupSession(session.getId(), false, "unused"); assertFalse(_service.getManager().getSessionsInternal().containsKey(session.getId())); result = _service.backupSession(session.getId(), false, null); _service.getTrackingHostValve().resetRequestThreadLocal(); assertEquals(result.get().getStatus(), BackupResultStatus.SKIPPED);
@Test(enabled = false) // spurious failures public void testBackupSessionInCouchbaseCluster() throws Exception { final MemcachedSessionService service = _tomcat1.getService(); cluster.add(setupCouchbase(getMaxCouchbasePort() + 1)); service.setMemcachedNodes(getMemcachedNodesConfig(getURIs())); setupCouchbaseClient(); waitForReconnect(service.getStorageClient(), cluster.size(), 1000); waitForReconnect(mc, cluster.size(), 1000); final MemcachedBackupSession session = createSession( service ); final String sessionId = "12345"; session.setId(sessionId); session.setAttribute( "foo", "bar" ); final BackupResult backupResult = service.backupSession( session.getIdInternal(), false, null ).get(); assertEquals(backupResult.getStatus(), BackupResultStatus.SUCCESS); final MemcachedBackupSession loadedSession = transcoderService.deserialize(mc.get(sessionId, ByteArrayTranscoder.INSTANCE), _tomcat1.getManager()); checkSession(loadedSession, session); }