diff options
Diffstat (limited to 'tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLastAccessTimeTest.java')
-rw-r--r-- | tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractLastAccessTimeTest.java | 101 |
1 files changed, 60 insertions, 41 deletions
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 bd5b0d6536..1095976342 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 @@ -18,26 +18,36 @@ package org.eclipse.jetty.server.session; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.io.IOException; import java.io.PrintWriter; -import java.util.Random; +import java.util.concurrent.Future; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; -import org.eclipse.jetty.client.ContentExchange; import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.http.HttpMethods; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * AbstractLastAccessTimeTest + * + * This test checks that a session can migrate from node A to node B, kept in use in node B + * past the time at which it would have expired due to inactivity on node A but is NOT + * scavenged by node A. In other words, it tests that a session that migrates from one node + * to another is not timed out on the original node. */ public abstract class AbstractLastAccessTimeTest { @@ -48,10 +58,15 @@ public abstract class AbstractLastAccessTimeTest { String contextPath = ""; String servletMapping = "/server"; - int maxInactivePeriod = 8; - int scavengePeriod = 2; + int maxInactivePeriod = 8; //session will timeout after 8 seconds + int scavengePeriod = 2; //scavenging occurs every 2 seconds AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod); - server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping); + TestServlet servlet1 = new TestServlet(); + ServletHolder holder1 = new ServletHolder(servlet1); + ServletContextHandler context = server1.addContext(contextPath); + TestSessionListener listener1 = new TestSessionListener(); + context.addEventListener(listener1); + context.addServlet(holder1, servletMapping); server1.start(); int port1=server1.getPort(); try @@ -63,19 +78,15 @@ public abstract class AbstractLastAccessTimeTest try { HttpClient client = new HttpClient(); - client.setConnectorType(HttpClient.CONNECTOR_SOCKET); client.start(); try { // Perform one request to server1 to create a session - ContentExchange exchange1 = new ContentExchange(true); - exchange1.setMethod(HttpMethods.GET); - exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping + "?action=init"); - client.send(exchange1); - exchange1.waitForDone(); - assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus()); - assertEquals("test", exchange1.getResponseContent()); - String sessionCookie = exchange1.getResponseFields().getStringField("Set-Cookie"); + Future<ContentResponse> future = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init"); + ContentResponse response1 = future.get(); + assertEquals(HttpServletResponse.SC_OK, response1.getStatus()); + assertEquals("test", response1.getContentAsString()); + String sessionCookie = response1.getHeaders().getStringField("Set-Cookie"); assertTrue( sessionCookie != null ); // Mangle the cookie, replacing Path with $Path, etc. sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); @@ -88,16 +99,14 @@ public abstract class AbstractLastAccessTimeTest int requestInterval = 500; for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i) { - ContentExchange exchange2 = new ContentExchange(true); - exchange2.setMethod(HttpMethods.GET); - exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping); - exchange2.getRequestFields().add("Cookie", sessionCookie); - client.send(exchange2); - exchange2.waitForDone(); - assertEquals(HttpServletResponse.SC_OK , exchange2.getResponseStatus()); - assertEquals("test", exchange2.getResponseContent()); - - String setCookie = exchange1.getResponseFields().getStringField("Set-Cookie"); + Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping); + request.header("Cookie", sessionCookie); + future = request.send(); + ContentResponse response2 = future.get(); + assertEquals(HttpServletResponse.SC_OK , response2.getStatus()); + assertEquals("test", response2.getContentAsString()); + + String setCookie = response2.getHeaders().getStringField("Set-Cookie"); if (setCookie!=null) sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); @@ -108,17 +117,8 @@ public abstract class AbstractLastAccessTimeTest // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period Thread.sleep(scavengePeriod * 2500L); - // Access again server1, and ensure that we can still access the session - exchange1 = new ContentExchange(true); - exchange1.setMethod(HttpMethods.GET); - exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping); - exchange1.getRequestFields().add("Cookie", sessionCookie); - client.send(exchange1); - exchange1.waitForDone(); - assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus()); - //test that the session was kept alive by server 2 and still contains what server1 put in it - assertEquals("test", exchange1.getResponseContent()); - + //check that the session was not scavenged over on server1 by ensuring that the SessionListener destroy method wasn't called + assertTrue (listener1.destroyed == false); } finally { @@ -136,8 +136,30 @@ public abstract class AbstractLastAccessTimeTest } } + public static class TestSessionListener implements HttpSessionListener + { + public boolean destroyed = false; + public boolean created = false; + + @Override + public void sessionDestroyed(HttpSessionEvent se) + { + destroyed = true; + } + + @Override + public void sessionCreated(HttpSessionEvent se) + { + created = true; + } + } + + + public static class TestServlet extends HttpServlet { + + @Override protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { @@ -146,7 +168,6 @@ public abstract class AbstractLastAccessTimeTest { HttpSession session = request.getSession(true); session.setAttribute("test", "test"); - sendResult(session, httpServletResponse.getWriter()); } @@ -161,8 +182,6 @@ public abstract class AbstractLastAccessTimeTest { session.setAttribute("test", "test"); } - - } } |