/** * Tests that non-sticky sessions are not invalidated too early when sessions are accessed readonly. * Each (even session readonly request) must update the lastAccessedTime for the session in memcached. */ @Test( enabled = true, dataProvider = "lockingModes" ) public void testNonStickySessionIsValidEvenWhenAccessedReadonly( @Nonnull final LockingMode lockingMode, @Nullable final Pattern uriPattern ) throws IOException, InterruptedException, HttpException, ExecutionException { _tomcat1.getManager().setMaxInactiveInterval( 1 ); _tomcat1.getManager().setLockingMode( lockingMode, uriPattern, true ); long start = currentTimeMillis(); final String sessionId = get( _httpClient, TC_PORT_1, null ).getSessionId(); assertNotNull( sessionId ); assertEquals( get( _httpClient, TC_PORT_1, sessionId ).getSessionId(), sessionId, "Wrong/new sessionId after " + (currentTimeMillis() - start) + " ms." ); Thread.sleep( 500 ); assertEquals( get( _httpClient, TC_PORT_1, sessionId ).getSessionId(), sessionId, "Wrong/new sessionId after " + (currentTimeMillis() - start) + " ms." ); Thread.sleep( 500 ); assertEquals( get( _httpClient, TC_PORT_1, sessionId ).getSessionId(), sessionId, "Wrong/new sessionId after " + (currentTimeMillis() - start) + " ms." ); }
/** * Tests that non-sticky sessions are seen as valid (request.isRequestedSessionIdValid) and from * the correct source for different session tracking modes (uri/cookie). */ @Test( enabled = true, dataProvider = "sessionTrackingModesProvider" ) public void testNonStickySessionIsValidForDifferentSessionTrackingModes( @Nonnull final SessionTrackingMode sessionTrackingMode ) throws IOException, InterruptedException, HttpException, ExecutionException { _tomcat1.getManager().setMaxInactiveInterval( 1 ); _tomcat1.getManager().setLockingMode( LockingMode.ALL, null, true ); final String sessionId = get( _httpClient, TC_PORT_1, null ).getSessionId(); assertNotNull( sessionId ); Response response = get( _httpClient, TC_PORT_1, PATH_GET_REQUESTED_SESSION_INFO, sessionId, sessionTrackingMode, null, null ); assertEquals( response.getSessionId(), sessionId ); assertEquals( response.get( KEY_REQUESTED_SESSION_ID ), sessionId ); assertEquals( Boolean.parseBoolean( response.get( KEY_IS_REQUESTED_SESSION_ID_VALID ) ), true ); Thread.sleep( 100 ); response = get( _httpClient, TC_PORT_1, PATH_GET_REQUESTED_SESSION_INFO, sessionId, sessionTrackingMode, null, null ); assertEquals( response.getSessionId(), sessionId ); assertEquals( response.get( KEY_REQUESTED_SESSION_ID ), sessionId ); assertEquals( Boolean.parseBoolean( response.get( KEY_IS_REQUESTED_SESSION_ID_VALID ) ), true ); }
manager.setMaxInactiveInterval( delay * 4 );
manager.setMaxInactiveInterval( 5 ); manager.setMemcachedNodes(memcachedNodes); manager.getMemcachedSessionService().setSessionBackupAsync(false);
manager.setMaxInactiveInterval( 5 ); manager.setMemcachedNodes(memcachedNodes); manager.getMemcachedSessionService().setSessionBackupAsync(false);
manager.setMaxInactiveInterval( delay * 4 );
_tomcat1.getManager().setMaxInactiveInterval(60*60*24*30 / 2 + 1); setLockingMode(lockingMode, uriPattern);
/** * Test for issue http://code.google.com/p/memcached-session-manager/issues/detail?id=120 */ @Test(enabled = true, dataProvider = "lockingModesWithSessionLocking") @edu.umd.cs.findbugs.annotations.SuppressWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") public void testLoadBackupSessionShouldWorkWithInfiniteSessionTimeoutIssue120(@Nonnull final LockingMode lockingMode, @Nullable final Pattern uriPattern) throws IOException, InterruptedException, HttpException, ExecutionException { _tomcat1.getManager().setMaxInactiveInterval(-1); setLockingMode(lockingMode, uriPattern); final String sessionId = post(_httpClient, TC_PORT_1, null, "k1", "v1").getSessionId(); assertNotNull(sessionId); Thread.sleep(200); // we want to get the session from the primary node Response response = get(_httpClient, TC_PORT_1, sessionId); assertEquals(response.getSessionId(), sessionId); assertEquals(response.get("k1"), "v1"); // now we shut down the primary node so that the session is loaded from the backup node final SessionIdFormat fmt = new SessionIdFormat(); final String nodeId = fmt.extractMemcachedId( sessionId ); final MemCacheDaemon<?> primary = NODE_ID_1.equals(nodeId) ? _daemon1 : _daemon2; primary.stop(); Thread.sleep( 200 ); // the session should be loaded from the backup node response = get(_httpClient, TC_PORT_1, sessionId); assertEquals(fmt.createNewSessionId(response.getSessionId(), nodeId), sessionId); assertEquals(response.get("k1"), "v1"); }
/** * Test for issue #49: * Sessions not associated with a memcached node don't get associated as soon as a memcached is available * @throws InterruptedException * @throws IOException * @throws TimeoutException * @throws ExecutionException */ @Test( enabled = true ) public void testNotAssociatedSessionGetsAssociatedIssue49() throws InterruptedException, IOException, ExecutionException, TimeoutException { _daemon.stop(); final SessionManager manager = _tomcat1.getManager(); manager.setMaxInactiveInterval( 5 ); manager.setSticky( true ); final SessionIdFormat sessionIdFormat = new SessionIdFormat(); final Session session = manager.createSession( null ); assertNull( sessionIdFormat.extractMemcachedId( session.getId() ) ); _daemon.start(); // Wait so that the daemon will be available and the client can reconnect (async get didn't do the trick) waitForReconnect(manager.getMemcachedSessionService().getStorageClient(), 1, 4000); final String newSessionId = manager.getMemcachedSessionService().changeSessionIdOnMemcachedFailover( session.getId() ); assertNotNull( newSessionId ); assertEquals( newSessionId, session.getId() ); assertEquals( sessionIdFormat.extractMemcachedId( newSessionId ), _memcachedNodeId ); }
/** * Tests that non-sticky sessions are not leading to stale data - that sessions are removed from * tomcat when the request is finished. */ @Test( enabled = true ) public void testNoStaleSessionsWithNonStickySessions() throws IOException, InterruptedException, HttpException { _tomcat1.getManager().setMaxInactiveInterval( 1 ); _tomcat2.getManager().setMaxInactiveInterval( 1 ); final String key = "foo"; final String value1 = "bar"; final String sessionId1 = post( _httpClient, TC_PORT_1, null, key, value1 ).getSessionId(); assertNotNull( sessionId1 ); await("session exists in memcached").ignoreExceptions().until(new Callable<Object>() { @Override public Object call() throws Exception { return _client.get( sessionId1 ); } }, notNullValue()); /* We modify the stored value with the next request which is served by tc2 */ final String value2 = "baz"; final String sessionId2 = post( _httpClient, TC_PORT_2, sessionId1, key, value2 ).getSessionId(); assertEquals( sessionId2, sessionId1 ); /* Check that tc1 reads the updated value */ final Response response = get( _httpClient, TC_PORT_1, sessionId1 ); assertEquals( response.getSessionId(), sessionId1 ); assertEquals( response.get( key ), value2 ); }
@Test( enabled = true ) @SuppressWarnings( "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE" ) public void testNonStickySessionIsStoredInSecondaryMemcachedForBackup() throws IOException, InterruptedException, HttpException { _tomcat1.getManager().setMaxInactiveInterval( 1 ); _tomcat2.getManager().setMaxInactiveInterval( 1 ); final String sessionId1 = post( _httpClient, TC_PORT_1, null, "foo", "bar" ).getSessionId(); assertNotNull( sessionId1 ); // the memcached client writes async, so it's ok to wait a little bit (especially on windows) waitForMemcachedClient( 100 ); final SessionIdFormat fmt = new SessionIdFormat(); final String nodeId = fmt.extractMemcachedId( sessionId1 ); final MemCacheDaemon<?> primary = nodeId.equals( NODE_ID_1 ) ? _daemon1 : _daemon2; final MemCacheDaemon<?> secondary = nodeId.equals( NODE_ID_1 ) ? _daemon2 : _daemon1; assertNotNull( primary.getCache().get( key( sessionId1 ) )[0], sessionId1 ); assertNotNull( primary.getCache().get( key( fmt.createValidityInfoKeyName( sessionId1 ) ) )[0], fmt.createValidityInfoKeyName( sessionId1 ) ); // The executor needs some time to finish the backup... Thread.sleep( 500 ); assertNotNull( secondary.getCache().get( key( fmt.createBackupKey( sessionId1 ) ) )[0] ); assertNotNull( secondary.getCache().get( key( fmt.createBackupKey( fmt.createValidityInfoKeyName( sessionId1 ) ) ) )[0] ); }