From c727518d5c26caf35a235734ea2bf65fe0d5122b Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Wed, 18 May 2016 17:04:34 +1000 Subject: [PATCH] Fix session test timeouts --- .../gcloud/session/SessionExpiryTest.java | 3 +- .../AbstractCreateAndInvalidateTest.java | 8 ++--- .../session/AbstractLastAccessTimeTest.java | 7 ++-- .../AbstractLocalSessionScavengingTest.java | 21 ++++++------ .../session/AbstractNewSessionTest.java | 2 +- .../AbstractProxySerializationTest.java | 2 +- .../AbstractReentrantRequestSessionTest.java | 2 +- .../session/AbstractRemoveSessionTest.java | 4 +-- .../session/AbstractSessionCookieTest.java | 4 +-- .../session/AbstractSessionExpiryTest.java | 33 ++++++++++++------- ...bstractSessionInvalidateAndCreateTest.java | 10 +++--- 11 files changed, 50 insertions(+), 46 deletions(-) diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/SessionExpiryTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/SessionExpiryTest.java index 1af07adfafd..15b3ee11055 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/SessionExpiryTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/SessionExpiryTest.java @@ -78,7 +78,7 @@ public class SessionExpiryTest extends AbstractSessionExpiryTest public void testSessionExpiry() throws Exception { super.testSessionExpiry(); - try{_testSupport.assertSessions(0);}catch(Exception e){ Assert.fail(e.getMessage());} + _testSupport.deleteSessions(); } @Override @@ -92,7 +92,6 @@ public class SessionExpiryTest extends AbstractSessionExpiryTest public void verifySessionDestroyed(TestHttpSessionListener listener, String sessionId) { super.verifySessionDestroyed(listener, sessionId); - try{_testSupport.assertSessions(0);}catch(Exception e){ Assert.fail(e.getMessage());} } diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractCreateAndInvalidateTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractCreateAndInvalidateTest.java index 4ba7eb6cee2..396c79f4fe3 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractCreateAndInvalidateTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractCreateAndInvalidateTest.java @@ -54,7 +54,7 @@ import org.junit.Test; * AbstractCreateAndInvalidateTest * * Test that creating a session and invalidating it before the request exits the session - * does not result in the session being in mongo + * does not result in the session being persisted */ public abstract class AbstractCreateAndInvalidateTest{ @@ -88,7 +88,7 @@ public abstract class AbstractCreateAndInvalidateTest{ * Create and then invalidate a session in the same request. * @throws Exception */ - //@Test + @Test public void testSessionCreateAndInvalidate() throws Exception { String contextPath = ""; @@ -132,7 +132,7 @@ public abstract class AbstractCreateAndInvalidateTest{ * completes. * @throws Exception */ - //@Test + @Test public void testSessionCreateForward () throws Exception { String contextPath = ""; @@ -162,7 +162,7 @@ public abstract class AbstractCreateAndInvalidateTest{ ContentResponse response = client.GET(url+"?action=forward"); assertEquals(HttpServletResponse.SC_OK,response.getStatus()); - //check that the sessions exist in mongo + //check that the sessions exist persisted checkSession(_servlet._id, true); checkSessionByKey (_servlet._id, "0_0_0_0:", true); checkSessionByKey (_servlet._id, "0_0_0_0:_contextB", true); diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLastAccessTimeTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLastAccessTimeTest.java index df1f5c2bfff..ff4174fd6a8 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLastAccessTimeTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLastAccessTimeTest.java @@ -65,8 +65,7 @@ public abstract class AbstractLastAccessTimeTest String servletMapping = "/server"; int maxInactivePeriod = 8; //session will timeout after 8 seconds int scavengePeriod = 2; //scavenging occurs every 2 seconds - int idlePassivatePeriod = -1; //dont check for idle sessions to passivate - AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod, idlePassivatePeriod); + AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT); TestServlet servlet1 = new TestServlet(); ServletHolder holder1 = new ServletHolder(servlet1); ServletContextHandler context = server1.addContext(contextPath); @@ -80,7 +79,7 @@ public abstract class AbstractLastAccessTimeTest { server1.start(); int port1=server1.getPort(); - AbstractTestServer server2 = createServer(0, maxInactivePeriod, scavengePeriod, idlePassivatePeriod); + AbstractTestServer server2 = createServer(0, maxInactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT); ServletContextHandler context2 = server2.addContext(contextPath); context2.addServlet(TestServlet.class, servletMapping); SessionHandler m2 = context2.getSessionHandler(); @@ -130,7 +129,7 @@ public abstract class AbstractLastAccessTimeTest // At this point, session1 should be eligible for expiration. // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period - Thread.sleep(scavengePeriod * 2500L); + Thread.sleep(maxInactivePeriod+(scavengePeriod * 2500L)); //check that the session was not scavenged over on server1 by ensuring that the SessionListener destroy method wasn't called assertFalse(listener1._destroys.contains(AbstractTestServer.extractSessionId(sessionCookie))); diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLocalSessionScavengingTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLocalSessionScavengingTest.java index b6fae797dae..d4104b360ee 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLocalSessionScavengingTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLocalSessionScavengingTest.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.server.session; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -46,7 +47,7 @@ public abstract class AbstractLocalSessionScavengingTest { try { - Thread.sleep(scavengePeriod * 2500L); + Thread.sleep(scavengePeriod * 1000L); } catch (InterruptedException e) { @@ -59,10 +60,9 @@ public abstract class AbstractLocalSessionScavengingTest { String contextPath = ""; String servletMapping = "/server"; - int inactivePeriod = 1; - int scavengePeriod = 2; - int evictionSec = 20; - AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod, evictionSec); + int inactivePeriod = 4; + int scavengePeriod = 1; + AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT); ServletContextHandler context1 = server1.addContext(contextPath); context1.addServlet(TestServlet.class, servletMapping); @@ -70,7 +70,7 @@ public abstract class AbstractLocalSessionScavengingTest { server1.start(); int port1 = server1.getPort(); - AbstractTestServer server2 = createServer(0, inactivePeriod, scavengePeriod * 3, evictionSec); + AbstractTestServer server2 = createServer(0, inactivePeriod, scavengePeriod * 2, SessionCache.NEVER_EVICT); ServletContextHandler context2 = server2.addContext(contextPath); context2.addServlet(TestServlet.class, servletMapping); @@ -103,8 +103,8 @@ public abstract class AbstractLocalSessionScavengingTest assertEquals(HttpServletResponse.SC_OK,response2.getStatus()); SessionHandler m2 = context2.getSessionHandler(); - // Wait for the scavenger to run on node1, waiting 2.5 times the scavenger period - pause(scavengePeriod); + // Wait for the scavenger to run on node1 + pause(inactivePeriod+(2*scavengePeriod)); assertEquals(1, m1.getSessionsCreated()); @@ -116,9 +116,9 @@ public abstract class AbstractLocalSessionScavengingTest assertEquals(1, m1.getSessionsCreated()); - // Wait for the scavenger to run on node2, waiting 2 times the scavenger period + // Wait for the scavenger to run on node2, waiting 3 times the scavenger period // This ensures that the scavenger on node2 runs at least once. - pause(scavengePeriod); + pause(inactivePeriod+(2*scavengePeriod)); // Check that node2 does not have any local session cached request = client.newRequest(urls[1] + "?action=check"); @@ -156,6 +156,7 @@ public abstract class AbstractLocalSessionScavengingTest else if ("test".equals(action)) { HttpSession session = request.getSession(false); + assertNotNull(session); session.setAttribute("test", "test"); } else if ("check".equals(action)) diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractNewSessionTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractNewSessionTest.java index 7ff306f5566..7932d220141 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractNewSessionTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractNewSessionTest.java @@ -62,7 +62,7 @@ public abstract class AbstractNewSessionTest { String servletMapping = "/server"; int scavengePeriod = 3; - AbstractTestServer server = createServer(0, 1, scavengePeriod, 4); + AbstractTestServer server = createServer(0, 1, scavengePeriod, SessionCache.NEVER_EVICT); ServletContextHandler context = server.addContext("/"); context.addServlet(TestServlet.class, servletMapping); String contextPath = ""; diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractProxySerializationTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractProxySerializationTest.java index 66e556c6c68..6a6657c8ae3 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractProxySerializationTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractProxySerializationTest.java @@ -73,7 +73,7 @@ public abstract class AbstractProxySerializationTest String contextPath = ""; String servletMapping = "/server"; int scavengePeriod = 10; - AbstractTestServer server = createServer(0, 20, scavengePeriod, 20); + AbstractTestServer server = createServer(0, 20, scavengePeriod, SessionCache.NEVER_EVICT); ServletContextHandler context = server.addContext(contextPath); InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar"); diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractReentrantRequestSessionTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractReentrantRequestSessionTest.java index 886fe9751a3..6249dc1e793 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractReentrantRequestSessionTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractReentrantRequestSessionTest.java @@ -50,7 +50,7 @@ public abstract class AbstractReentrantRequestSessionTest { String contextPath = ""; String servletMapping = "/server"; - AbstractTestServer server = createServer(0, 100, 400, 10); + AbstractTestServer server = createServer(0, 100, 400, SessionCache.NEVER_EVICT); server.addContext(contextPath).addServlet(TestServlet.class, servletMapping); try { diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java index c73128f3bad..86231aac87c 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractRemoveSessionTest.java @@ -55,9 +55,7 @@ public abstract class AbstractRemoveSessionTest { String contextPath = ""; String servletMapping = "/server"; - int scavengePeriod = 3; - int inspectionPeriod = 1; - AbstractTestServer server = createServer(0, 1, scavengePeriod, -1); + AbstractTestServer server = createServer(0, -1, -1, SessionCache.NEVER_EVICT); ServletContextHandler context = server.addContext(contextPath); context.addServlet(TestServlet.class, servletMapping); TestEventListener testListener = new TestEventListener(); diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCookieTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCookieTest.java index d6f14beb27f..516dd20f6ef 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCookieTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCookieTest.java @@ -64,9 +64,7 @@ public abstract class AbstractSessionCookieTest String contextPath = ""; String servletMapping = "/server"; int scavengePeriod = 3; - int inspectPeriod = 1; - int idlePassivatePeriod = 10; - AbstractTestServer server = createServer(0, 1, scavengePeriod,idlePassivatePeriod); + AbstractTestServer server = createServer(0, 1, scavengePeriod,SessionCache.NEVER_EVICT); ServletContextHandler context = server.addContext(contextPath); context.addServlet(TestServlet.class, servletMapping); diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionExpiryTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionExpiryTest.java index b695b1c514e..278dd31f268 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionExpiryTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionExpiryTest.java @@ -36,6 +36,7 @@ import javax.servlet.http.HttpSessionListener; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.server.session.Session.SessionInactivityTimeout; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.junit.Test; @@ -53,7 +54,7 @@ public abstract class AbstractSessionExpiryTest { try { - Thread.sleep(scavengePeriod * 2500L); + Thread.sleep(scavengePeriod * 1000L); } catch (InterruptedException e) { @@ -78,6 +79,10 @@ public abstract class AbstractSessionExpiryTest }; + /** + * Check session is preserved over stop/start + * @throws Exception + */ @Test public void testSessionNotExpired() throws Exception { @@ -85,9 +90,7 @@ public abstract class AbstractSessionExpiryTest String servletMapping = "/server"; int inactivePeriod = 20; int scavengePeriod = 10; - int inspectPeriod = 1; - int idlePassivatePeriod = 8; - AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod, idlePassivatePeriod); + AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT); TestServlet servlet = new TestServlet(); ServletHolder holder = new ServletHolder(servlet); server1.addContext(contextPath).addServlet(holder, servletMapping); @@ -133,6 +136,11 @@ public abstract class AbstractSessionExpiryTest } + /** + * Check that a session that expires whilst the server is stopped will not be + * able to be used when the server restarts + * @throws Exception + */ @Test public void testSessionExpiry() throws Exception { @@ -140,10 +148,9 @@ public abstract class AbstractSessionExpiryTest String contextPath = ""; String servletMapping = "/server"; - int inactivePeriod = 2; + int inactivePeriod = 4; int scavengePeriod = 1; - int inspectPeriod = 1; - AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod, -1); + AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT); TestServlet servlet = new TestServlet(); ServletHolder holder = new ServletHolder(servlet); ServletContextHandler context = server1.addContext(contextPath); @@ -176,12 +183,15 @@ public abstract class AbstractSessionExpiryTest //now stop the server server1.stop(); - //and wait until the expiry time has passed + //and wait until the session should have expired pause(inactivePeriod); //restart the server server1.start(); + //and wait until the scavenger has run + pause(inactivePeriod+(scavengePeriod*2)); + port1 = server1.getPort(); url = "http://localhost:" + port1 + contextPath + servletMapping; @@ -189,11 +199,10 @@ public abstract class AbstractSessionExpiryTest Request request = client.newRequest(url + "?action=test"); request.getHeaders().add("Cookie", sessionCookie); ContentResponse response2 = request.send(); + assertEquals(HttpServletResponse.SC_OK,response2.getStatus()); - - //and wait until the expiry time has passed - pause(inactivePeriod); - + String cookie2 = response2.getHeaders().get("Set-Cookie"); + assertTrue (!cookie2.equals(sessionCookie)); verifySessionDestroyed (listener, sessionId); } finally diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionInvalidateAndCreateTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionInvalidateAndCreateTest.java index 8f2d259c7d9..b36ea739f54 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionInvalidateAndCreateTest.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionInvalidateAndCreateTest.java @@ -81,7 +81,7 @@ public abstract class AbstractSessionInvalidateAndCreateTest { try { - Thread.sleep(scavengePeriod * 3000L); + Thread.sleep(scavengePeriod * 1000L); } catch (InterruptedException e) { @@ -94,8 +94,8 @@ public abstract class AbstractSessionInvalidateAndCreateTest { String contextPath = ""; String servletMapping = "/server"; - int inactivePeriod = 1; - int scavengePeriod = 2; + int inactivePeriod = 6; + int scavengePeriod = 3; AbstractTestServer server = createServer(0, inactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT); ServletContextHandler context = server.addContext(contextPath); TestServlet servlet = new TestServlet(); @@ -130,8 +130,8 @@ public abstract class AbstractSessionInvalidateAndCreateTest ContentResponse response2 = request2.send(); assertEquals(HttpServletResponse.SC_OK,response2.getStatus()); - // Wait for the scavenger to run, waiting 3 times the scavenger period - pause(scavengePeriod); + // Wait for the scavenger to run + pause(inactivePeriod+scavengePeriod); //test that the session created in the last test is scavenged: //the HttpSessionListener should have been called when session1 was invalidated and session2 was scavenged