From d82b5ad65a62ccd49266c0a9dc20342478068c53 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Fri, 11 Mar 2016 15:11:23 +1100 Subject: [PATCH] Issue #411 Add more debug log for mongosessionmanager and remove debug printlns --- .../jetty/server/session/AbstractSession.java | 4 +- .../session/AbstractSessionManager.java | 4 +- .../jetty/nosql/mongodb/IdleSessionTest.java | 7 +- .../nosql/mongodb/SessionExpiryTest.java | 75 +++++++++++++++++++ 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java index 7d8d5b29e92..779e07b6707 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSession.java @@ -557,8 +557,10 @@ public abstract class AbstractSession implements AbstractSessionManager.SessionI @Override public void setMaxInactiveInterval(int secs) { - if (secs < 0) + if (secs <= 0) LOG.warn("Session {} is now immortal (maxInactiveInterval={})", _clusterId, secs); + else if (LOG.isDebugEnabled()) + LOG.debug("Session {} maxInactiveInterval={}", _clusterId, secs); _maxIdleMs=(long)secs*1000L; } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionManager.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionManager.java index 1dcec10c530..e2194a83299 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionManager.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionManager.java @@ -627,8 +627,10 @@ public abstract class AbstractSessionManager extends ContainerLifeCycle implemen public void setMaxInactiveInterval(int seconds) { _dftMaxIdleSecs=seconds; - if (_dftMaxIdleSecs < 0) + if (_dftMaxIdleSecs <= 0) __log.warn("Sessions created by this manager are immortal (default maxInactiveInterval={})"+_dftMaxIdleSecs); + else if (__log.isDebugEnabled()) + __log.debug("SessionManager default maxInactiveInterval={}", _dftMaxIdleSecs); } /* ------------------------------------------------------------ */ diff --git a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/IdleSessionTest.java b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/IdleSessionTest.java index 1116e21c738..94db7a95a49 100644 --- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/IdleSessionTest.java +++ b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/IdleSessionTest.java @@ -139,7 +139,6 @@ public class IdleSessionTest //Test trying to de-idle an expired session (ie before the scavenger can get to it) - System.err.println("\n TESTING DEIDLE EXPIRED\n"); //make a request to set up a session on the server response = client.GET(url + "?action=init"); assertEquals(HttpServletResponse.SC_OK,response.getStatus()); @@ -153,14 +152,12 @@ public class IdleSessionTest //stop the scavenger ((MongoTestServer.TestMongoSessionIdManager)server1.getServer().getSessionIdManager()).cancelScavenge(); - System.err.println("SCAVENGE STOPPED"); + //check that the session is idle checkSessionIdle(); - - System.err.println("WAITING FOR EXPIRY TIME TO PASS"); + //wait until the session should be expired pause (inactivePeriod + (inactivePeriod/2)); - System.err.println("EXPIRY TIME PASSED"); //make a request to try and deidle the session //make another request to de-idle the session diff --git a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionExpiryTest.java b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionExpiryTest.java index b6d289d1f74..de947b36d96 100644 --- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionExpiryTest.java +++ b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionExpiryTest.java @@ -191,6 +191,55 @@ public class SessionExpiryTest extends AbstractSessionExpiryTest } + @Test + public void testChangeNewSessionTimeout () throws Exception + { + String contextPath = ""; + String servletMapping = "/server"; + int inactivePeriod = 10; + int scavengePeriod = 1; + AbstractTestServer server1 = createServer(0, inactivePeriod, scavengePeriod); + ImmediateChangeTimeoutServlet servlet = new ImmediateChangeTimeoutServlet(); + ServletHolder holder = new ServletHolder(servlet); + ServletContextHandler context = server1.addContext(contextPath); + context.addServlet(holder, servletMapping); + TestHttpSessionListener listener = new TestHttpSessionListener(); + + context.getSessionHandler().addEventListener(listener); + + server1.start(); + int port1 = server1.getPort(); + + try + { + HttpClient client = new HttpClient(); + client.start(); + String url = "http://localhost:" + port1 + contextPath + servletMapping; + + inactivePeriod = 5; //change from the sessionmanager configured default + + //make a request to set up a session on the server and change its inactive setting straight away + ContentResponse response1 = client.GET(url + "?action=init&val="+inactivePeriod); + assertEquals(HttpServletResponse.SC_OK,response1.getStatus()); + String sessionCookie = response1.getHeaders().get("Set-Cookie"); + assertTrue(sessionCookie != null); + // Mangle the cookie, replacing Path with $Path, etc. + sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); + + String sessionId = AbstractTestServer.extractSessionId(sessionCookie); + + DBCollection sessions = ((MongoSessionIdManager)((MongoTestServer)server1).getServer().getSessionIdManager()).getSessions(); + verifySessionCreated(listener,sessionId); + //verify that the session timeout is the new value and not the default + verifySessionTimeout(sessions, sessionId, inactivePeriod); + } + finally + { + server1.stop(); + } + } + + public void verifySessionTimeout (DBCollection sessions, String id, int sec) throws Exception { assertNotNull(sessions); @@ -268,5 +317,31 @@ public class SessionExpiryTest extends AbstractSessionExpiryTest } } } + + public static class ImmediateChangeTimeoutServlet extends HttpServlet + { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException + { + String action = request.getParameter("action"); + if ("init".equals(action)) + { + HttpSession session = request.getSession(true); + assertNotNull(session); + String tmp = request.getParameter("val"); + int val = (StringUtil.isBlank(tmp)?0:Integer.valueOf(tmp.trim())); + session.setMaxInactiveInterval(val); + } + else if ("change".equals(action)) + { + String tmp = request.getParameter("val"); + int val = (StringUtil.isBlank(tmp)?0:Integer.valueOf(tmp.trim())); + HttpSession session = request.getSession(false); + assertNotNull(session); + session.setMaxInactiveInterval(val); + } + } + } }