/** * 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() ) ); }
@Test public void testSerializeSessionWithAttributes() { final MemcachedBackupSession session = (MemcachedBackupSession) _manager.createSession( null ); final TranscoderService transcoderService = new TranscoderService( new JavaSerializationTranscoder( _manager ) ); final String value = "bar"; session.setAttribute( "foo", value ); session.setLastBackupTime( System.currentTimeMillis() ); final byte[] data = transcoderService.serialize( session ); final MemcachedBackupSession deserialized = transcoderService.deserialize( data, _manager ); assertSessionFields( session, deserialized ); Assert.assertEquals( value, deserialized.getAttribute( "foo" ) ); }
/** * Test for issue #38: * Notify HttpSessionActivationListeners when loading a session from memcached * @throws Exception */ @Test( enabled = true ) public void testHttpSessionActivationListenersNotifiedOnLoadWithJvmRoute() throws Exception { final SessionManager manager1 = _tomcat1.getManager(); final SessionManager manager2 = _tomcat2.getManager(); final SessionIdFormat format = new SessionIdFormat(); final String sessionId1 = get( _httpClient, TC_PORT_1, null ).getSessionId(); assertEquals( format.extractJvmRoute( sessionId1 ), JVM_ROUTE_1 ); final MemcachedBackupSession session = (MemcachedBackupSession) manager1.findSession( sessionId1 ); session.setAttribute( "listener", new RecordingSessionActivationListener() ); get( _httpClient, TC_PORT_1, sessionId1 ); final String sessionId2 = get( _httpClient, TC_PORT_2, sessionId1 ).getSessionId(); assertEquals( format.stripJvmRoute( sessionId2 ), format.stripJvmRoute( sessionId1 ) ); assertEquals( format.extractJvmRoute( sessionId2 ), JVM_ROUTE_2 ); final MemcachedBackupSession loaded = (MemcachedBackupSession) manager2.findSession( sessionId2 ); assertNotNull( loaded ); final RecordingSessionActivationListener listener = (RecordingSessionActivationListener) loaded.getAttribute( "listener" ); assertNotNull( listener ); final String notifiedSessionId = listener.getSessionDidActivate(); assertEquals( notifiedSessionId, sessionId2 ); }
/** * Test for issue #38: * Notify HttpSessionActivationListeners when loading a session from memcached * @throws Exception */ @Test( enabled = true ) public void testHttpSessionActivationListenersNotifiedOnLoadWithoutJvmRoute() throws Exception { _tomcat1.stop(); _tomcat2.stop(); _tomcat1 = startTomcat( TC_PORT_1, null ); _tomcat2 = startTomcat( TC_PORT_2, null ); final SessionManager manager1 = _tomcat1.getManager(); final SessionManager manager2 = _tomcat2.getManager(); final SessionIdFormat format = new SessionIdFormat(); final String sessionId1 = get( _httpClient, TC_PORT_1, null ).getSessionId(); assertNull( format.extractJvmRoute( sessionId1 ) ); final MemcachedBackupSession session = (MemcachedBackupSession) manager1.findSession( sessionId1 ); session.setAttribute( "listener", new RecordingSessionActivationListener() ); get( _httpClient, TC_PORT_1, sessionId1 ); final String sessionId2 = get( _httpClient, TC_PORT_2, sessionId1 ).getSessionId(); assertEquals( sessionId2, sessionId1 ); final MemcachedBackupSession loaded = (MemcachedBackupSession) manager2.findSession( sessionId2 ); assertNotNull( loaded ); final RecordingSessionActivationListener listener = (RecordingSessionActivationListener) loaded.getAttribute( "listener" ); assertNotNull( listener ); final String notifiedSessionId = listener.getSessionDidActivate(); assertEquals( notifiedSessionId, sessionId2 ); }
@Test( enabled = true ) public void testDeserializeHibernateCollection() { final SessionManager manager = createSessionManager(); final Set<Animal> animals = new HashSet<Animal>( Arrays.asList( new Animal( "cat" ) ) ); final Person person = new Person( "foo bar", animals ); final Long personId = createPerson( person ); final Person foundPerson = findPerson( personId ); LOG.info( "person: " + person.toString() ); LOG.info( "found: " + foundPerson.toString() ); TestUtils.assertDeepEquals( person, foundPerson ); final TranscoderService transcoderService = new TranscoderService( createTranscoder( manager ) ); final MemcachedBackupSession session = createSession( manager, "123456789" ); session.setAttribute( "person", foundPerson ); final byte[] data = transcoderService.serialize( session ); final MemcachedBackupSession deserialized = transcoderService.deserialize( data, manager ); final Person deserializedPerson = (Person) deserialized.getAttribute( "person" ); TestUtils.assertDeepEquals( foundPerson, deserializedPerson ); }