attributes)
+ throws Exception
+ {
+
+ DBCollection collection = _mongoClient.getDB(DB_NAME).getCollection(COLLECTION_NAME);
+
+ // Form query for upsert
+ BasicDBObject key = new BasicDBObject(MongoSessionDataStore.__ID, id);
+
+ // Form updates
+ BasicDBObject update = new BasicDBObject();
+ boolean upsert = false;
+ BasicDBObject sets = new BasicDBObject();
+
+ Object version = new Long(1);
+
+ // New session
+ upsert = true;
+ sets.put(MongoSessionDataStore.__CREATED,created);
+ sets.put(MongoSessionDataStore.__VALID,true);
+ sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__VERSION,version);
+ sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__LASTSAVED, System.currentTimeMillis());
+ sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__LASTNODE, lastNode);
+ sets.put(MongoSessionDataStore.__MAX_IDLE, maxIdle);
+ sets.put(MongoSessionDataStore.__EXPIRY, expiry);
+ sets.put(MongoSessionDataStore.__ACCESSED, accessed);
+ sets.put(MongoSessionDataStore.__LAST_ACCESSED, lastAccessed);
+
+ if (attributes != null)
+ {
+ for (String name : attributes.keySet())
+ {
+ Object value = attributes.get(name);
+ sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath+ "." + MongoUtils.encodeName(name),
+ MongoUtils.encodeName(value));
+ }
+ }
+ update.put("$set",sets);
+ collection.update(key,update,upsert,false,WriteConcern.SAFE);
+ }
+
+
+
+
+
}
diff --git a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/NonClusteredSessionScavengingTest.java b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/NonClusteredSessionScavengingTest.java
deleted file mode 100644
index 0dd005ad43d..00000000000
--- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/NonClusteredSessionScavengingTest.java
+++ /dev/null
@@ -1,84 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-package org.eclipse.jetty.nosql.mongodb;
-
-import static org.junit.Assert.fail;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-
-import org.eclipse.jetty.server.session.AbstractNonClusteredSessionScavengingTest;
-import org.eclipse.jetty.server.session.SessionDataStoreFactory;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-
-/**
- * NonClusteredSessionScavengingTest
- */
-public class NonClusteredSessionScavengingTest extends AbstractNonClusteredSessionScavengingTest
-{
-
- @BeforeClass
- public static void beforeClass() throws Exception
- {
- MongoTestHelper.dropCollection();
- MongoTestHelper.createCollection();
- }
-
- @AfterClass
- public static void afterClass() throws Exception
- {
- MongoTestHelper.dropCollection();
- }
-
-
-
-
-
- /**
- * @see org.eclipse.jetty.server.session.AbstractNonClusteredSessionScavengingTest#assertSession(java.lang.String, boolean)
- */
- @Override
- public void assertSession(String id, boolean exists)
- {
- assertNotNull(_dataStore);
- try
- {
- boolean inmap = _dataStore.exists(id);
- if (exists)
- assertTrue(inmap);
- else
- assertFalse(inmap);
- }
- catch (Exception e)
- {
- fail(e.getMessage());
- }
- }
-
- /**
- * @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory()
- */
- @Override
- public SessionDataStoreFactory createSessionDataStoreFactory()
- {
- return MongoTestHelper.newSessionDataStoreFactory();
- }
-}
diff --git a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionDump.java b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionDump.java
deleted file mode 100644
index 461e6bc4e84..00000000000
--- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionDump.java
+++ /dev/null
@@ -1,188 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-package org.eclipse.jetty.nosql.mongodb;
-
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.Date;
-import java.util.Enumeration;
-
-import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-
-/* ------------------------------------------------------------ */
-/** Test Servlet Sessions.
- *
- *
- */
-public class SessionDump extends HttpServlet
-{
-
- int redirectCount=0;
- /* ------------------------------------------------------------ */
- String pageType;
-
- /* ------------------------------------------------------------ */
- @Override
- public void init(ServletConfig config)
- throws ServletException
- {
- super.init(config);
- }
-
- /* ------------------------------------------------------------ */
- protected void handleForm(HttpServletRequest request,
- HttpServletResponse response)
- {
- HttpSession session = request.getSession(false);
- String action = request.getParameter("Action");
- String name = request.getParameter("Name");
- String value = request.getParameter("Value");
-
- if (action!=null)
- {
- if(action.equals("New Session"))
- {
- session = request.getSession(true);
- session.setAttribute("test","value");
- }
- else if (session!=null)
- {
- if (action.equals("Invalidate"))
- session.invalidate();
- else if (action.equals("Set") && name!=null && name.length()>0)
- session.setAttribute(name,value);
- else if (action.equals("Remove"))
- session.removeAttribute(name);
- }
- }
- }
-
- /* ------------------------------------------------------------ */
- @Override
- public void doPost(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException
- {
- handleForm(request,response);
- String nextUrl = getURI(request)+"?R="+redirectCount++;
- String encodedUrl=response.encodeRedirectURL(nextUrl);
- response.sendRedirect(encodedUrl);
- }
-
- /* ------------------------------------------------------------ */
- @Override
- public void doGet(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException
- {
- handleForm(request,response);
-
- response.setContentType("text/html");
-
- HttpSession session = request.getSession(getURI(request).indexOf("new")>0);
- try
- {
- if (session!=null)
- session.isNew();
- }
- catch(IllegalStateException e)
- {
- session=null;
- }
-
- PrintWriter out = response.getWriter();
- out.println("Session Dump Servlet:
");
- out.println("
");
-
- if (request.isRequestedSessionIdFromCookie())
- out.println("Turn off cookies in your browser to try url encoding
");
-
- if (request.isRequestedSessionIdFromURL())
- out.println("
Turn on cookies in your browser to try cookie encoding
");
- out.println("Encoded Link
");
-
- }
- catch (IllegalStateException e)
- {
- e.printStackTrace();
- }
- }
-
- }
-
- /* ------------------------------------------------------------ */
- @Override
- public String getServletInfo() {
- return "Session Dump Servlet";
- }
-
- /* ------------------------------------------------------------ */
- private String getURI(HttpServletRequest request)
- {
- String uri=(String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
- if (uri==null)
- uri=request.getRequestURI();
- return uri;
- }
-
-}
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
deleted file mode 100644
index b655f00671e..00000000000
--- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionExpiryTest.java
+++ /dev/null
@@ -1,385 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-package org.eclipse.jetty.nosql.mongodb;
-
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-
-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 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.AbstractSessionExpiryTest;
-import org.eclipse.jetty.server.session.DefaultSessionCacheFactory;
-import org.eclipse.jetty.server.session.SessionCache;
-import org.eclipse.jetty.server.session.SessionDataStoreFactory;
-import org.eclipse.jetty.server.session.TestServer;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.servlet.ServletHolder;
-import org.eclipse.jetty.util.StringUtil;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import com.mongodb.BasicDBObject;
-import com.mongodb.DBCollection;
-import com.mongodb.DBObject;
-
-
-
-
-public class SessionExpiryTest extends AbstractSessionExpiryTest
-{
-
-
- @BeforeClass
- public static void beforeClass() throws Exception
- {
- MongoTestHelper.dropCollection();
- MongoTestHelper.createCollection();
- }
-
- @AfterClass
- public static void afterClass() throws Exception
- {
- MongoTestHelper.dropCollection();
- }
- /**
- * @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory()
- */
- @Override
- public SessionDataStoreFactory createSessionDataStoreFactory()
- {
- return MongoTestHelper.newSessionDataStoreFactory();
- }
-
- @Test
- @Override
- public void testSessionNotExpired() throws Exception
- {
- super.testSessionNotExpired();
- }
-
- @Test
- @Override
- public void testSessionExpiry() throws Exception
- {
- super.testSessionExpiry();
- }
-
- @Test
- @Override
- public void testRequestForSessionWithChangedTimeout() throws Exception
- {
- super.testRequestForSessionWithChangedTimeout();
- }
-
- @Test
- public void testBigSessionExpiry() throws Exception
- {
- String contextPath = "";
- String servletMapping = "/server";
- int inactivePeriod = Integer.MAX_VALUE * 60; //integer overflow
- int scavengePeriod = 10;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
-
- MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
- storeFactory.setGracePeriodSec(scavengePeriod);
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
- ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
- 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;
-
- //make a request to set up a session on the server
- ContentResponse response1 = client.GET(url + "?action=init");
- 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 = TestServer.extractSessionId(sessionCookie);
-
- DBCollection sessions = MongoTestHelper.getCollection();
- verifySessionCreated(listener,sessionId);
- //verify that the session timeout is set in mongo
- verifySessionTimeout(sessions, sessionId, -1); //SessionManager sets -1 if maxInactive < 0
-
- //get the session expiry time from mongo
- long expiry = getSessionExpiry(sessions, sessionId);
- assertEquals(0, expiry);
-
- }
- finally
- {
- server1.stop();
- }
- }
-
- @Test
- public void changeSessionTimeout() throws Exception
- {
- String contextPath = "";
- String servletMapping = "/server";
- int inactivePeriod = 10;
- int scavengePeriod = 1;
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
- storeFactory.setGracePeriodSec(scavengePeriod);
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
- ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
- 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;
-
- //make a request to set up a session on the server
- ContentResponse response1 = client.GET(url + "?action=init");
- 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 = TestServer.extractSessionId(sessionCookie);
-
- DBCollection sessions = MongoTestHelper.getCollection();
- verifySessionCreated(listener,sessionId);
- //verify that the session timeout is set in mongo
- verifySessionTimeout(sessions, sessionId, inactivePeriod);
-
- //get the session expiry time from mongo
- long expiry = getSessionExpiry(sessions, sessionId);
- //make another request to change the session timeout to a smaller value
- inactivePeriod = 5;
- Request request = client.newRequest(url + "?action=change&val="+inactivePeriod);
- request.getHeaders().add("Cookie", sessionCookie);
- ContentResponse response2 = request.send();
- assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
-
-
- //check the timeout in mongo
- verifySessionTimeout(sessions, sessionId, inactivePeriod);
- //check the session expiry time has decreased from previous value
- assertTrue(getSessionExpiry(sessions, sessionId) expiry);
- assertTrue(getSessionAccessed(sessions, sessionId)+ (1000L*inactivePeriod) <= getSessionExpiry(sessions, sessionId));
- assertTrue (latestExpiry >= 15);//old inactive expired in 5, new inactive expired in 20
- }
- finally
- {
- server1.stop();
- }
- }
-
-
- @Test
- public void testChangeNewSessionTimeout () throws Exception
- {
- String contextPath = "";
- String servletMapping = "/server";
- int inactivePeriod = 10;
- int scavengePeriod = 1;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
- storeFactory.setGracePeriodSec(scavengePeriod);
-
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,cacheFactory, storeFactory);
- 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 = TestServer.extractSessionId(sessionCookie);
-
- DBCollection sessions = MongoTestHelper.getCollection();
- 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
- {
- long val;
-
- if (sec > 0)
- val = sec*1000L;
- else
- val = sec;
-
- assertNotNull(sessions);
- assertNotNull(id);
-
- DBObject o = sessions.findOne(new BasicDBObject(MongoSessionDataStore.__ID,id));
- assertNotNull(o);
- Long maxIdle = (Long)o.get(MongoSessionDataStore.__MAX_IDLE);
- assertNotNull(maxIdle);
- assertEquals(val, maxIdle.longValue());
- }
-
- public long getSessionExpiry (DBCollection sessions, String id) throws Exception
- {
- assertNotNull(sessions);
- assertNotNull(id);
-
- DBObject o = sessions.findOne(new BasicDBObject(MongoSessionDataStore.__ID,id));
- assertNotNull(o);
- Long expiry = (Long)o.get(MongoSessionDataStore.__EXPIRY);
- return (expiry == null? null : expiry.longValue());
- }
-
- public long getSessionMaxInactiveInterval (DBCollection sessions, String id) throws Exception
- {
- assertNotNull(sessions);
- assertNotNull(id);
-
- DBObject o = sessions.findOne(new BasicDBObject(MongoSessionDataStore.__ID,id));
- assertNotNull(o);
- Long inactiveInterval = (Long)o.get(MongoSessionDataStore.__MAX_IDLE);
- return (inactiveInterval == null? null : inactiveInterval.longValue());
- }
-
- public long getSessionAccessed (DBCollection sessions, String id) throws Exception
- {
- assertNotNull(sessions);
- assertNotNull(id);
-
- DBObject o = sessions.findOne(new BasicDBObject(MongoSessionDataStore.__ID,id));
- assertNotNull(o);
- Long accessed = (Long)o.get(MongoSessionDataStore.__ACCESSED);
- return (accessed == null? null : accessed.longValue());
- }
-
- public void debugPrint (DBCollection sessions, String id) throws Exception
- {
- assertNotNull(sessions);
- assertNotNull(id);
-
- DBObject o = sessions.findOne(new BasicDBObject(MongoSessionDataStore.__ID,id));
- assertNotNull(o);
- System.err.println(o);
- }
-
-
- 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);
- }
- }
- }
-
-}
diff --git a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionInvalidateCreateScavengeTest.java b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionInvalidateCreateScavengeTest.java
deleted file mode 100644
index 90a8f579d7c..00000000000
--- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/SessionInvalidateCreateScavengeTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-package org.eclipse.jetty.nosql.mongodb;
-
-import org.eclipse.jetty.server.session.AbstractSessionInvalidateCreateScavengeTest;
-import org.eclipse.jetty.server.session.SessionDataStoreFactory;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class SessionInvalidateCreateScavengeTest extends AbstractSessionInvalidateCreateScavengeTest
-{
-
-
- @BeforeClass
- public static void beforeClass() throws Exception
- {
- MongoTestHelper.dropCollection();
- MongoTestHelper.createCollection();
- }
-
- @AfterClass
- public static void afterClass() throws Exception
- {
- MongoTestHelper.dropCollection();
- }
-
-
- /**
- * @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory()
- */
- @Override
- public SessionDataStoreFactory createSessionDataStoreFactory()
- {
- return MongoTestHelper.newSessionDataStoreFactory();
- }
-
-
- @Test
- @Override
- public void testSessionScavenge() throws Exception
- {
- super.testSessionScavenge();
- }
-}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredLastAccessTimeTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredLastAccessTimeTest.java
deleted file mode 100644
index b7d7d2371e3..00000000000
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredLastAccessTimeTest.java
+++ /dev/null
@@ -1,238 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-package org.eclipse.jetty.server.session;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.HashSet;
-import java.util.Set;
-
-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.HttpClient;
-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;
-
-
-/**
- * AbstractClusteredLastAccessTimeTest
- *
- * 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 AbstractClusteredLastAccessTimeTest extends AbstractTestBase
-{
-
- @Test
- public void testLastAccessTime() throws Exception
- {
- String contextPath = "/";
- String servletMapping = "/server";
- int maxInactivePeriod = 8; //session will timeout after 8 seconds
- int scavengePeriod = 2; //scavenging occurs every 2 seconds
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
- ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
- TestServer server1 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
- TestServlet servlet1 = new TestServlet();
- ServletHolder holder1 = new ServletHolder(servlet1);
- ServletContextHandler context = server1.addContext(contextPath);
- TestSessionListener listener1 = new TestSessionListener();
- context.getSessionHandler().addEventListener(listener1);
- context.addServlet(holder1, servletMapping);
- SessionHandler m1 = context.getSessionHandler();
-
-
- try
- {
- server1.start();
- int port1=server1.getPort();
-
- TestServer server2 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
- ServletContextHandler context2 = server2.addContext(contextPath);
- context2.addServlet(TestServlet.class, servletMapping);
- SessionHandler m2 = context2.getSessionHandler();
-
-
- try
- {
- server2.start();
- int port2=server2.getPort();
- HttpClient client = new HttpClient();
- client.start();
- try
- {
- // Perform one request to server1 to create a session
- ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping.substring(1) + "?action=init");
- assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
- assertEquals("test", response1.getContentAsString());
- String sessionCookie = response1.getHeaders().get("Set-Cookie");
- assertTrue( sessionCookie != null );
- assertEquals(1, ((DefaultSessionCache)m1.getSessionCache()).getSessionsCurrent());
- assertEquals(1, ((DefaultSessionCache)m1.getSessionCache()).getSessionsMax());
- assertEquals(1, ((DefaultSessionCache)m1.getSessionCache()).getSessionsTotal());
- // Mangle the cookie, replacing Path with $Path, etc.
- sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
-
- // Perform some request to server2 using the session cookie from the previous request
- // This should migrate the session from server1 to server2, and leave server1's
- // session in a very stale state, while server2 has a very fresh session.
- // We want to test that optimizations done to the saving of the shared lastAccessTime
- // do not break the correct working
- int requestInterval = 500;
- for (int i = 0; i < maxInactivePeriod * (1000 / requestInterval); ++i)
- {
- Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping.substring(1));
- request.header("Cookie", sessionCookie);
- ContentResponse response2 = request.send();
- assertEquals(HttpServletResponse.SC_OK , response2.getStatus());
- assertEquals("test", response2.getContentAsString());
-
- String setCookie = response2.getHeaders().get("Set-Cookie");
- if (setCookie!=null)
- sessionCookie = setCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
-
- Thread.sleep(requestInterval);
- assertSessionCounts(1,1,1, m2);
- }
-
- // 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(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(TestServer.extractSessionId(sessionCookie)));
- assertAfterScavenge(m1);
- }
- finally
- {
- client.stop();
- }
- }
- finally
- {
- server2.stop();
- }
- }
- finally
- {
- server1.stop();
- }
- }
-
- public void assertAfterSessionCreated (SessionHandler m)
- {
- assertSessionCounts(1, 1, 1, m);
- }
-
- public void assertAfterScavenge (SessionHandler manager)
- {
- assertSessionCounts(1,1,1, manager);
- }
-
- public void assertSessionCounts (int current, int max, int total, SessionHandler manager)
- {
- assertEquals(current, ((DefaultSessionCache)manager.getSessionCache()).getSessionsCurrent());
- assertEquals(max, ((DefaultSessionCache)manager.getSessionCache()).getSessionsMax());
- assertEquals(total, ((DefaultSessionCache)manager.getSessionCache()).getSessionsTotal());
- }
-
- public static class TestSessionListener implements HttpSessionListener
- {
- public Set _creates = new HashSet();
- public Set _destroys = new HashSet();
-
- @Override
- public void sessionDestroyed(HttpSessionEvent se)
- {
- _destroys.add(se.getSession().getId());
- }
-
- @Override
- public void sessionCreated(HttpSessionEvent se)
- {
- _creates.add(se.getSession().getId());
- }
- }
-
-
-
- public static class TestServlet extends HttpServlet
- {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- @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);
- session.setAttribute("test", "test");
- sendResult(session, httpServletResponse.getWriter());
-
- }
- else
- {
- HttpSession session = request.getSession(false);
-
- // if we node hopped we should get the session and test should already be present
- sendResult(session, httpServletResponse.getWriter());
-
- if (session!=null)
- {
- session.setAttribute("test", "test");
- }
- }
- }
-
- private void sendResult(HttpSession session, PrintWriter writer)
- {
- if (session != null)
- {
- writer.print(session.getAttribute("test"));
- }
- else
- {
- writer.print("null");
- }
- }
- }
-}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredSessionMigrationTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredSessionMigrationTest.java
deleted file mode 100644
index da986252ed2..00000000000
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredSessionMigrationTest.java
+++ /dev/null
@@ -1,148 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-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 javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-import org.eclipse.jetty.client.HttpClient;
-import org.eclipse.jetty.client.api.ContentResponse;
-import org.eclipse.jetty.client.api.Request;
-import org.junit.Test;
-
-/**
- * AbstractClusteredSessionMigrationTest
- *
- * Check that a session that is active on node 1 can be accessed on node2.
- */
-public abstract class AbstractClusteredSessionMigrationTest extends AbstractTestBase
-{
-
-
- @Test
- public void testSessionMigration() throws Exception
- {
- String contextPath = "/";
- String servletMapping = "/server";
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
- ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
-
- TestServer server1 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC,
- cacheFactory, storeFactory);
- server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
-
- try
- {
- server1.start();
- int port1=server1.getPort();
-
- TestServer server2 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC,
- cacheFactory, storeFactory);
- server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
-
- try
- {
- server2.start();
- int port2=server2.getPort();
-
- HttpClient client = new HttpClient();
- client.start();
- try
- {
- // Perform one request to server1 to create a session
- int value = 1;
- Request request1 = client.POST("http://localhost:" + port1 + contextPath + servletMapping.substring(1) + "?action=set&value=" + value);
- ContentResponse response1 = request1.send();
- 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=");
-
- // Perform a request to server2 using the session cookie from the previous request
- // This should migrate the session from server1 to server2.
- Request request2 = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping.substring(1) + "?action=get");
- request2.header("Cookie", sessionCookie);
- ContentResponse response2 = request2.send();
- assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
- String response = response2.getContentAsString();
- assertEquals(response.trim(),String.valueOf(value)); }
- finally
- {
- client.stop();
- }
- }
- finally
- {
- server2.stop();
- }
- }
- finally
- {
- server1.stop();
- }
- }
-
- public static class TestServlet extends HttpServlet
- {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- doPost(request, response);
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- HttpSession session = request.getSession(false);
-
- String action = request.getParameter("action");
- if ("set".equals(action))
- {
- if (session == null) session = request.getSession(true);
- int value = Integer.parseInt(request.getParameter("value"));
- session.setAttribute("value", value);
- PrintWriter writer = response.getWriter();
- writer.println(value);
- writer.flush();
- }
- else if ("get".equals(action))
- {
- int value = (Integer)session.getAttribute("value");
- int x = session.getMaxInactiveInterval();
- assertTrue(x > 0);
- PrintWriter writer = response.getWriter();
- writer.println(value);
- writer.flush();
- }
- }
- }
-}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredSessionScavengingTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredSessionScavengingTest.java
index 26f84a15fb9..e7b2089d4ec 100644
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredSessionScavengingTest.java
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractClusteredSessionScavengingTest.java
@@ -19,10 +19,15 @@
package org.eclipse.jetty.server.session;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -34,195 +39,116 @@ 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.servlet.ServletContextHandler;
-import org.eclipse.jetty.util.statistic.CounterStatistic;
+import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test;
/**
* AbstractClusteredSessionScavengingTest
+ *
+ * Test that a session that was live on node1, but then more
+ * recently used on node2 does not expire over on node1.
*/
public abstract class AbstractClusteredSessionScavengingTest extends AbstractTestBase
{
- public void pause(int scavengePeriod)
+ public void pause(int secs)
+ throws InterruptedException
{
- try
- {
- Thread.sleep(scavengePeriod * 1000L);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
+ Thread.sleep(TimeUnit.SECONDS.toMillis(secs));
}
-
-
- public static class SessionListener implements HttpSessionListener
- {
- public CounterStatistic count = new CounterStatistic();
- /**
- * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
- */
- @Override
- public void sessionCreated(HttpSessionEvent se)
- {
- count.increment();
- }
- /**
- * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
- */
- @Override
- public void sessionDestroyed(HttpSessionEvent se)
- {
- count.decrement();
- }
- }
+
+
+
@Test
- public void testNoScavenging() throws Exception
+ public void testClusteredScavenge() throws Exception
{
String contextPath = "/";
String servletMapping = "/server";
- int inactivePeriod = 3;
- int scavengePeriod = 0;
-
+ int maxInactivePeriod = 5; //session will timeout after 5 seconds
+ int scavengePeriod = 1; //scavenging occurs every 1 seconds
+
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
-
-
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
- ServletContextHandler context1 = server1.addContext(contextPath);
- context1.addServlet(TestServlet.class, servletMapping);
- SessionListener listener = new SessionListener();
- context1.getSessionHandler().addEventListener(listener);
-
-
- try
- {
- server1.start();
- int port1 = server1.getPort();
-
- HttpClient client = new HttpClient();
- client.start();
- try
- {
- String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
-
-
- // Create the session
- ContentResponse response1 = client.GET(url + "?action=init");
- assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
- String sessionCookie = response1.getHeaders().get("Set-Cookie");
- assertTrue(sessionCookie != null);
- SessionHandler m1 = context1.getSessionHandler();
- assertEquals(1, m1.getSessionsCreated());
-
-
- // Wait a while to ensure that the session should have expired, if the
- //scavenger was running
- pause(2*inactivePeriod);
-
- assertEquals(1, m1.getSessionsCreated());
-
-
- if (m1 instanceof TestSessionHandler)
- {
- ((TestSessionHandler)m1).assertCandidatesForExpiry(0);
- }
-
- //check a session removed listener did not get called
- assertEquals(1, listener.count.getCurrent());
- }
- finally
- {
- client.stop();
- }
- }
- finally
- {
- server1.stop();
- }
- }
-
-
- @Test
- public void testLocalSessionsScavenging() throws Exception
- {
- String contextPath = "";
- String servletMapping = "/server";
- int inactivePeriod = 4;
- int scavengePeriod = 1;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT); //don't evict sessions
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
+ ((AbstractSessionDataStoreFactory)storeFactory).setSavePeriodSec(0); //always save when the session exits
+
+ TestServer server1 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
+ TestServlet servlet1 = new TestServlet();
+ ServletHolder holder1 = new ServletHolder(servlet1);
+ ServletContextHandler context = server1.addContext(contextPath);
+ TestSessionListener listener1 = new TestSessionListener();
+ context.getSessionHandler().addEventListener(listener1);
+ context.addServlet(holder1, servletMapping);
+ SessionHandler m1 = context.getSessionHandler();
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
- ServletContextHandler context1 = server1.addContext(contextPath);
- context1.addServlet(TestServlet.class, servletMapping);
try
{
server1.start();
- int port1 = server1.getPort();
- TestServer server2 = new TestServer(0, inactivePeriod, scavengePeriod * 2, cacheFactory, storeFactory);
+ int port1=server1.getPort();
+
+ TestServer server2 = new TestServer(0, maxInactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ServletContextHandler context2 = server2.addContext(contextPath);
context2.addServlet(TestServlet.class, servletMapping);
+ SessionHandler m2 = context2.getSessionHandler();
+
try
{
server2.start();
- int port2 = server2.getPort();
+ int port2=server2.getPort();
HttpClient client = new HttpClient();
client.start();
try
{
- String[] urls = new String[2];
- urls[0] = "http://localhost:" + port1 + contextPath + servletMapping;
- urls[1] = "http://localhost:" + port2 + contextPath + servletMapping;
-
- // Create the session on node1
- ContentResponse response1 = client.GET(urls[0] + "?action=init");
- assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
+ // Perform one request to server1 to create a session
+ ContentResponse response1 = client.GET("http://localhost:" + port1 + contextPath + servletMapping.substring(1) + "?action=init");
+ assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
+ assertEquals("test", response1.getContentAsString());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
- assertTrue(sessionCookie != null);
+ assertTrue( sessionCookie != null );
+ assertEquals(1, ((DefaultSessionCache)m1.getSessionCache()).getSessionsCurrent());
+ assertEquals(1, ((DefaultSessionCache)m1.getSessionCache()).getSessionsMax());
+ assertEquals(1, ((DefaultSessionCache)m1.getSessionCache()).getSessionsTotal());
// Mangle the cookie, replacing Path with $Path, etc.
- sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
- SessionHandler m1 = context1.getSessionHandler();
- assertEquals(1, m1.getSessionsCreated());
-
- // Be sure the session is also present in node2
- org.eclipse.jetty.client.api.Request request = client.newRequest(urls[1] + "?action=test");
- request.header("Cookie", sessionCookie);
- ContentResponse response2 = request.send();
- assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
- SessionHandler m2 = context2.getSessionHandler();
-
- // Wait for the scavenger to run on node1
- pause(inactivePeriod+(2*scavengePeriod));
-
- assertEquals(1, m1.getSessionsCreated());
-
- // Check that node1 does not have any local session cached
- request = client.newRequest(urls[0] + "?action=check");
- response1 = request.send();
- assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
+ sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
+ String id = TestServer.extractSessionId(sessionCookie);
+ Session s1 = ((DefaultSessionCache)m1.getSessionCache()).get(id);
+ assertNotNull(s1);
+ long expiry = s1.getSessionData().getExpiry();
- assertEquals(1, m1.getSessionsCreated());
-
- // 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(inactivePeriod+(2*scavengePeriod));
- // Check that node2 does not have any local session cached
- request = client.newRequest(urls[1] + "?action=check");
- response2 = request.send();
- assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
+ //Now do requests for the session to node2. This will update the expiry time on the session.
+ //Send requests for the next maxInactiveInterval, pausing a little between each request.
+ int requestInterval = 500; //ms pause between requests
+ long start = System.currentTimeMillis();
+ long end = expiry;
+ long time = start;
+ while (time < end)
+ {
+ Request request = client.newRequest("http://localhost:" + port2 + contextPath + servletMapping.substring(1));
+ request.header("Cookie", sessionCookie); //use existing session
+ ContentResponse response2 = request.send();
+ assertEquals(HttpServletResponse.SC_OK , response2.getStatus());
+ assertEquals("test", response2.getContentAsString());
+ Thread.sleep(requestInterval);
+ assertSessionCounts(1,1,1, m2);
+ time = System.currentTimeMillis();
+ }
+
+ //session on node1 should be eligible for scavenge
+ //ensure scavenger has run on node1
+ Thread.sleep(TimeUnit.SECONDS.toMillis(scavengePeriod)); // wait until just after the original expiry time has passed
+
+
+ //check that the session wasn't in fact scavenged because it was in use on node1
+ assertFalse(listener1._destroys.contains(TestServer.extractSessionId(sessionCookie)));
+ assertAfterScavenge(m1);
}
finally
{
@@ -239,9 +165,51 @@ public abstract class AbstractClusteredSessionScavengingTest extends AbstractTes
server1.stop();
}
}
+
+
+
+ public void assertAfterSessionCreated (SessionHandler m)
+ {
+ assertSessionCounts(1, 1, 1, m);
+ }
+
+ public void assertAfterScavenge (SessionHandler manager)
+ {
+ assertSessionCounts(1,1,1, manager);
+ }
+
+ public void assertSessionCounts (int current, int max, int total, SessionHandler manager)
+ {
+ assertEquals(current, ((DefaultSessionCache)manager.getSessionCache()).getSessionsCurrent());
+ assertEquals(max, ((DefaultSessionCache)manager.getSessionCache()).getSessionsMax());
+ assertEquals(total, ((DefaultSessionCache)manager.getSessionCache()).getSessionsTotal());
+ }
+
+
+ public static class TestSessionListener implements HttpSessionListener
+ {
+ public Set _creates = new HashSet<>();
+ public Set _destroys = new HashSet<>();
+
+ @Override
+ public void sessionDestroyed(HttpSessionEvent se)
+ {
+ _destroys.add(se.getSession().getId());
+ }
+
+ @Override
+ public void sessionCreated(HttpSessionEvent se)
+ {
+ _creates.add(se.getSession().getId());
+ }
+ }
+
+
public static class TestServlet extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
@@ -250,18 +218,34 @@ public abstract class AbstractClusteredSessionScavengingTest extends AbstractTes
{
HttpSession session = request.getSession(true);
session.setAttribute("test", "test");
+ sendResult(session, httpServletResponse.getWriter());
+
}
- else if ("test".equals(action))
+ else
{
HttpSession session = request.getSession(false);
- assertNotNull(session);
- session.setAttribute("test", "test");
+
+ // if we node hopped we should get the session and test should already be present
+ sendResult(session, httpServletResponse.getWriter());
+
+ if (session!=null)
+ {
+ session.setAttribute("test", "test");
+ }
}
- else if ("check".equals(action))
+ }
+
+ private void sendResult(HttpSession session, PrintWriter writer)
+ {
+ if (session != null)
{
- HttpSession session = request.getSession(false);
- assertTrue(session == null);
+ writer.print(session.getAttribute("test"));
+ }
+ else
+ {
+ writer.print("null");
}
}
}
+
}
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
deleted file mode 100644
index 080b1472538..00000000000
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractProxySerializationTest.java
+++ /dev/null
@@ -1,134 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-
-package org.eclipse.jetty.server.session;
-
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.net.URL;
-import java.net.URLClassLoader;
-
-import javax.servlet.http.HttpServletResponse;
-
-import org.eclipse.jetty.client.HttpClient;
-import org.eclipse.jetty.client.api.ContentResponse;
-import org.eclipse.jetty.client.api.Request;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
-import org.eclipse.jetty.util.IO;
-import org.junit.Test;
-
-/**
- * AbstractProxySerializationTest
- *
- * For SessionDataStores that passivate with serialization.
- */
-public abstract class AbstractProxySerializationTest extends AbstractTestBase
-{
-
- public abstract void customizeContext (ServletContextHandler c);
-
-
- /**
- * @param msec milliseconds to sleep
- */
- public void pause(int msec)
- {
- try
- {
- Thread.sleep(msec);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
-
- @Test
- public void testProxySerialization() throws Exception
- {
- String contextPath = "/";
- String servletMapping = "/server";
- int scavengePeriod = 10;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
- ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
- TestServer server = new TestServer(0, 20, scavengePeriod,
- cacheFactory, storeFactory);
- ServletContextHandler context = server.addContext(contextPath);
-
- InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar");
-
- File testDir = MavenTestingUtils.getTargetTestingDir("proxy-serialization");
- testDir.mkdirs();
-
- File extractedJar = new File (testDir, "proxy-serialization.jar");
- extractedJar.createNewFile();
- IO.copy(is, new FileOutputStream(extractedJar));
-
-
- URLClassLoader loader = new URLClassLoader(new URL[] {extractedJar.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
- context.setClassLoader(loader);
- context.addServlet("TestServlet", servletMapping);
- customizeContext(context);
-
- try
- {
- server.start();
- int port=server.getPort();
- HttpClient client = new HttpClient();
- client.start();
- try
- {
- ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=create");
- assertEquals(HttpServletResponse.SC_OK,response.getStatus());
- String sessionCookie = response.getHeaders().get("Set-Cookie");
- assertTrue(sessionCookie != null);
-
- //stop the context to be sure the sesssion will be passivated
- context.stop();
-
- //restart the context
- context.start();
-
- // Make another request using the session id from before
- Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=test");
- response = request.send();
- assertEquals(HttpServletResponse.SC_OK,response.getStatus());
- }
- finally
- {
- client.stop();
- }
- }
- finally
- {
- server.stop();
- }
-
- }
-}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionDataStoreTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionDataStoreTest.java
new file mode 100644
index 00000000000..4f02afe2a14
--- /dev/null
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionDataStoreTest.java
@@ -0,0 +1,761 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+
+package org.eclipse.jetty.server.session;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.junit.Test;
+
+/**
+ * AbstractSessionDataStoreTest
+ *
+ *
+ */
+public abstract class AbstractSessionDataStoreTest
+{
+ public static final int GRACE_PERIOD_SEC = (int)TimeUnit.HOURS.toSeconds(2);
+
+ /**
+ * A timestamp representing a time that was close to the epoch, and thus
+ * happened a long time ago. Used as expiry timestamp for to
+ * signify a session is expired.
+ */
+ public static final long ANCIENT_TIMESTAMP = 100L;
+ public static final long RECENT_TIMESTAMP = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(3*GRACE_PERIOD_SEC);
+
+
+
+
+ public abstract SessionDataStoreFactory createSessionDataStoreFactory();
+
+ public abstract void persistSession(SessionData data) throws Exception;
+
+ public abstract void persistUnreadableSession(SessionData data) throws Exception;
+
+ public abstract boolean checkSessionExists (SessionData data) throws Exception;
+
+ public abstract boolean checkSessionPersisted (SessionData data) throws Exception;
+
+
+
+ /**
+ * Test that the store can persist a session.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testStoreSession() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ store.start();
+
+ //create a session
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", 100, now, now-1, -1);//never expires
+ data.setAttribute("a", "b");
+ data.setLastNode(sessionContext.getWorkerName());
+
+ store.store("1234", data);
+
+ //check that the store contains all of the session data
+ assertTrue(checkSessionPersisted(data));
+ }
+
+
+ /**
+ * Test that the store can update a pre-existing session.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testUpdateSession() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ store.start();
+
+ //create a session
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", 100, 200, 199, -1);//never expires
+ data.setAttribute("a", "b");
+ data.setLastNode(sessionContext.getWorkerName());
+ data.setLastSaved(400); //make it look like it was previously saved by the store
+
+
+ //put it into the store
+ persistSession(data);
+
+ //now test we can update the session
+ data.setLastAccessed(now-1);
+ data.setAccessed(now);
+ data.setMaxInactiveMs(TimeUnit.MINUTES.toMillis(2));
+ data.setAttribute("a", "c");
+ store.store("1234", data);
+
+ assertTrue(checkSessionPersisted(data));
+ }
+
+
+ /**
+ * Test that the store can persist a session that contains
+ * serializable objects in the attributes.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testStoreObjectAttributes() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ store.start();
+
+ //create a session
+ SessionData data = store.newSessionData("1234", 100, 200, 199, -1);//never expires
+ TestFoo testFoo = new TestFoo();
+ testFoo.setInt(33);
+ FooInvocationHandler handler = new FooInvocationHandler(testFoo);
+ Foo foo = (Foo)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {Foo.class}, handler);
+ data.setAttribute("foo", foo);
+ data.setLastNode(sessionContext.getWorkerName());
+
+ //test that it can be persisted
+ store.store("1234", data);
+ checkSessionPersisted(data);
+ }
+
+ /**
+ * Test that we can load a persisted session.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testLoadSessionExists() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ //persist a session that is not expired
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", 100, now, now-1, -1);//never expires
+ data.setLastNode(sessionContext.getWorkerName());
+ persistSession(data);
+
+ store.start();
+
+ //test that we can retrieve it
+ SessionData loaded = store.load("1234");
+ assertNotNull(loaded);
+ assertEquals("1234", loaded.getId());
+ assertEquals(100, loaded.getCreated());
+ assertEquals(now, loaded.getAccessed());
+ assertEquals(now-1, loaded.getLastAccessed());
+ assertEquals(0, loaded.getExpiry());
+ }
+
+
+ /**
+ * Test that an expired session can be loaded.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testLoadSessionExpired() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+
+ //persist a session that is expired
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("678", 100, now-20, now-30, 10);//10 sec max idle
+ data.setLastNode(sessionContext.getWorkerName());
+ data.setExpiry(RECENT_TIMESTAMP); //make it expired recently
+ persistSession(data);
+
+ store.start();
+
+ //test we can retrieve it
+ SessionData loaded = store.load("678");
+ assertNotNull(loaded);
+ assertEquals("678", loaded.getId());
+ assertEquals(100, loaded.getCreated());
+ assertEquals(now-20, loaded.getAccessed());
+ assertEquals(now-30, loaded.getLastAccessed());
+ assertEquals(RECENT_TIMESTAMP, loaded.getExpiry());
+ }
+
+
+ /**
+ * Test that a non-existent session cannot be loaded.
+ * @throws Exception
+ */
+ @Test
+ public void testLoadSessionDoesNotExist() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+ store.start();
+
+ //test we can't retrieve a non-existent session
+ SessionData loaded = store.load("111");
+ assertNull(loaded);
+ }
+
+
+ /**
+ * Test that a session that cannot be loaded throws exception.
+ * @throws Exception
+ */
+ @Test
+ public void testLoadSessionFails() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+
+ //persist a session that is damaged and cannot be read
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("222", 100, now, now-1, -1);
+ data.setLastNode(sessionContext.getWorkerName());
+ persistUnreadableSession(data);
+
+ store.start();
+
+ //test that we can retrieve it
+ try
+ {
+ store.load("222");
+ fail("Session should be unreadable");
+ }
+ catch (UnreadableSessionDataException e)
+ {
+ //expected exception
+ }
+ }
+
+
+
+ /**
+ * Test that we can delete a persisted session.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testDeleteSessionExists() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ //persist a session that is not expired
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", 100, now, now-1, -1);
+ data.setLastNode(sessionContext.getWorkerName());
+ persistSession(data);
+
+ store.start();
+
+ //delete the session via the store
+ store.delete("1234");
+
+ //check the session is no longer exists
+ assertFalse(checkSessionExists(data));
+ }
+
+
+ /**
+ * Test deletion of non-existent session.
+ * @throws Exception
+ */
+ @Test
+ public void testDeleteSessionDoesNotExist() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ store.initialize(new SessionContext("foo", context.getServletContext()));
+ store.start();
+
+ //delete the non-existent session via the store
+ store.delete("3333");
+ }
+
+
+ /**
+ * Test SessionDataStore.getExpired. Tests the situation
+ * where the session candidates are also expired in the
+ * store.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetExpiredPersistedAndExpired() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ //persist a session that is expired
+ SessionData data = store.newSessionData("1234", 100, 101, 101, 10);
+ data.setLastNode(sessionContext.getWorkerName());
+ data.setExpiry(RECENT_TIMESTAMP); //make it expired recently so FileSessionDataStore doesn't eliminate it on startup
+ persistSession(data);
+
+ //persist another session that is expired
+ SessionData data2 = store.newSessionData("5678", 100, 100, 101, 30);
+ data2.setLastNode(sessionContext.getWorkerName());
+ data2.setExpiry(RECENT_TIMESTAMP); //make it expired recently so FileSessionDataStore doesn't eliminate it on startup
+ persistSession(data2);
+
+ store.start();
+
+ Set candidates = new HashSet<>(Arrays.asList(new String[] {"1234", "5678"}));
+ Set expiredIds = store.getExpired(candidates);
+ assertEquals(2, expiredIds.size());
+ assertTrue(expiredIds.contains("1234"));
+ assertTrue(expiredIds.contains("5678"));
+ }
+
+
+ /**
+ * Test SessionDataStore.getExpired: tests the situation where
+ * the session candidates are not expired in the store.
+ * @throws Exception
+ */
+ @Test
+ public void testGetExpiredPersistedNotExpired() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ long now = System.currentTimeMillis();
+ //persist a session that is not expired
+ SessionData data = store.newSessionData("1234", 100, now, now-1, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode(sessionContext.getWorkerName());
+ persistSession(data);
+
+ //persist another session that is not expired
+ SessionData data2 = store.newSessionData("5678", 100, now, now-1, TimeUnit.MINUTES.toMillis(60));
+ data2.setLastNode(sessionContext.getWorkerName());
+ persistSession(data2);
+
+ store.start();
+
+ Set candidates = new HashSet<>(Arrays.asList(new String[] {"1234", "5678"}));
+ Set expiredIds = store.getExpired(candidates);
+ assertEquals(0, expiredIds.size());
+ }
+
+ /**
+ * Test SessionDataStore.getExpired: tests the situation where
+ * the session candidates don't exist in the store.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetExpiredNotPersisted() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+ store.start();
+
+ Set candidates = new HashSet<>(Arrays.asList(new String[] {"1234", "5678"}));
+ Set expiredIds = store.getExpired(candidates);
+ assertEquals(2, expiredIds.size());
+ assertTrue(expiredIds.contains("1234"));
+ assertTrue(expiredIds.contains("5678"));
+ }
+
+ /**
+ * Test SessionDataStore.getExpired: tests the situation where
+ * there are more persisted expired sessions in the store than
+ * present in the candidate list.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetExpiredPersistedAndExpiredOnly() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ //persist a session that is expired
+ SessionData data = store.newSessionData("1234", 100, 101, 100, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode(sessionContext.getWorkerName());
+ data.setExpiry(RECENT_TIMESTAMP); //must be recently expired, or FileSessionDataStore will eliminate it on startup
+ persistSession(data);
+
+ //persist another session that is expired
+ SessionData data2 = store.newSessionData("5678", 100, 101, 100, TimeUnit.MINUTES.toMillis(60));
+ data2.setLastNode(sessionContext.getWorkerName());
+ data2.setExpiry(RECENT_TIMESTAMP); //must be recently expired, or FileSessionDataStore will eliminate it on startup
+ persistSession(data2);
+
+ store.start();
+
+ Set candidates = new HashSet<>();
+ Set expiredIds = store.getExpired(candidates);
+ assertEquals(2, expiredIds.size());
+ assertTrue(expiredIds.contains("1234"));
+ assertTrue(expiredIds.contains("5678"));
+ }
+
+
+ /**
+ * Test SessionDataStore.getExpired: tests the situation where
+ * there are sessions that are not in use on the node, but have
+ * expired and are last used by another node.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetExpiredDifferentNode() throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ //persist a session that is expired for a different node
+ SessionData data = store.newSessionData("1234", 100, 101, 100, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode("other");
+ data.setExpiry(RECENT_TIMESTAMP); //must be recently expired, or FileSessionDataStore will eliminate it on startup
+ persistSession(data);
+
+ store.start();
+
+ Set candidates = new HashSet<>();
+ Set expiredIds = store.getExpired(candidates);
+ assertEquals(1, expiredIds.size());
+ assertTrue(expiredIds.contains("1234"));
+ }
+
+
+
+ /**
+ * Test the exist() method with a session that does exist and is not expired
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testExistsNotExpired () throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ long now = System.currentTimeMillis();
+ //persist a session that is not expired
+ SessionData data = store.newSessionData("1234", 100, now, now-1, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode(sessionContext.getWorkerName());
+ persistSession(data);
+
+ store.start();
+
+ assertTrue(store.exists("1234"));
+ }
+
+ /**
+ * Test the exist() method with a session that does exist and is expired
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testExistsIsExpired () throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ //persist a session that is expired
+ SessionData data = store.newSessionData("1234", 100, 101, 100, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode(sessionContext.getWorkerName());
+ data.setExpiry(RECENT_TIMESTAMP);
+ persistSession(data);
+
+ store.start();
+
+ assertFalse(store.exists("1234"));
+ }
+
+ /**
+ * Test the exist() method with a session that does not exist
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testExistsNotExists () throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ store.start();
+
+ assertFalse(store.exists("8888"));
+ }
+
+
+ @Test
+ public void testExistsDifferentContext () throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+
+ //persist a session for a different context
+ SessionData data = store.newSessionData("1234", 100, 101, 100, TimeUnit.MINUTES.toMillis(60));
+ data.setContextPath("_other");
+ data.setLastNode(sessionContext.getWorkerName());
+ persistSession(data);
+
+ store.start();
+
+ //check that session does not exist for this context
+ assertFalse(store.exists("1234"));
+ }
+
+
+ /**
+ * Test setting a save period to avoid writes when the attributes haven't changed.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSavePeriodOnUpdate ()
+ throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ ((AbstractSessionDataStoreFactory)factory).setSavePeriodSec(20); //only save every 20sec
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+ store.start();
+
+ long now = System.currentTimeMillis();
+
+ //persist a session that is not expired, and has been saved before
+ SessionData data = store.newSessionData("1234", 100, now-10, now-20, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode(sessionContext.getWorkerName());
+ data.setLastSaved(now-100);
+ persistSession(data);
+
+ //update just the access and last access time
+ data.setLastAccessed(now-5);
+ data.setAccessed(now-1);
+
+ //test that a save does not change the stored data
+ store.store("1234", data);
+
+ //reset the times for a check
+ data.setLastAccessed(now-20);
+ data.setAccessed(now-10);
+ checkSessionPersisted(data);
+ }
+
+
+ /**
+ * Check that a session that has never previously been
+ * saved will be saved despite the savePeriod setting.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSavePeriodOnCreate ()
+ throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ ((AbstractSessionDataStoreFactory)factory).setSavePeriodSec(20); //only save every 20sec
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+ store.start();
+
+ long now = System.currentTimeMillis();
+ //create a session that is not expired, and has never been saved before
+ SessionData data = store.newSessionData("1234", 100, now-10, now-20, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode(sessionContext.getWorkerName());
+
+ store.store("1234", data);
+
+ checkSessionPersisted(data);
+ }
+
+
+ /**
+ * Check that a session whose attributes have changed will always
+ * be saved despite the savePeriod
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSavePeriodDirtySession ()
+ throws Exception
+ {
+ //create the SessionDataStore
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ SessionDataStoreFactory factory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC);
+ ((AbstractSessionDataStoreFactory)factory).setSavePeriodSec(20); //only save every 20sec
+ SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler());
+ SessionContext sessionContext = new SessionContext("foo", context.getServletContext());
+ store.initialize(sessionContext);
+ store.start();
+
+ //persist a session that is not expired
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", 100, now-10, now-20, TimeUnit.MINUTES.toMillis(60));
+ data.setLastNode(sessionContext.getWorkerName());
+ data.setLastSaved(now-100);
+ data.setAttribute("wibble", "wobble");
+ persistSession(data);
+
+ //now change the attributes
+ data.setAttribute("wibble", "bobble");
+ data.setLastAccessed(now-5);
+ data.setAccessed(now-1);
+
+ store.store("1234", data);
+
+ checkSessionPersisted(data);
+ }
+}
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
deleted file mode 100644
index bf3308f631a..00000000000
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionExpiryTest.java
+++ /dev/null
@@ -1,447 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-package org.eclipse.jetty.server.session;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-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.HttpClient;
-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.eclipse.jetty.util.StringUtil;
-import org.eclipse.jetty.util.log.Log;
-import org.junit.Ignore;
-import org.junit.Test;
-
-/**
- * AbstractSessionExpiryTest
- *
- * Ensure session is not removed when server stops, and that a
- * non-expired session can be loaded in after restart.
- */
-public abstract class AbstractSessionExpiryTest extends AbstractTestBase
-{
-
- public void pause(int period)
- {
- try
- {
- Thread.sleep(period * 1000L);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
-
- public class TestHttpSessionListener implements HttpSessionListener
- {
- public List createdSessions = new ArrayList();
- public List destroyedSessions = new ArrayList();
- public boolean accessAttribute = false;
- public Exception ex = null;
-
- public TestHttpSessionListener(boolean access)
- {
- accessAttribute = access;
- }
-
- public TestHttpSessionListener()
- {
- accessAttribute = false;
- }
-
- @Override
- public void sessionDestroyed(HttpSessionEvent se)
- {
- destroyedSessions.add(se.getSession().getId());
- if (accessAttribute)
- {
- try
- {
-
- se.getSession().getAttribute("anything");
- }
- catch (Exception e)
- {
- ex = e;
- }
- }
- }
-
- @Override
- public void sessionCreated(HttpSessionEvent se)
- {
- createdSessions.add(se.getSession().getId());
- }
- };
-
- @Test
- @Ignore // https://github.com/eclipse/jetty.project/issues/2214
- public void testSessionExpiresWithListener() throws Exception
- {
- String contextPath = "/";
- String servletMapping = "/server";
- int inactivePeriod = 3;
- int scavengePeriod = 1;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
- ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
- TestServlet servlet = new TestServlet();
- ServletHolder holder = new ServletHolder(servlet);
- ServletContextHandler context = server1.addContext(contextPath);
- context.addServlet(holder, servletMapping);
- TestHttpSessionListener listener = new TestHttpSessionListener(true);
-
- context.getSessionHandler().addEventListener(listener);
-
- server1.start();
- int port1 = server1.getPort();
-
- try
- {
- HttpClient client = new HttpClient();
- client.start();
- String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
-
- //make a request to set up a session on the server
- ContentResponse response1 = client.GET(url + "?action=init");
- assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
- String sessionCookie = response1.getHeaders().get("Set-Cookie");
- assertTrue(sessionCookie != null);
-
- String sessionId = TestServer.extractSessionId(sessionCookie);
-
- verifySessionCreated(listener,sessionId);
-
- //and wait until the session should have expired
- pause(inactivePeriod+(scavengePeriod*2));
-
- verifySessionDestroyed (listener, sessionId);
- assertNull(listener.ex);
- }
- finally
- {
- server1.stop();
- }
- }
-
- /**
- * Check session is preserved over stop/start
- * @throws Exception
- */
- @Test
- public void testSessionNotExpired() throws Exception
- {
- String contextPath = "/";
- String servletMapping = "/server";
- int inactivePeriod = 20;
- int scavengePeriod = 10;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
- ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
- TestServlet servlet = new TestServlet();
- ServletHolder holder = new ServletHolder(servlet);
- server1.addContext(contextPath).addServlet(holder, servletMapping);
-
- HttpClient client = new HttpClient();
-
- try
- {
- Log.getLogger(org.eclipse.jetty.util.thread.QueuedThreadPool.class).setDebugEnabled(true);
- server1.start();
- int port1 = server1.getPort();
-
- client.start();
- String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
-
- //make a request to set up a session on the server
- ContentResponse response = client.GET(url + "?action=init");
- assertEquals(HttpServletResponse.SC_OK,response.getStatus());
- String sessionCookie = response.getHeaders().get("Set-Cookie");
- assertTrue(sessionCookie != null);
-
- //now stop the server
- server1.stop();
-
-
- //start the server again, before the session times out
- server1.start();
- port1 = server1.getPort();
- url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
-
- //make another request, the session should not have expired
- Request request = client.newRequest(url + "?action=notexpired");
- ContentResponse response2 = request.send();
- assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
-
- }
- finally
- {
- Log.getLogger(org.eclipse.jetty.util.thread.QueuedThreadPool.class).setDebugEnabled(false);
- client.stop();
- server1.stop();
- }
- }
-
-
- /**
- * 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
- {
-
-
- String contextPath = "/";
- String servletMapping = "/server";
- int inactivePeriod = 4;
- int scavengePeriod = 1;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
- ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
- TestServlet servlet = new TestServlet();
- 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.substring(1);
-
- //make a request to set up a session on the server
- ContentResponse response1 = client.GET(url + "?action=init");
- assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
- String sessionCookie = response1.getHeaders().get("Set-Cookie");
- assertTrue(sessionCookie != null);
-
- String sessionId = TestServer.extractSessionId(sessionCookie);
-
- verifySessionCreated(listener,sessionId);
-
- //now stop the server
- server1.stop();
-
- //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.substring(1);
-
- //make another request, the session should have expired
- Request request = client.newRequest(url + "?action=test");
- ContentResponse response2 = request.send();
-
- assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
- String cookie2 = response2.getHeaders().get("Set-Cookie");
- assertTrue (!cookie2.equals(sessionCookie));
- verifySessionDestroyed (listener, sessionId);
- }
- finally
- {
- server1.stop();
- }
- }
-
-
- @Test
- public void testRequestForSessionWithChangedTimeout () throws Exception
- {
- String contextPath = "/";
- String servletMapping = "/server";
- int inactivePeriod = 5;
- int scavengePeriod = 1;
-
- DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
- cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
- SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
- ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
- TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
- ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
- 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.substring(1);
-
- //make a request to set up a session on the server with the session manager's inactive timeout
- ContentResponse response = client.GET(url + "?action=init");
- assertEquals(HttpServletResponse.SC_OK,response.getStatus());
- String sessionCookie = response.getHeaders().get("Set-Cookie");
- assertTrue(sessionCookie != null);
-
- //make another request to change the session timeout to a larger value
- int newInactivePeriod = 100;
- Request request = client.newRequest(url + "?action=change&val="+newInactivePeriod);
- response = request.send();
- assertEquals(HttpServletResponse.SC_OK,response.getStatus());
-
- //stop and restart the session manager to ensure it needs to reload the session
- context.stop();
- context.start();
-
- //wait until the session manager timeout has passed and re-request the session
- //which should still be valid
- pause(inactivePeriod);
-
- request = client.newRequest(url + "?action=check");
- response = request.send();
- assertEquals(HttpServletResponse.SC_OK,response.getStatus());
- String sessionCookie2 = response.getHeaders().get("Set-Cookie");
- assertNull(sessionCookie2);
-
- }
- finally
- {
- server1.stop();
- }
- }
-
-
-
- public void verifySessionCreated (TestHttpSessionListener listener, String sessionId)
- {
- assertTrue(listener.createdSessions.contains(sessionId));
- }
- public void verifySessionDestroyed (TestHttpSessionListener listener, String sessionId)
- {
- assertTrue (listener.destroyedSessions.contains(sessionId));
- }
-
-
-
- public static class TestServlet extends HttpServlet
- {
- public String originalId = null;
-
- @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);
- session.setAttribute("test", "test");
- originalId = session.getId();
- }
- else if ("test".equals(action))
- {
- HttpSession session = request.getSession(true);
- assertTrue(session != null);
- assertTrue(!originalId.equals(session.getId()));
- }
- else if ("notexpired".equals(action))
- {
- HttpSession session = request.getSession(false);
- assertTrue(session != null);
- assertTrue(originalId.equals(session.getId()));
- }
-
- }
- }
-
-
- public static class ChangeTimeoutServlet 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);
- session.setAttribute("test", "test");
- }
- 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);
- }
- else if ("check".equals(action))
- {
- HttpSession session = request.getSession(false);
- assertNotNull(session);
- }
- }
- }
-
-}
diff --git a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/HashTestHelper.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/Foo.java
similarity index 82%
rename from tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/HashTestHelper.java
rename to tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/Foo.java
index bb072ff7839..72819d75362 100644
--- a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/HashTestHelper.java
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/Foo.java
@@ -19,15 +19,12 @@
package org.eclipse.jetty.server.session;
/**
- * HashTestHelper
- *
+ * Foo
+ *
+ *
*/
-public class HashTestHelper
+public interface Foo
{
-
- public static SessionDataStoreFactory newSessionDataStoreFactory()
- {
- return new NullSessionDataStoreFactory();
- }
-
+ public int getInt();
+ public void setInt (int i);
}
diff --git a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/ModifyMaxInactiveIntervalTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/FooInvocationHandler.java
similarity index 63%
rename from tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/ModifyMaxInactiveIntervalTest.java
rename to tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/FooInvocationHandler.java
index 33df753ef75..ef5f630f76f 100644
--- a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/ModifyMaxInactiveIntervalTest.java
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/FooInvocationHandler.java
@@ -16,32 +16,34 @@
// ========================================================================
//
-
package org.eclipse.jetty.server.session;
-import org.junit.After;
+import java.io.Serializable;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
/**
- * ModifyMaxInactiveIntervalTest
+ * ProxiableSessionAttributeObjectInvocationHandler
*
*
*/
-public class ModifyMaxInactiveIntervalTest extends AbstractModifyMaxInactiveIntervalTest
+public class FooInvocationHandler implements InvocationHandler, Serializable
{
- /**
- * @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory()
- */
- @Override
- public SessionDataStoreFactory createSessionDataStoreFactory()
+ private static final long serialVersionUID = -4009478822490178554L;
+
+ private Foo foo;
+
+ public FooInvocationHandler (Foo f)
{
- return JdbcTestHelper.newSessionDataStoreFactory();
+ foo = f;
}
- @After
- public void tearDown() throws Exception
+
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
- JdbcTestHelper.shutdown(null);
+ return method.invoke(foo, args);
}
-
}
diff --git a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionInvalidateCreateScavengeTest.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestFoo.java
similarity index 65%
rename from tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionInvalidateCreateScavengeTest.java
rename to tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestFoo.java
index 3ec2eb172b8..8b004fe99cb 100644
--- a/tests/test-sessions/test-hash-sessions/src/test/java/org/eclipse/jetty/server/session/SessionInvalidateCreateScavengeTest.java
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestFoo.java
@@ -18,23 +18,40 @@
package org.eclipse.jetty.server.session;
-import org.junit.Test;
+import java.io.Serializable;
-public class SessionInvalidateCreateScavengeTest extends AbstractSessionInvalidateCreateScavengeTest
+/**
+ * TestFoo
+ *
+ *
+ */
+public class TestFoo implements Foo, Serializable
{
- /**
- * @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory()
- */
- @Override
- public SessionDataStoreFactory createSessionDataStoreFactory()
- {
- return HashTestHelper.newSessionDataStoreFactory();
- }
+ private static final long serialVersionUID = 953717519120144555L;
- @Test
+ private int i = -99;
+
@Override
- public void testSessionScavenge() throws Exception
+ public int getInt()
{
- super.testSessionScavenge();
+ return this.i;
}
+
+ @Override
+ public void setInt(int i)
+ {
+ this.i = i;
+ }
+
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj == null)
+ return false;
+
+ return (((Foo)obj).getInt() == getInt());
+ }
+
+
}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestFooServlet.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestFooServlet.java
new file mode 100644
index 00000000000..548a5498106
--- /dev/null
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestFooServlet.java
@@ -0,0 +1,59 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.server.session;
+
+import java.io.IOException;
+import java.lang.reflect.Proxy;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+
+
+public class TestFooServlet extends HttpServlet
+{
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+ String action = request.getParameter("action");
+
+ if ("create".equals(action))
+ {
+ HttpSession session = request.getSession(true);
+ TestFoo testFoo = new TestFoo();
+ testFoo.setInt(33);
+ FooInvocationHandler handler = new FooInvocationHandler(testFoo);
+ Foo foo = (Foo)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] {Foo.class}, handler);
+ session.setAttribute("foo", foo);
+ }
+ else if ("test".equals(action))
+ {
+ HttpSession session = request.getSession(false);
+ if (session == null)
+ response.sendError(500, "Session not activated");
+ Foo foo = (Foo)session.getAttribute("foo");
+ if (foo == null || foo.getInt() != 33)
+ response.sendError(500, "Foo not deserialized");
+ }
+
+ }
+}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestHttpSessionListener.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestHttpSessionListener.java
new file mode 100644
index 00000000000..6e366458183
--- /dev/null
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestHttpSessionListener.java
@@ -0,0 +1,71 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+
+package org.eclipse.jetty.server.session;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+
+/**
+ * TestSessionListener
+ *
+ *
+ */
+public class TestHttpSessionListener implements HttpSessionListener
+{
+ public List createdSessions = new ArrayList<>();
+ public List destroyedSessions = new ArrayList<>();
+ public boolean accessAttribute = false;
+ public Exception ex = null;
+
+ public TestHttpSessionListener(boolean access)
+ {
+ accessAttribute = access;
+ }
+
+ public TestHttpSessionListener()
+ {
+ accessAttribute = false;
+ }
+
+ public void sessionDestroyed(HttpSessionEvent se)
+ {
+ destroyedSessions.add(se.getSession().getId());
+ if (accessAttribute)
+ {
+ try
+ {
+ se.getSession().getAttribute("anything");
+ }
+ catch (Exception e)
+ {
+ ex = e;
+ }
+ }
+ }
+
+ public void sessionCreated(HttpSessionEvent se)
+ {
+ createdSessions.add(se.getSession().getId());
+ }
+
+}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java
index 552f4a63cf4..51ac9b98be3 100644
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java
+++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java
@@ -27,35 +27,39 @@ import java.util.Set;
/**
* TestSessionDataStore
*
- * Make a fake session data store that creates a new SessionData object
+ * Make a fake session data store (non clustered!) that creates a new SessionData object
* every time load(id) is called.
*/
public class TestSessionDataStore extends AbstractSessionDataStore
{
public Map _map = new HashMap<>();
+ public boolean _passivating;
+
+ public TestSessionDataStore ()
+ {
+ _passivating = false;
+ }
+
+ public TestSessionDataStore (boolean passivating)
+ {
+ _passivating = passivating;
+ }
- /**
- * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
- */
@Override
public boolean isPassivating()
{
- return false;
+ return _passivating;
}
- /**
- * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String)
- */
+
@Override
public boolean exists(String id) throws Exception
{
return _map.containsKey(id);
}
- /**
- * @see org.eclipse.jetty.server.session.SessionDataMap#load(java.lang.String)
- */
+
@Override
public SessionData load(String id) throws Exception
{
@@ -67,27 +71,21 @@ public class TestSessionDataStore extends AbstractSessionDataStore
return nsd;
}
- /**
- * @see org.eclipse.jetty.server.session.SessionDataMap#delete(java.lang.String)
- */
+
@Override
public boolean delete(String id) throws Exception
{
return (_map.remove(id) != null);
}
- /**
- * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long)
- */
+
@Override
public void doStore(String id, SessionData data, long lastSaveTime) throws Exception
{
_map.put(id, data);
}
- /**
- * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doGetExpired(java.util.Set)
- */
+
@Override
public Set doGetExpired(Set candidates)
{
@@ -102,5 +100,4 @@ public class TestSessionDataStore extends AbstractSessionDataStore
}
return set;
}
-
}
diff --git a/tests/test-sessions/test-sessions-common/src/main/resources/proxy-serialization.jar b/tests/test-sessions/test-sessions-common/src/main/resources/proxy-serialization.jar
deleted file mode 100644
index fe3f0402dd9..00000000000
Binary files a/tests/test-sessions/test-sessions-common/src/main/resources/proxy-serialization.jar and /dev/null differ
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ClientCrossContextSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ClientCrossContextSessionTest.java
index f818c6f0188..381f7b46cd0 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ClientCrossContextSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ClientCrossContextSessionTest.java
@@ -107,6 +107,7 @@ public class ClientCrossContextSessionTest
public static class TestServletA extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
public String sessionId;
@Override
@@ -130,6 +131,7 @@ public class ClientCrossContextSessionTest
public static class TestServletB extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
public String sessionId;
@Override
@@ -141,8 +143,6 @@ public class ClientCrossContextSessionTest
sessionId = session.getId();
-
-
// Add something to the session
session.setAttribute("B", "B");
@@ -151,6 +151,4 @@ public class ClientCrossContextSessionTest
assertTrue(objectA == null);
}
}
-
-
}
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/CreationTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/CreationTest.java
index 4664be00432..f942c00f4b0 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/CreationTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/CreationTest.java
@@ -335,6 +335,7 @@ public class CreationTest
public static class TestServlet extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
public String _id = null;
public CountDownLatch _synchronizer;
public SessionDataStore _store;
@@ -421,6 +422,8 @@ public class CreationTest
public static class TestServletB extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DefaultSessionCacheTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DefaultSessionCacheTest.java
new file mode 100644
index 00000000000..34e86b44dcd
--- /dev/null
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DefaultSessionCacheTest.java
@@ -0,0 +1,672 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+
+package org.eclipse.jetty.server.session;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.http.HttpSessionActivationListener;
+import javax.servlet.http.HttpSessionEvent;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.junit.Test;
+
+/**
+ * DefaultSessionCacheTest
+ *
+ *
+ */
+public class DefaultSessionCacheTest
+{
+
+ public static class TestSessionActivationListener implements HttpSessionActivationListener
+ {
+ public int passivateCalls = 0;
+ public int activateCalls = 0;
+
+ @Override
+ public void sessionWillPassivate(HttpSessionEvent se)
+ {
+ ++passivateCalls;
+ }
+
+ @Override
+ public void sessionDidActivate(HttpSessionEvent se)
+ {
+ ++activateCalls;
+ }
+
+ }
+
+
+ /**
+ * Test sessions are saved when shutdown with a store.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testShutdownWithSessionStore()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore(true);//fake passivation
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+
+ context.start();
+
+ //put a session in the cache and store
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ Session session = cache.newSession(data);
+ TestSessionActivationListener listener = new TestSessionActivationListener();
+ cache.put("1234", session);
+ assertTrue(cache.contains("1234"));
+ session.setAttribute("aaa", listener);
+ cache.put("1234", session);
+
+ assertTrue(store.exists("1234"));
+ assertTrue(cache.contains("1234"));
+
+ context.stop(); //calls shutdown
+
+ assertTrue(store.exists("1234"));
+ assertFalse(cache.contains("1234"));
+ assertEquals(2, listener.passivateCalls);
+ assertEquals(1, listener.activateCalls);
+ }
+
+
+
+
+ /**
+ * Test that a new Session object can be created from
+ * previously persisted data (SessionData).
+ * @throws Exception
+ */
+ @Test
+ public void testNewSessionFromPersistedData()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore(true);//fake passivation
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+
+ context.start();
+
+ long now = System.currentTimeMillis();
+ //fake persisted data
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ Session session = cache.newSession(data);
+ assertNotNull(session);
+ assertEquals("1234", session.getId());
+ }
+
+ /**
+ * Test that a session id can be renewed.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testRenewSessionId()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore(true);//fake passivation
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+
+ context.start();
+
+ //put a session in the cache and store
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ Session session = cache.newSession(data);
+ cache.put("1234", session);
+ assertTrue(cache.contains("1234"));
+
+ cache.renewSessionId("1234", "5678");
+
+ assertTrue(cache.contains("5678"));
+ assertFalse(cache.contains("1234"));
+
+ assertTrue(store.exists("5678"));
+ assertFalse(store.exists("1234"));
+ }
+
+
+
+ /**
+ * Test that a session that is in the cache can be retrieved.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testGetSessionInCache()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ Session session = cache.newSession(data);
+
+ //put the session in the cache
+ cache.put("1234", session);
+
+ assertNotNull(cache.get("1234"));
+ }
+
+ /**
+ * Test that the cache can load from the SessionDataStore
+ * @throws Exception
+ */
+ @Test
+ public void testGetSessionNotInCache()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //put session data into the store
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ store.store("1234", data);
+
+ assertFalse(cache.contains("1234"));
+
+ Session session = cache.get("1234");
+ assertNotNull(session);
+ assertEquals("1234", session.getId());
+ assertEquals(now-20, session.getCreationTime());
+ }
+
+ @Test
+ public void testPutRequestsStillActive()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //make a session
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ data.setExpiry(now+TimeUnit.DAYS.toMillis(1));
+ Session session = cache.newSession(data);
+ session.access(now); //simulate request still active
+ cache.put("1234", session);
+ assertTrue(session.isResident());
+ assertTrue(cache.contains("1234"));
+ assertFalse(store.exists("1234"));
+
+ }
+
+ @Test
+ public void testPutLastRequest()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //make a session
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ data.setExpiry(now+TimeUnit.DAYS.toMillis(1));
+ Session session = cache.newSession(data);
+ session.access(now); //simulate request still active
+ session.complete(); //simulate request exiting
+ cache.put("1234", session);
+ assertTrue(session.isResident());
+ assertTrue(cache.contains("1234"));
+ assertTrue(store.exists("1234"));
+
+ }
+
+ /**
+ * Test contains method.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testContains()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //test one that isn't contained
+ assertFalse(cache.contains("1234"));
+
+ //test one that is contained
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ Session session = cache.newSession(data);
+
+ //put the session in the cache
+ cache.put("1234", session);
+ assertTrue(cache.contains("1234"));
+ }
+
+ /**
+ * Test the exist method.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testExists()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //test one that doesn't exist at all
+ assertFalse(cache.exists("1234"));
+
+ //test one that only exists in the store
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ store.store("1234", data);
+ assertTrue(cache.exists("1234"));
+
+ //test one that exists in the cache also
+ Session session = cache.newSession(data);
+ cache.put("1234", session);
+ assertTrue(cache.exists("1234"));
+ }
+
+
+ /**
+ * Test the delete method.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testDelete()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //test remove non-existant session
+ Session session = cache.delete("1234");
+ assertNull(session);
+
+ //test remove of existing session in store only
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ store.store("1234", data);
+ session = cache.delete("1234");
+ assertNotNull(session);
+ assertFalse(store.exists("1234"));
+ assertFalse(cache.contains("1234"));
+
+ //test remove of session in both store and cache
+ data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ session = cache.newSession(data);
+ cache.put("1234", session);
+ assertTrue(store.exists("1234"));
+ assertTrue(cache.contains("1234"));
+ session = cache.delete("1234");
+ assertNotNull(session);
+ assertFalse(store.exists("1234"));
+ assertFalse(cache.contains("1234"));
+ }
+
+
+
+ @Test
+ public void testExpiration()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //test no candidates, no data in store
+ Set result = cache.checkExpiration(Collections.emptySet());
+ assertTrue(result.isEmpty());
+
+ //test candidates that are in the cache and NOT expired
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ data.setExpiry(now+TimeUnit.DAYS.toMillis(1));
+ Session session = cache.newSession(data);
+ cache.put("1234", session);
+ assertTrue(cache.exists("1234"));
+ result = cache.checkExpiration(Collections.singleton("1234"));
+ assertTrue(result.isEmpty());
+
+ //test candidates that are in the cache AND expired
+ data.setExpiry(1);
+ cache.put("1234", session);
+ result = cache.checkExpiration(Collections.singleton("1234"));
+ assertEquals(1, result.size());
+ assertEquals("1234", result.iterator().next());
+
+ //test candidates that are not in the cache
+ SessionData data2 = store.newSessionData("567", now-50, now-40, now-30, TimeUnit.MINUTES.toMillis(10));
+ data2.setExpiry(1);
+ store.store("567", data2);
+
+ result = cache.checkExpiration(Collections.emptySet());
+ assertNotNull(result);
+ assertEquals(2, result.size());
+ assertTrue(result.contains("1234"));
+ assertTrue(result.contains("567"));
+ }
+
+ @Test
+ public void testCheckInactiveSession()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //test NEVER EVICT
+ //test session that is not resident
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ data.setExpiry(now+TimeUnit.DAYS.toMillis(1));
+ Session session = cache.newSession(data);
+ cache.checkInactiveSession(session);
+ assertFalse(store.exists("1234"));
+ assertFalse(cache.contains("1234"));
+ assertFalse(session.isResident());
+ //ie nothing happens to the session
+
+ //test session that is resident but not valid
+ cache.put("1234", session);
+ session._state = Session.State.INVALID;
+ cache.checkInactiveSession(session);
+ assertTrue(store.exists("1234"));
+ assertTrue(cache.contains("1234"));
+ assertTrue(session.isResident());
+ assertFalse(session.isValid());
+ //ie nothing happens to the session
+
+ //test session that is resident, is valid, but NEVER_EVICT
+ session._state = Session.State.VALID;
+ cache.checkInactiveSession(session);
+ assertTrue(store.exists("1234"));
+ assertTrue(cache.contains("1234"));
+ assertTrue(session.isResident());
+ assertTrue(session.isValid());
+ //ie nothing happens to the session
+
+ //test EVICT_ON_INACTIVITY, session has passed the inactivity time
+ cache.setEvictionPolicy(SessionCache.EVICT_ON_INACTIVITY);
+ data.setAccessed(now-TimeUnit.SECONDS.toMillis(30));
+ cache.checkInactiveSession(session);
+ assertFalse(cache.contains("1234"));
+ assertFalse(session.isResident());
+
+ //test EVICT_ON_SESSION_EXIT with requests still active.
+ //this should not affect the session because it this is an idle test only
+ SessionData data2 = store.newSessionData("567", now, now-TimeUnit.SECONDS.toMillis(30), now-TimeUnit.SECONDS.toMillis(40), TimeUnit.MINUTES.toMillis(10));
+ data2.setExpiry(now+TimeUnit.DAYS.toMillis(1));//not expired
+ Session session2 = cache.newSession(data2);
+ cache.put("567", session2);//ensure session is in cache
+ cache.setEvictionPolicy(SessionCache.EVICT_ON_SESSION_EXIT);
+ session2.access(System.currentTimeMillis());//simulate 1 request in session
+ assertTrue(cache.contains("567"));
+ cache.checkInactiveSession(session2);
+ assertTrue(cache.contains("567")); //not evicted
+
+
+ //test EVICT_ON_SESSION_EXIT - requests not active
+ //this should not affect the session because this is an idle test only
+ session2.complete(); //simulate last request leaving session
+ cache.checkInactiveSession(session2);
+ assertTrue(cache.contains("567"));
+
+ }
+
+ @Test
+ public void testSaveOnEviction ()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.EVICT_ON_INACTIVITY); //evict after 1 second inactivity
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ //make a session
+ long now = System.currentTimeMillis();
+ SessionData data = store.newSessionData("1234", now-20, now-10, now-20, TimeUnit.MINUTES.toMillis(10));
+ data.setExpiry(now+TimeUnit.DAYS.toMillis(1));
+ Session session = cache.newSession(data);
+ cache.put("1234", session); //make it resident
+ assertTrue(cache.contains("1234"));
+ long accessed = now-TimeUnit.SECONDS.toMillis(30); //make it idle
+ data.setAccessed(accessed);
+ cache.checkInactiveSession(session);
+ assertFalse(cache.contains("1234"));
+ assertFalse(session.isResident());
+ SessionData retrieved = store.load("1234");
+ assertEquals(accessed, retrieved.getAccessed()); //check that we persisted the session before we evicted
+ }
+
+ @Test
+ public void testSaveOnCreateTrue ()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ cacheFactory.setSaveOnCreate(true);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ long now = System.currentTimeMillis();
+ cache.newSession(null, "1234", now, TimeUnit.MINUTES.toMillis(10));
+ assertTrue(store.exists("1234"));
+ }
+
+
+ @Test
+ public void testSaveOnCreateFalse ()
+ throws Exception
+ {
+ Server server = new Server();
+
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/test");
+ context.setServer(server);
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ cacheFactory.setSaveOnCreate(false);
+ DefaultSessionCache cache = (DefaultSessionCache)cacheFactory.getSessionCache(context.getSessionHandler());
+
+ TestSessionDataStore store = new TestSessionDataStore();
+ cache.setSessionDataStore(store);
+ context.getSessionHandler().setSessionCache(cache);
+ context.start();
+
+ long now = System.currentTimeMillis();
+ cache.newSession(null, "1234", now, TimeUnit.MINUTES.toMillis(10));
+ assertFalse(store.exists("1234"));
+ }
+
+
+}
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java
index e14893dc4cc..53922ca3b45 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java
@@ -140,12 +140,14 @@ public class DeleteUnloadableSessionTest
}
/**
- * TestServlet
+ * TestFooServlet
*
*
*/
public static class TestServlet extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DirtyAttributeTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DirtyAttributeTest.java
index 414327c96f2..27301843c40 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DirtyAttributeTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DirtyAttributeTest.java
@@ -38,7 +38,6 @@ import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.junit.After;
import org.junit.Test;
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/IdleSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/IdleSessionTest.java
index 659d5b71730..1354212d2e5 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/IdleSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/IdleSessionTest.java
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -44,31 +45,18 @@ import org.junit.Test;
/**
* IdleSessionTest
*
- * Checks that a session can be idled and de-idled on the next request if it hasn't expired.
- *
- *
- *
+ * Checks that a session can be passivated and re-activated on the next request if it hasn't expired.
*/
public class IdleSessionTest
{
protected TestServlet _servlet = new TestServlet();
protected TestServer _server1 = null;
-
- /**
- * @param sec
- */
- public void pause (int sec)
+
+ public void pause (int sec)throws InterruptedException
{
- try
- {
- Thread.sleep(sec * 1000L);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
+ Thread.sleep(TimeUnit.SECONDS.toMillis(sec));
}
/**
@@ -81,7 +69,7 @@ public class IdleSessionTest
String servletMapping = "/server";
int inactivePeriod = 20;
int scavengePeriod = 3;
- int evictionSec = 5;
+ int evictionSec = 5; //evict from cache if idle for 5 sec
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evictionSec);
@@ -106,32 +94,32 @@ public class IdleSessionTest
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
- //and wait until the session should be idled out
- pause(evictionSec*3);
+ //and wait until the session should be passivated out
+ pause(evictionSec*2);
//check that the session has been idled
String id = TestServer.extractSessionId(sessionCookie);
assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
- //make another request to de-idle the session
+ //make another request to reactivate the session
Request request = client.newRequest(url + "?action=test");
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
- //check session de-idled
+ //check session reactivated
assertTrue(contextHandler.getSessionHandler().getSessionCache().contains(id));
- //wait again for the session to be idled
- pause(evictionSec*3);
+ //wait again for the session to be passivated
+ pause(evictionSec*2);
//check that it is
assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
- //While idle, take some action to ensure that a deidle won't work, like
+ //While passivated, take some action to ensure that a reactivate won't work, like
//deleting the sessions in the store
((TestSessionDataStore)contextHandler.getSessionHandler().getSessionCache().getSessionDataStore())._map.clear();
@@ -140,7 +128,7 @@ public class IdleSessionTest
response2 = request.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
- //Test trying to de-idle an expired session (ie before the scavenger can get to it)
+ //Test trying to reactivate an expired session (ie before the scavenger can get to it)
//make a request to set up a session on the server
response = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
@@ -149,20 +137,20 @@ public class IdleSessionTest
id = TestServer.extractSessionId(sessionCookie);
//and wait until the session should be idled out
- pause(evictionSec * 3);
+ pause(evictionSec * 2);
//stop the scavenger
if (_server1.getHouseKeeper() != null)
_server1.getHouseKeeper().stop();
- //check that the session is idle
+ //check that the session is passivated
assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
//wait until the session should be expired
pause (inactivePeriod + (3*scavengePeriod));
- //make another request to de-idle the session
+ //make another request to reactivate the session
request = client.newRequest(url + "?action=testfail");
response2 = request.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ImmortalSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ImmortalSessionTest.java
index af20e84b78f..2bb42a84ac2 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ImmortalSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ImmortalSessionTest.java
@@ -19,8 +19,8 @@
package org.eclipse.jetty.server.session;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.PrintWriter;
@@ -131,6 +131,8 @@ public class ImmortalSessionTest
public static class TestServlet extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractModifyMaxInactiveIntervalTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ModifyMaxInactiveIntervalTest.java
similarity index 84%
rename from tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractModifyMaxInactiveIntervalTest.java
rename to tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ModifyMaxInactiveIntervalTest.java
index 37a9b37a426..8c48d4cdece 100644
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractModifyMaxInactiveIntervalTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ModifyMaxInactiveIntervalTest.java
@@ -19,9 +19,11 @@
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;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -37,28 +39,81 @@ import org.junit.Test;
/**
- * AbstractModifyMaxInactiveIntervalTest
+ * ModifyMaxInactiveIntervalTest
*
*
*
*/
-public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTestBase
+public class ModifyMaxInactiveIntervalTest extends AbstractTestBase
{
- public static int newMaxInactive = 20;
public static int __scavenge = 1;
+
+
+ /**
+ * Test that setting an integer overflow valued max inactive interval
+ * results in an immortal session (value -1).
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testHugeMaxInactiveInterval() throws Exception
+ {
+ int inactivePeriod = Integer.MAX_VALUE * 60; //integer overflow
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
+
+ TestServer server = new TestServer(0, inactivePeriod, __scavenge, cacheFactory, storeFactory);
+ ServletContextHandler ctxA = server.addContext("/mod");
+ ctxA.addServlet(TestModServlet.class, "/test");
+
+ server.start();
+ int port=server.getPort();
+ try
+ {
+ HttpClient client = new HttpClient();
+ client.start();
+ try
+ {
+ // Perform a request to create a session
+ ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
+
+ assertEquals(HttpServletResponse.SC_OK,response.getStatus());
+ String sessionCookie = response.getHeaders().get("Set-Cookie");
+ assertTrue(sessionCookie != null);
+ String id = TestServer.extractSessionId(sessionCookie);
+
+ //check that the maxInactive is -1
+ Session s = ctxA.getSessionHandler().getSession(id);
+ assertEquals(-1, s.getMaxInactiveInterval());
+
+ }
+ finally
+ {
+ client.stop();
+ }
+ }
+ finally
+ {
+ server.stop();
+ }
+
+ }
@Test
public void testReduceMaxInactiveInterval() throws Exception
{
int oldMaxInactive = 30;
int newMaxInactive = 1;
+ int scavengeSec = __scavenge;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
- TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
+ TestServer server = new TestServer(0, oldMaxInactive, scavengeSec, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
@@ -83,7 +138,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
// Wait for the session to expire
- Thread.sleep(1500*newMaxInactive);
+ Thread.sleep(TimeUnit.SECONDS.toMillis(newMaxInactive+scavengeSec));
//do another request using the cookie to ensure the session is NOT there
request= client.newRequest("http://localhost:" + port + "/mod/test?action=test&val="+newMaxInactive);
@@ -108,13 +163,14 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
{
int oldMaxInactive = 1;
int newMaxInactive = 10;
+ int scavengeSec = __scavenge;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
- TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
+ TestServer server = new TestServer(0, oldMaxInactive, scavengeSec, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
@@ -139,7 +195,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
// wait until the old inactive interval should have expired
- Thread.sleep(1100*oldMaxInactive);
+ Thread.sleep(TimeUnit.SECONDS.toMillis(scavengeSec+oldMaxInactive));
//do another request using the cookie to ensure the session is still there
request= client.newRequest("http://localhost:" + port + "/mod/test?action=test&val="+newMaxInactive);
@@ -166,13 +222,14 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
int newMaxInactive = 120; //2min
int evict = 2;
int sleep = evict;
+ int scavenge = __scavenge;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evict);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
- TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
+ TestServer server = new TestServer(0, oldMaxInactive, scavenge, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
@@ -221,13 +278,14 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
int newMaxInactive = 2;
int evict = 4;
int sleep = evict;
+ int scavenge = __scavenge;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evict);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
- TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
+ TestServer server = new TestServer(0, oldMaxInactive, scavenge, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
@@ -273,13 +331,14 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
{
int oldMaxInactive = -1;
int newMaxInactive = 120;
+ int scavenge = __scavenge;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
- TestServer server = new TestServer(0, oldMaxInactive, 1, cacheFactory, storeFactory);
+ TestServer server = new TestServer(0, oldMaxInactive, scavenge, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
@@ -324,15 +383,15 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
public void testNoExpireSessionInUse() throws Exception
{
int maxInactive = 3;
- int sleep = maxInactive + (int)(maxInactive * 0.8);
-
+ int scavenge = __scavenge;
+ int sleep = maxInactive + scavenge;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
- TestServer server = new TestServer(0, maxInactive, 1, cacheFactory, storeFactory);
+ TestServer server = new TestServer(0, maxInactive, scavenge, cacheFactory, storeFactory);
ServletContextHandler ctxA = server.addContext("/mod");
ctxA.addServlet(TestModServlet.class, "/test");
@@ -345,7 +404,6 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
try
{
// Perform a request to create a session
-
ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
@@ -376,7 +434,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
{
int oldMaxInactive = 4;
int newMaxInactive = 20;
- int sleep = oldMaxInactive+(int)(oldMaxInactive * 0.8);
+ int sleep = oldMaxInactive+__scavenge;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
@@ -395,8 +453,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
client.start();
try
{
- // Perform a request to create a session
-
+ // Perform a request to create a session
ContentResponse response = client.GET("http://localhost:" + port + "/mod/test?action=create");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
@@ -410,7 +467,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
//wait for longer than the old inactive interval
- Thread.sleep(sleep*1000L);
+ Thread.sleep(TimeUnit.SECONDS.toMillis(sleep));
//do another request using the cookie to ensure the session is still there
request= client.newRequest("http://localhost:" + port + "/mod/test?action=test&val="+newMaxInactive);
@@ -488,6 +545,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
if ("create".equals(action))
{
HttpSession session = request.getSession(true);
+ assertNotNull(session);
return;
}
@@ -502,7 +560,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
int wait = (tmp==null?0:Integer.parseInt(tmp));
if (wait >0)
{
- try { Thread.sleep(wait*1000);}catch (Exception e) {throw new ServletException(e);}
+ try { Thread.sleep(TimeUnit.SECONDS.toMillis(wait));}catch (Exception e) {throw new ServletException(e);}
}
HttpSession session = request.getSession(false);
if (session == null)
@@ -531,7 +589,7 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
if (interval > 0)
{
- try{Thread.sleep(interval*1000);}catch (Exception e) {throw new ServletException(e);}
+ try{Thread.sleep(TimeUnit.SECONDS.toMillis(interval));}catch (Exception e) {throw new ServletException(e);}
}
session = request.getSession(false);
@@ -558,5 +616,15 @@ public abstract class AbstractModifyMaxInactiveIntervalTest extends AbstractTest
}
}
}
+
+
+ /**
+ *
+ */
+ @Override
+ public SessionDataStoreFactory createSessionDataStoreFactory()
+ {
+ return new TestSessionDataStoreFactory();
+ }
}
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractNonClusteredSessionScavengingTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/NonClusteredSessionScavengingTest.java
similarity index 67%
rename from tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractNonClusteredSessionScavengingTest.java
rename to tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/NonClusteredSessionScavengingTest.java
index 3e2abd8e2cc..7e16dfdcb30 100644
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractNonClusteredSessionScavengingTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/NonClusteredSessionScavengingTest.java
@@ -20,11 +20,14 @@ package org.eclipse.jetty.server.session;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.IOException;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -39,31 +42,95 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.Test;
/**
- * AbstractNonClusteredSessionScavengingTest
+ * NonClusteredSessionScavengingTest
*
* Create a session, wait for it to be scavenged, re-present the cookie and check that a
* new session is created.
*/
-public abstract class AbstractNonClusteredSessionScavengingTest extends AbstractTestBase
+public class NonClusteredSessionScavengingTest extends AbstractTestBase
{
public SessionDataStore _dataStore;
-
- public abstract void assertSession (String id, boolean exists);
-
- public void pause(int scavenge)
+
+ public void pause(int scavenge) throws InterruptedException
{
+ Thread.sleep(TimeUnit.SECONDS.toMillis(scavenge));
+ }
+
+
+ @Test
+ public void testNoScavenging() throws Exception
+ {
+ String contextPath = "/";
+ String servletMapping = "/server";
+ int inactivePeriod = 3;
+ int scavengePeriod = 0; //turn off scavenging
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
+
+
+ TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
+ cacheFactory, storeFactory);
+ ServletContextHandler context1 = server1.addContext(contextPath);
+ context1.addServlet(TestServlet.class, servletMapping);
+ TestHttpSessionListener listener = new TestHttpSessionListener();
+ context1.getSessionHandler().addEventListener(listener);
+
+
try
{
- Thread.sleep(scavenge * 1000L);
+ server1.start();
+ int port1 = server1.getPort();
+
+ HttpClient client = new HttpClient();
+ client.start();
+ try
+ {
+ String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
+
+
+ // Create the session
+ ContentResponse response1 = client.GET(url + "?action=create");
+ assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
+ String sessionCookie = response1.getHeaders().get("Set-Cookie");
+ assertTrue(sessionCookie != null);
+ SessionHandler m1 = context1.getSessionHandler();
+ assertEquals(1, m1.getSessionsCreated());
+
+
+ // Wait a while to ensure that the session should have expired, if the
+ //scavenger was running
+ pause(2*inactivePeriod);
+
+ assertEquals(1, m1.getSessionsCreated());
+
+
+ if (m1 instanceof TestSessionHandler)
+ {
+ ((TestSessionHandler)m1).assertCandidatesForExpiry(0);
+ }
+
+ //check the session listener did not get called
+ assertTrue(listener.destroyedSessions.isEmpty());
+ }
+ finally
+ {
+ client.stop();
+ }
}
- catch (InterruptedException e)
+ finally
{
- e.printStackTrace();
+ server1.stop();
}
}
+
+
+
+
@Test
public void testNewSession() throws Exception
{
@@ -75,12 +142,12 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
+
TestServer server = new TestServer(0, maxInactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
+ cacheFactory, storeFactory);
ServletContextHandler context = server.addContext("/");
_dataStore = context.getSessionHandler().getSessionCache().getSessionDataStore();
-
+
context.addServlet(TestServlet.class, servletMapping);
String contextPath = "/";
@@ -100,13 +167,15 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
// Let's wait for the scavenger to run
pause(maxInactivePeriod + scavengePeriod);
- assertSession (TestServer.extractSessionId(sessionCookie), false);
+ assertFalse(_dataStore.exists(TestServer.extractSessionId(sessionCookie)));
// The session should not be there anymore, but we present an old cookie
// The server should create a new session.
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=old-create");
response = request.send();
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
+ String sessionCookie2 = response.getHeaders().get("Set-Cookie");
+ assertNotEquals(TestServer.extractSessionId(sessionCookie), TestServer.extractSessionId(sessionCookie2));
}
finally
{
@@ -123,7 +192,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
public void testImmortalSession() throws Exception
{
String servletMapping = "/server";
- int scavengePeriod = 3;
+ int scavengePeriod = 1;
int maxInactivePeriod = 0;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
@@ -155,7 +224,7 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
// Let's wait for the scavenger to run
pause(2*scavengePeriod);
- assertSession(TestServer.extractSessionId(sessionCookie), true);
+ assertTrue(_dataStore.exists(TestServer.extractSessionId(sessionCookie)));
// Test that the session is still there
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping.substring(1) + "?action=old-test");
@@ -203,8 +272,17 @@ public abstract class AbstractNonClusteredSessionScavengingTest extends Abstract
}
else
{
- assertTrue(false);
+ fail("Unknown servlet action");
}
}
}
+
+ /**
+ *
+ */
+ @Override
+ public SessionDataStoreFactory createSessionDataStoreFactory()
+ {
+ return new TestSessionDataStoreFactory();
+ }
}
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RedirectSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RedirectSessionTest.java
index 01cb1e63699..16049071210 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RedirectSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RedirectSessionTest.java
@@ -42,8 +42,6 @@ import org.junit.Test;
*/
public class RedirectSessionTest
{
-
-
@Test
public void testSessionRedirect() throws Exception
@@ -58,9 +56,6 @@ public class RedirectSessionTest
testServletContextHandler.addServlet(Servlet1.class, "/one");
testServletContextHandler.addServlet(Servlet2.class, "/two");
-
-
-
try
{
testServer.start();
@@ -89,6 +84,8 @@ public class RedirectSessionTest
public static class Servlet1 extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
@@ -102,6 +99,8 @@ public class RedirectSessionTest
public static class Servlet2 extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
@@ -113,7 +112,4 @@ public class RedirectSessionTest
}
}
-
-
-
}
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ReentrantRequestSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ReentrantRequestSessionTest.java
index a9cbf650945..f415fe1cb63 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ReentrantRequestSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/ReentrantRequestSessionTest.java
@@ -94,6 +94,8 @@ public class ReentrantRequestSessionTest
public static class TestServlet extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
@@ -103,8 +105,6 @@ public class ReentrantRequestSessionTest
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
-
-
String action = request.getParameter("action");
if ("create".equals(action))
{
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java
index 7e1b47f1238..435f71ac038 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/RemoveSessionTest.java
@@ -81,7 +81,7 @@ public class RemoveSessionTest
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
- //ensure sessionCreated listener is called
+ //ensure sessionCreated bindingListener is called
assertTrue (testListener.isCreated());
assertEquals(1, m.getSessionsCreated());
assertEquals(1, ((DefaultSessionCache)m.getSessionCache()).getSessionsMax());
@@ -91,7 +91,7 @@ public class RemoveSessionTest
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=delete");
response = request.send();
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
- //ensure sessionDestroyed listener is called
+ //ensure sessionDestroyed bindingListener is called
assertTrue(testListener.isDestroyed());
assertEquals(0, ((DefaultSessionCache)m.getSessionCache()).getSessionsCurrent());
assertEquals(1, ((DefaultSessionCache)m.getSessionCache()).getSessionsMax());
@@ -121,7 +121,8 @@ public class RemoveSessionTest
}
public static class TestServlet extends HttpServlet
{
- @Override
+ private static final long serialVersionUID = 1L;
+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String action = request.getParameter("action");
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameContextForwardedSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameContextForwardedSessionTest.java
index f02d89c8ab2..6b68b026351 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameContextForwardedSessionTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameContextForwardedSessionTest.java
@@ -64,8 +64,7 @@ public class SameContextForwardedSessionTest
@Test
public void testSessionCreateInForward() throws Exception
- {
-
+ {
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
@@ -77,8 +76,6 @@ public class SameContextForwardedSessionTest
testServletContextHandler.addServlet(Servlet2.class, "/two");
testServletContextHandler.addServlet(Servlet3.class, "/three");
testServletContextHandler.addServlet(Servlet4.class, "/four");
-
-
try
{
@@ -122,6 +119,7 @@ public class SameContextForwardedSessionTest
public static class Servlet1 extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
CountDownLatch _synchronizer;
public void setSynchronizer(CountDownLatch sync)
@@ -151,6 +149,8 @@ public class SameContextForwardedSessionTest
public static class Servlet2 extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
@@ -171,6 +171,8 @@ public class SameContextForwardedSessionTest
public static class Servlet3 extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
@@ -189,6 +191,8 @@ public class SameContextForwardedSessionTest
public static class Servlet4 extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameNodeLoadTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameNodeLoadTest.java
index ec13b65484c..f3f0b43ff36 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameNodeLoadTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SameNodeLoadTest.java
@@ -189,6 +189,8 @@ public class SameNodeLoadTest
public static class TestServlet
extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SaveOptimizeTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SaveOptimizeTest.java
index 4e51832572f..d2b5e064623 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SaveOptimizeTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SaveOptimizeTest.java
@@ -53,12 +53,9 @@ import org.junit.Test;
*/
public class SaveOptimizeTest
{
-
protected TestServlet _servlet;
protected TestServer _server1 = null;
-
-
/**
* Create and then invalidate a session in the same request.
@@ -488,6 +485,7 @@ public class SaveOptimizeTest
public static class TestServlet extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
public String _id = null;
public SessionDataStore _store;
public HttpSession _firstSession = null;
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java
index 440938adfaf..80c4dae9f34 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java
@@ -29,6 +29,7 @@ import static org.junit.Assert.assertNull;
import java.io.IOException;
import java.util.Set;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -153,7 +154,7 @@ public class SessionEvictionFailureTest
/**
- * TestServlet
+ * TestFooServlet
*
*
*/
@@ -179,23 +180,13 @@ public class SessionEvictionFailureTest
}
}
}
-
-
- /**
- * @param sec
- */
- public static void pause (int sec)
+
+
+ public static void pause (int sec) throws InterruptedException
{
- try
- {
- Thread.currentThread().sleep(sec*1000L);
- }
- catch (InterruptedException e)
- {
- //just return;
- }
+ Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(sec));
}
-
+
@Test
diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionInvalidateCreateScavengeTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionInvalidateCreateScavengeTest.java
similarity index 90%
rename from tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionInvalidateCreateScavengeTest.java
rename to tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionInvalidateCreateScavengeTest.java
index 872e0e41349..eef8a67065e 100644
--- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/AbstractSessionInvalidateCreateScavengeTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionInvalidateCreateScavengeTest.java
@@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -46,46 +47,22 @@ import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test;
/**
- * AbstractSessionInvalidateCreateScavengeTest
+ * SessionInvalidateCreateScavengeTest
*
* This test verifies that invalidating an existing session and creating
* a new session within the scope of a single request will expire the
* newly created session correctly (removed from the server and session listeners called).
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=377610
*/
-public abstract class AbstractSessionInvalidateCreateScavengeTest extends AbstractTestBase
+public class SessionInvalidateCreateScavengeTest extends AbstractTestBase
{
- public class MySessionListener implements HttpSessionListener
+ @Override
+ public SessionDataStoreFactory createSessionDataStoreFactory()
{
- List destroys = new ArrayList<>();
-
- @Override
- public void sessionCreated(HttpSessionEvent e)
- {
-
- }
-
- @Override
- public void sessionDestroyed(HttpSessionEvent e)
- {
- destroys.add(e.getSession().hashCode());
- }
+ return new TestSessionDataStoreFactory();
}
-
- public void pause(int scavengePeriod)
- {
- try
- {
- Thread.sleep(scavengePeriod * 1000L);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
-
@Test
@SuppressWarnings("ReferenceEquality")
public void testSessionScavenge() throws Exception
@@ -93,22 +70,22 @@ public abstract class AbstractSessionInvalidateCreateScavengeTest extends Abstra
String contextPath = "/";
String servletMapping = "/server";
int inactivePeriod = 6;
- int scavengePeriod = 3;
-
+ int scavengePeriod = 1;
+
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
-
+
TestServer server = new TestServer(0, inactivePeriod, scavengePeriod,
- cacheFactory, storeFactory);
+ cacheFactory, storeFactory);
ServletContextHandler context = server.addContext(contextPath);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
context.addServlet(holder, servletMapping);
MySessionListener listener = new MySessionListener();
context.getSessionHandler().addEventListener(listener);
-
+
try
{
server.start();
@@ -126,14 +103,14 @@ public abstract class AbstractSessionInvalidateCreateScavengeTest extends Abstra
assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
-
+
// Make a request which will invalidate the existing session and create a new one
Request request2 = client.newRequest(url + "?action=test");
ContentResponse response2 = request2.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
// Wait for the scavenger to run
- pause(inactivePeriod+(2*scavengePeriod));
+ Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(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
@@ -153,15 +130,24 @@ public abstract class AbstractSessionInvalidateCreateScavengeTest extends Abstra
}
}
- public static class Foo implements Serializable
+ public class MySessionListener implements HttpSessionListener
{
- public boolean bar = false;
-
- public boolean getBar() { return bar;};
+ List destroys = new ArrayList<>();
+
+ public void sessionCreated(HttpSessionEvent e)
+ {
+
+ }
+
+ public void sessionDestroyed(HttpSessionEvent e)
+ {
+ destroys.add(e.getSession().hashCode());
+ }
}
public static class MySessionBindingListener implements HttpSessionBindingListener, Serializable
{
+ private static final long serialVersionUID = 1L;
private boolean unbound = false;
@Override
@@ -179,6 +165,7 @@ public abstract class AbstractSessionInvalidateCreateScavengeTest extends Abstra
public static class TestServlet extends HttpServlet
{
+ private static final long serialVersionUID = 1L;
public MySessionBindingListener listener = new MySessionBindingListener();
@@ -220,11 +207,12 @@ public abstract class AbstractSessionInvalidateCreateScavengeTest extends Abstra
assertTrue(!newId.equals(oldId));
assertTrue (session.getAttribute("identity")==null);
session.setAttribute("identity", "session2");
- session.setAttribute("listener", listener);
+ session.setAttribute("bindingListener", listener);
}
else
fail("Session already missing");
}
}
}
+
}
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java
index 21352f10651..7a034922e99 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java
@@ -19,11 +19,15 @@
package org.eclipse.jetty.server.session;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.Serializable;
+import java.net.HttpCookie;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -43,39 +47,17 @@ import org.junit.Test;
/**
* SessionListenerTest
*
- * Test that the HttpSessionBindingListeners are called.
+ * Test that session listeners are called.
*/
public class SessionListenerTest
{
- public static class MySessionBindingListener implements HttpSessionBindingListener, Serializable
- {
- boolean unbound = false;
- boolean bound = false;
-
- @Override
- public void valueUnbound(HttpSessionBindingEvent event)
- {
- unbound = true;
- }
-
- @Override
- public void valueBound(HttpSessionBindingEvent event)
- {
- bound = true;
- }
- }
-
- public static class Foo implements Serializable
- {
- public boolean bar = false;
-
- public boolean getBar() { return bar;};
- }
-
-
-
+ /**
+ * Test that listeners are called when a session is deliberately invalidated.
+ *
+ * @throws Exception
+ */
@Test
- public void testListener() throws Exception
+ public void testListenerWithInvalidation() throws Exception
{
String contextPath = "";
String servletMapping = "/server";
@@ -90,6 +72,8 @@ public class SessionListenerTest
TestServer server = new TestServer(0, inactivePeriod, scavengePeriod,
cacheFactory, storeFactory);
ServletContextHandler context = server.addContext(contextPath);
+ TestHttpSessionListener listener = new TestHttpSessionListener(true);
+ context.getSessionHandler().addEventListener(listener);
TestServlet servlet = new TestServlet();
ServletHolder holder = new ServletHolder(servlet);
context.addServlet(holder, servletMapping);
@@ -104,22 +88,23 @@ public class SessionListenerTest
try
{
String url = "http://localhost:" + port1 + contextPath + servletMapping;
-
-
// Create the session
ContentResponse response1 = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
String sessionCookie = response1.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
- assertTrue (servlet.listener.bound);
+ assertTrue (TestServlet.bindingListener.bound);
+ String sessionId = TestServer.extractSessionId(sessionCookie);
+ assertTrue(listener.createdSessions.contains(sessionId));
// Make a request which will invalidate the existing session
Request request2 = client.newRequest(url + "?action=test");
ContentResponse response2 = request2.send();
assertEquals(HttpServletResponse.SC_OK,response2.getStatus());
- assertTrue (servlet.listener.unbound);
+ assertTrue (TestServlet.bindingListener.unbound);
+ assertTrue (listener.destroyedSessions.contains(sessionId));
}
finally
{
@@ -131,10 +116,158 @@ public class SessionListenerTest
server.stop();
}
}
+
+
+ /**
+ * Test that listeners are called when a session expires.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testSessionExpiresWithListener() throws Exception
+ {
+ String contextPath = "/";
+ String servletMapping = "/server";
+ int inactivePeriod = 3;
+ int scavengePeriod = 1;
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
+
+ TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
+ cacheFactory, storeFactory);
+ TestServlet servlet = new TestServlet();
+ ServletHolder holder = new ServletHolder(servlet);
+ ServletContextHandler context = server1.addContext(contextPath);
+ context.addServlet(holder, servletMapping);
+ TestHttpSessionListener listener = new TestHttpSessionListener(true);
+ context.getSessionHandler().addEventListener(listener);
+
+ server1.start();
+ int port1 = server1.getPort();
+
+ try
+ {
+ HttpClient client = new HttpClient();
+ client.start();
+ String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
+
+ //make a request to set up a session on the server
+ ContentResponse response1 = client.GET(url + "?action=init");
+ assertEquals(HttpServletResponse.SC_OK,response1.getStatus());
+ String sessionCookie = response1.getHeaders().get("Set-Cookie");
+ assertTrue(sessionCookie != null);
+
+ String sessionId = TestServer.extractSessionId(sessionCookie);
+
+ assertTrue(listener.createdSessions.contains(sessionId));
+
+ //and wait until the session should have expired
+ Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(inactivePeriod+(scavengePeriod)));
+
+ assertTrue (listener.destroyedSessions.contains(sessionId));
+
+ assertNull(listener.ex);
+ }
+ finally
+ {
+ server1.stop();
+ }
+ }
+
+ /**
+ * Check that a session that is expired cannot be reused, and expiry listeners are called for it
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testExpiredSession() throws Exception
+ {
+ String contextPath = "/";
+ String servletMapping = "/server";
+ int inactivePeriod = 4;
+ int scavengePeriod = 1;
+
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
+ cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
+ SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
+ ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod);
+
+ TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod,
+ cacheFactory, storeFactory);
+ SimpleTestServlet servlet = new SimpleTestServlet();
+ 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
+ {
+ //save a session that has already expired
+ long now = System.currentTimeMillis();
+ SessionData data = context.getSessionHandler().getSessionCache().getSessionDataStore().newSessionData("1234", now-10, now-5, now-10, 30000);
+ data.setExpiry(100); //make it expired a long time ago
+ context.getSessionHandler().getSessionCache().getSessionDataStore().store("1234", data);
+
+ HttpClient client = new HttpClient();
+ client.start();
+
+ port1 = server1.getPort();
+ String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1);
+
+ //make another request using the id of the expired session
+ Request request = client.newRequest(url + "?action=test");
+ request.cookie(new HttpCookie("JSESSIONID", "1234"));
+ ContentResponse response = request.send();
+ assertEquals(HttpServletResponse.SC_OK,response.getStatus());
+
+ //should be a new session id
+ String cookie2 = response.getHeaders().get("Set-Cookie");
+ assertNotEquals("1234", TestServer.extractSessionId(cookie2));
+
+ assertTrue (listener.destroyedSessions.contains("1234"));
+
+ assertNull(listener.ex);
+
+ }
+ finally
+ {
+ server1.stop();
+ }
+ }
+
+
+
+ public static class MySessionBindingListener implements HttpSessionBindingListener, Serializable
+ {
+ private static final long serialVersionUID = 1L;
+ boolean unbound = false;
+ boolean bound = false;
+
+ public void valueUnbound(HttpSessionBindingEvent event)
+ {
+ unbound = true;
+ }
+
+ public void valueBound(HttpSessionBindingEvent event)
+ {
+ bound = true;
+ }
+ }
+
+
public static class TestServlet extends HttpServlet
{
- public static final MySessionBindingListener listener = new MySessionBindingListener();
+ private static final long serialVersionUID = 1L;
+ public static final MySessionBindingListener bindingListener = new MySessionBindingListener();
@Override
@@ -145,7 +278,7 @@ public class SessionListenerTest
if ("init".equals(action))
{
HttpSession session = request.getSession(true);
- session.setAttribute("foo", listener);
+ session.setAttribute("foo", bindingListener);
assertNotNull(session);
}
@@ -159,4 +292,20 @@ public class SessionListenerTest
}
}
}
+
+ public static class SimpleTestServlet extends HttpServlet
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
+ {
+ String action = request.getParameter("action");
+ if ("test".equals(action))
+ {
+ HttpSession session = request.getSession(true);
+ assertTrue(session != null);
+ }
+ }
+ }
}
diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionRenewTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionRenewTest.java
index b07ecacf338..d7d5092e918 100644
--- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionRenewTest.java
+++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionRenewTest.java
@@ -197,7 +197,8 @@ public class SessionRenewTest
public static class TestServlet extends HttpServlet
{
-
+ private static final long serialVersionUID = 1L;
+
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{