Tidy up session tests, add more comments.

This commit is contained in:
Jan Bartel 2012-02-10 12:28:44 +11:00
parent f7fd3a4680
commit 7aff671bae
5 changed files with 27 additions and 22 deletions

View File

@ -26,6 +26,7 @@ import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http.HttpMethods; import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -46,10 +47,14 @@ public abstract class AbstractClientCrossContextSessionTest
String contextB = "/contextB"; String contextB = "/contextB";
String servletMapping = "/server"; String servletMapping = "/server";
AbstractTestServer server = createServer(0); AbstractTestServer server = createServer(0);
TestServletA servletA = new TestServletA();
ServletHolder holderA = new ServletHolder(servletA);
ServletContextHandler ctxA = server.addContext(contextA); ServletContextHandler ctxA = server.addContext(contextA);
ctxA.addServlet(TestServletA.class, servletMapping); ctxA.addServlet(holderA, servletMapping);
ServletContextHandler ctxB = server.addContext(contextB); ServletContextHandler ctxB = server.addContext(contextB);
ctxB.addServlet(TestServletB.class, servletMapping); TestServletB servletB = new TestServletB();
ServletHolder holderB = new ServletHolder(servletB);
ctxB.addServlet(holderB, servletMapping);
server.start(); server.start();
int port = server.getPort(); int port = server.getPort();
@ -80,6 +85,7 @@ public abstract class AbstractClientCrossContextSessionTest
client.send(exchangeB); client.send(exchangeB);
exchangeB.waitForDone(); exchangeB.waitForDone();
assertEquals(HttpServletResponse.SC_OK,exchangeB.getResponseStatus()); assertEquals(HttpServletResponse.SC_OK,exchangeB.getResponseStatus());
assertEquals(servletA.sessionId, servletB.sessionId);
} }
finally finally
{ {
@ -94,11 +100,17 @@ public abstract class AbstractClientCrossContextSessionTest
public static class TestServletA extends HttpServlet public static class TestServletA extends HttpServlet
{ {
public String sessionId;
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
if (session == null) session = request.getSession(true); if (session == null)
{
session = request.getSession(true);
sessionId = session.getId();
}
// Add something to the session // Add something to the session
session.setAttribute("A", "A"); session.setAttribute("A", "A");
@ -106,17 +118,22 @@ public abstract class AbstractClientCrossContextSessionTest
// Check that we don't see things put in session by contextB // Check that we don't see things put in session by contextB
Object objectB = session.getAttribute("B"); Object objectB = session.getAttribute("B");
assertTrue(objectB == null); assertTrue(objectB == null);
System.out.println("A: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
} }
} }
public static class TestServletB extends HttpServlet public static class TestServletB extends HttpServlet
{ {
public String sessionId;
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
{ {
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
if (session == null) session = request.getSession(true); if (session == null)
{
session = request.getSession(true);
sessionId = session.getId();
}
// Add something to the session // Add something to the session
session.setAttribute("B", "B"); session.setAttribute("B", "B");
@ -124,7 +141,6 @@ public abstract class AbstractClientCrossContextSessionTest
// Check that we don't see things put in session by contextA // Check that we don't see things put in session by contextA
Object objectA = session.getAttribute("A"); Object objectA = session.getAttribute("A");
assertTrue(objectA == null); assertTrue(objectA == null);
System.out.println("B: session.getAttributeNames() = " + Collections.list(session.getAttributeNames()));
} }
} }
} }

View File

@ -44,6 +44,7 @@ public abstract class AbstractImmortalSessionTest
String contextPath = ""; String contextPath = "";
String servletMapping = "/server"; String servletMapping = "/server";
int scavengePeriod = 2; int scavengePeriod = 2;
//turn off session expiry by setting maxInactiveInterval to -1
AbstractTestServer server = createServer(0, -1, scavengePeriod); AbstractTestServer server = createServer(0, -1, scavengePeriod);
server.addContext(contextPath).addServlet(TestServlet.class, servletMapping); server.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
server.start(); server.start();

View File

@ -98,13 +98,11 @@ public abstract class AbstractLastAccessTimeTest
Thread.sleep(requestInterval); Thread.sleep(requestInterval);
} }
System.out.println("Waiting for scavenging on node1...");
// At this point, session1 should be eligible for expiration. // At this point, session1 should be eligible for expiration.
// Let's wait for the scavenger to run, waiting 2.5 times the scavenger period // Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
Thread.sleep(scavengePeriod * 2500L); Thread.sleep(scavengePeriod * 2500L);
// Access again server1, and be sure we can // Access again server1, and ensure that we can still access the session
exchange1 = new ContentExchange(true); exchange1 = new ContentExchange(true);
exchange1.setMethod(HttpMethods.GET); exchange1.setMethod(HttpMethods.GET);
exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping); exchange1.setURL("http://localhost:" + port1 + contextPath + servletMapping);
@ -112,6 +110,8 @@ public abstract class AbstractLastAccessTimeTest
client.send(exchange1); client.send(exchange1);
exchange1.waitForDone(); exchange1.waitForDone();
assertEquals(HttpServletResponse.SC_OK, exchange1.getResponseStatus()); 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());
} }
finally finally

View File

@ -84,19 +84,7 @@ public abstract class AbstractOrphanedSessionTest
// must be removed by scavenging done in the other node. // must be removed by scavenging done in the other node.
Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod)); Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod));
System.err.println("FINISHED waiting for session to expire");
// Perform one request to server2 to be sure that the session has been expired // Perform one request to server2 to be sure that the session has been expired
//
// force invalidate to test
// ContentExchange exchange3 = new ContentExchange(true);
// exchange3.setMethod(HttpMethods.GET);
// exchange3.setURL("http://localhost:" + port2 + contextPath + servletMapping + "?action=remove");
// exchange3.getRequestFields().add("Cookie", sessionCookie);
// client.send(exchange3);
// exchange3.waitForDone();
System.err.println("CHECKING NODE2");
ContentExchange exchange2 = new ContentExchange(true); ContentExchange exchange2 = new ContentExchange(true);
exchange2.setMethod(HttpMethods.GET); exchange2.setMethod(HttpMethods.GET);
exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping + "?action=check"); exchange2.setURL("http://localhost:" + port2 + contextPath + servletMapping + "?action=check");

View File

@ -92,7 +92,7 @@ public abstract class AbstractReentrantRequestSessionTest
int port = Integer.parseInt(request.getParameter("port")); int port = Integer.parseInt(request.getParameter("port"));
String path = request.getParameter("path"); String path = request.getParameter("path");
// We want to make another request with a different session // We want to make another request
// while this request is still pending, to see if the locking is // while this request is still pending, to see if the locking is
// fine grained (per session at least). // fine grained (per session at least).
try try