Make calling passivate/active depend on type of session data store; fix tests

This commit is contained in:
Jan Bartel 2015-12-11 11:42:29 +11:00
parent 4793be634f
commit 31ea1704a1
50 changed files with 461 additions and 367 deletions

View File

@ -384,6 +384,15 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore
return reference.get();
}
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
*/
@Override
public boolean isPassivating()
{
return true;
}
}

View File

@ -45,7 +45,6 @@ public class GCloudSessionIdManager extends AbstractSessionIdManager
private final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session");
public static final int DEFAULT_IDLE_EXPIRY_MULTIPLE = 2;
public static final String KIND = "GCloudSessionId";
private Server _server;
private Datastore _datastore;
private KeyFactory _keyFactory;
private GCloudConfiguration _config;
@ -58,8 +57,7 @@ public class GCloudSessionIdManager extends AbstractSessionIdManager
*/
public GCloudSessionIdManager(Server server)
{
super();
_server = server;
super(server);
}
/**
@ -68,8 +66,7 @@ public class GCloudSessionIdManager extends AbstractSessionIdManager
*/
public GCloudSessionIdManager(Server server, Random random)
{
super(random);
_server = server;
super(server,random);
}

View File

@ -121,7 +121,7 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore
try
{
SessionData sd = load(candidate);
if (sd.isExpiredAt(now))
if (sd == null || sd.isExpiredAt(now))
expired.add(candidate);
}
@ -159,6 +159,17 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore
public static String getCacheKey (String id, ContextId contextId)
{
return contextId.toString()+"_"+id;
return contextId.getCanonicalContextPath()+"_"+contextId.getVhost()+"_"+id;
}
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
*/
@Override
public boolean isPassivating()
{
return true;
}
}

View File

@ -70,7 +70,6 @@ public class InfinispanSessionIdManager extends AbstractSessionIdManager
public final static String ID_KEY = "__o.e.j.s.infinispanIdMgr__";
public static final int DEFAULT_IDLE_EXPIRY_MULTIPLE = 2;
protected BasicCache<String,Object> _cache;
private Server _server;
private int _idleExpiryMultiple = DEFAULT_IDLE_EXPIRY_MULTIPLE;
@ -79,14 +78,12 @@ public class InfinispanSessionIdManager extends AbstractSessionIdManager
public InfinispanSessionIdManager(Server server)
{
super();
_server = server;
super(server);
}
public InfinispanSessionIdManager(Server server, Random random)
{
super(random);
_server = server;
super(server, random);
}

View File

@ -345,7 +345,7 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore
for ( DBObject session : verifiedExpiredSessions )
{
String id = (String)session.get(__ID);
if (LOG.isDebugEnabled()) LOG.debug("Mongo confirmed expired session {}", id);
if (LOG.isDebugEnabled()) LOG.debug("{} Mongo confirmed expired session {}", _contextId,id);
expiredSessions.add(id);
}
}
@ -368,7 +368,7 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore
for (DBObject session : oldExpiredSessions)
{
String id = (String)session.get(__ID);
if (LOG.isDebugEnabled()) LOG.debug("Mongo found old expired session {}", id);
if (LOG.isDebugEnabled()) LOG.debug("{} Mongo found old expired session {}", _contextId, id);
expiredSessions.add(id);
}
@ -615,4 +615,14 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore
return temp.get(keyChain[keyChain.length - 1]);
}
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
*/
@Override
public boolean isPassivating()
{
return true;
}
}

View File

@ -54,7 +54,6 @@ public class MongoSessionIdManager extends AbstractSessionIdManager
final DBCollection _sessions;
protected Server _server;
/**
@ -75,9 +74,8 @@ public class MongoSessionIdManager extends AbstractSessionIdManager
/* ------------------------------------------------------------ */
public MongoSessionIdManager(Server server, DBCollection sessions)
{
super(new Random());
super(server, new Random());
_server = server;
_sessions = sessions;
_sessions.ensureIndex(
@ -95,7 +93,6 @@ public class MongoSessionIdManager extends AbstractSessionIdManager
BasicDBObjectBuilder.start().add("sparse", false).add("background", true).get());
}
/* ------------------------------------------------------------ */

View File

@ -99,7 +99,7 @@ public class BalancerServletTest
if (nodeName != null)
{
AbstractSessionIdManager sessionIdManager = new HashSessionIdManager();
AbstractSessionIdManager sessionIdManager = new HashSessionIdManager(server);
sessionIdManager.setWorkerName(nodeName);
server.setSessionIdManager(sessionIdManager);
}

View File

@ -46,13 +46,15 @@ public abstract class AbstractSessionIdManager extends AbstractLifeCycle impleme
protected SessionScavenger _scavenger;
/* ------------------------------------------------------------ */
public AbstractSessionIdManager()
public AbstractSessionIdManager(Server server)
{
_server = server;
}
/* ------------------------------------------------------------ */
public AbstractSessionIdManager(Random random)
public AbstractSessionIdManager(Server server, Random random)
{
this(server);
_random=random;
}

View File

@ -232,13 +232,15 @@ public abstract class AbstractSessionStore extends AbstractLifeCycle implements
throw new IllegalArgumentException ("Put key="+id+" session="+(session==null?"null":session.getId()));
session.setSessionManager(_manager);
//if the session is new, the data has changed, or the cache is considered stale, write it to any backing store
if ((session.isNew() || session.getSessionData().isDirty() || isStale(session)) && _sessionDataStore != null)
{
session.willPassivate();
if (_sessionDataStore.isPassivating())
session.willPassivate();
_sessionDataStore.store(id, session.getSessionData());
session.didActivate();
if (_sessionDataStore.isPassivating())
session.didActivate();
}
doPutIfAbsent(id,session);

View File

@ -156,6 +156,15 @@ public class CachingSessionDataStore extends AbstractSessionDataStore
super.doStop();
}
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
*/
@Override
public boolean isPassivating()
{
return true;
}
}

View File

@ -37,7 +37,7 @@ public class ContextId
public static ContextId getContextId (String node, Context context)
{
return new ContextId(node, getContextPath(context), getVirtualHost(context));
return new ContextId((node==null?"":node), getContextPath(context), getVirtualHost(context));
}

View File

@ -221,7 +221,7 @@ public class FileSessionDataStore extends AbstractSessionDataStore
File file = null;
if (_storeDir != null)
{
file = new File(_storeDir, id);
file = new File(_storeDir, _contextId.toString()+"_"+id);
if (file.exists())
file.delete();
@ -274,6 +274,15 @@ public class FileSessionDataStore extends AbstractSessionDataStore
if (!_storeDir.exists())
_storeDir.mkdirs();
}
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
*/
@Override
public boolean isPassivating()
{
return true;
}
}

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.server.session;
import java.util.Set;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.ConcurrentHashSet;
/**
@ -30,6 +31,14 @@ import org.eclipse.jetty.util.ConcurrentHashSet;
*/
public class HashSessionIdManager extends AbstractSessionIdManager
{
/**
* @param server
*/
public HashSessionIdManager(Server server)
{
super(server);
}
private final Set<String> _ids = new ConcurrentHashSet<String>();

View File

@ -1020,6 +1020,19 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
{
_unloadables.clear();
}
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
*/
@Override
public boolean isPassivating()
{
return true;
}
}

View File

@ -144,14 +144,12 @@ public class JDBCSessionIdManager extends org.eclipse.jetty.server.session.Abstr
public JDBCSessionIdManager(Server server)
{
super();
_server=server;
super(server);
}
public JDBCSessionIdManager(Server server, Random random)
{
super(random);
_server=server;
super(server,random);
}
public SessionIdTableSchema getSessionIdTableSchema()

View File

@ -75,4 +75,14 @@ public class NullSessionDataStore extends AbstractSessionDataStore
return candidates; //whatever is suggested we accept
}
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()
*/
@Override
public boolean isPassivating()
{
return false;
}
}

View File

@ -93,4 +93,11 @@ public interface SessionDataStore extends LifeCycle
public Set<String> getExpired (Set<String> candidates);
/**
* True if this type of datastore will passivate session objects
* @return
*/
public boolean isPassivating ();
}

View File

@ -233,8 +233,6 @@ public class SessionManager extends ContainerLifeCycle implements org.eclipse.je
if (_sessionStore == null)
throw new IllegalStateException("No session store configured");
if (_sessionIdManager == null)
throw new IllegalStateException("No session id manager");
_context=ContextHandler.getCurrentContext();
_loader=Thread.currentThread().getContextClassLoader();
@ -255,7 +253,7 @@ public class SessionManager extends ContainerLifeCycle implements org.eclipse.je
try
{
Thread.currentThread().setContextClassLoader(serverLoader);
_sessionIdManager=new HashSessionIdManager();
_sessionIdManager=new HashSessionIdManager(server);
server.setSessionIdManager(_sessionIdManager);
server.manage(_sessionIdManager);
_sessionIdManager.start();
@ -302,10 +300,9 @@ public class SessionManager extends ContainerLifeCycle implements org.eclipse.je
tmp=_context.getInitParameter(org.eclipse.jetty.server.SessionManager.__CheckRemoteSessionEncoding);
if (tmp!=null)
_checkingRemoteSessionIdEncoding=Boolean.parseBoolean(tmp);
_contextId = ContextId.getContextId(_sessionIdManager.getWorkerName(), _context);
}
_contextId = ContextId.getContextId(_sessionIdManager.getWorkerName(), _context);
if (_sessionStore instanceof AbstractSessionStore)
((AbstractSessionStore)_sessionStore).setSessionManager(this);
@ -985,7 +982,7 @@ public class SessionManager extends ContainerLifeCycle implements org.eclipse.je
return; // couldn't get/load a session for this context with that id
}
_sessionTimeStats.set(round((System.currentTimeMillis() - session.getCreationTime())/1000.0));
_sessionTimeStats.set(round((System.currentTimeMillis() - session.getSessionData().getCreated())/1000.0));
session.invalidateAndRemove();
}
catch (Exception e)

View File

@ -466,9 +466,11 @@ public class ResponseTest
request.setRequestedSessionId("12345");
request.setRequestedSessionIdFromCookie(false);
HashSessionManager manager = new HashSessionManager();
manager.setSessionIdManager(new HashSessionIdManager());
manager.setSessionIdManager(new HashSessionIdManager(_server));
request.setSessionManager(manager);
request.setSession(new TestSession(manager, "12345"));
TestSession tsession = new TestSession(manager, "12345");
tsession.setExtendedId(manager.getSessionIdManager().getExtendedId("12345", null));
request.setSession(tsession);
manager.setCheckingRemoteSessionIdEncoding(false);
@ -541,7 +543,7 @@ public class ResponseTest
request.setRequestedSessionId("12345");
request.setRequestedSessionIdFromCookie(i>2);
HashSessionManager manager = new HashSessionManager();
manager.setSessionIdManager(new HashSessionIdManager());
manager.setSessionIdManager(new HashSessionIdManager(_server));
request.setSessionManager(manager);
request.setSession(new TestSession(manager, "12345"));
manager.setCheckingRemoteSessionIdEncoding(false);

View File

@ -60,7 +60,7 @@ public class FileSessionManagerTest
Server server = new Server();
SessionHandler handler = new SessionHandler();
handler.setServer(server);
final HashSessionIdManager idmgr = new HashSessionIdManager();
final HashSessionIdManager idmgr = new HashSessionIdManager(server);
idmgr.setServer(server);
server.setSessionIdManager(idmgr);
@ -92,7 +92,7 @@ public class FileSessionManagerTest
Server server = new Server();
SessionHandler handler = new SessionHandler();
handler.setServer(server);
final HashSessionIdManager idmgr = new HashSessionIdManager();
final HashSessionIdManager idmgr = new HashSessionIdManager(server);
idmgr.setServer(server);
server.setSessionIdManager(idmgr);
final FileSessionManager manager = new FileSessionManager();
@ -106,7 +106,7 @@ public class FileSessionManagerTest
manager.start();
//See SessionKey.getKey()
String expectedFilename = "_0.0.0.0_validFile123";
String expectedFilename = "__0.0.0.0_validFile123";
Assert.assertTrue(new File(testDir, expectedFilename).createNewFile());
@ -134,7 +134,7 @@ public class FileSessionManagerTest
Assert.assertTrue(testDir.canWrite());
handler.setSessionManager(manager);
AbstractSessionIdManager idManager = new HashSessionIdManager();
AbstractSessionIdManager idManager = new HashSessionIdManager(server);
idManager.setServer(server);
idManager.setWorkerName("foo");
manager.setSessionIdManager(idManager);
@ -153,10 +153,7 @@ public class FileSessionManagerTest
manager.setMaxInactiveInterval(30); // change max inactive interval for *new* sessions
manager.stop();
for (String f: testDir.list())
System.err.println(f);
String expectedFilename = "_0.0.0.0_"+session.getId();
String expectedFilename = "foo__0.0.0.0_"+session.getId();
Assert.assertTrue("File should exist!", new File(testDir, expectedFilename).exists());

View File

@ -30,6 +30,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.server.Server;
import org.junit.Test;
/**
@ -127,6 +128,14 @@ public class SessionCookieTest
public class MockSessionIdManager extends AbstractSessionIdManager
{
/**
* @param server
*/
public MockSessionIdManager(Server server)
{
super(server);
}
/**
* @see org.eclipse.jetty.server.SessionIdManager#isIdInUse(java.lang.String)
*/
@ -187,7 +196,8 @@ public class SessionCookieTest
@Test
public void testSecureSessionCookie () throws Exception
{
MockSessionIdManager idMgr = new MockSessionIdManager();
Server server = new Server();
MockSessionIdManager idMgr = new MockSessionIdManager(server);
idMgr.setWorkerName("node1");
MockSessionManager mgr = new MockSessionManager();
mgr.setSessionIdManager(idMgr);

View File

@ -64,7 +64,7 @@ public class FileTestServer extends AbstractTestServer
public SessionIdManager newSessionIdManager(Object config)
{
HashSessionIdManager mgr = new HashSessionIdManager();
HashSessionIdManager mgr = new HashSessionIdManager(_server);
mgr.setWorkerName("worker"+(__workers++));
return mgr;
}

View File

@ -41,7 +41,7 @@ public class HashTestServer extends AbstractTestServer
public SessionIdManager newSessionIdManager(Object config)
{
HashSessionIdManager mgr = new HashSessionIdManager();
HashSessionIdManager mgr = new HashSessionIdManager(_server);
mgr.setWorkerName("worker"+(__workers++));
return mgr;
}

View File

@ -88,7 +88,6 @@ public class InfinispanTestSupport
_tmpdir = File.createTempFile("infini", "span");
_tmpdir.delete();
_tmpdir.mkdir();
System.err.println("Temp file: "+_tmpdir);
Configuration config = _builder.persistence().addSingleFileStore().location(_tmpdir.getAbsolutePath()).storeAsBinary().build();
_manager.defineConfiguration(_name, config);
}

View File

@ -52,14 +52,5 @@ public class LastAccessTimeTest extends AbstractLastAccessTimeTest
{
super.testLastAccessTime();
}
@Override
public void assertAfterScavenge(SessionManager manager)
{
//The infinispan session manager will remove a session from its local memory that was a candidate to be scavenged if
//it checks with the cluster and discovers that another node is managing it, so the count is 0
assertSessionCounts(0, 1, 1, manager);
}
}

View File

@ -53,7 +53,7 @@ public class ReloadedSessionMissingClassTest
@Test
public void testSessionReloadWithMissingClass() throws Exception
{
((StdErrLog)Log.getLogger(org.eclipse.jetty.server.session.JDBCSessionManager.class)).setHideStacks(true);
((StdErrLog)Log.getLogger("org.eclipse.jetty.server.session")).setHideStacks(true);
Resource.setDefaultUseCaches(false);
String contextPath = "/foo";

View File

@ -36,6 +36,8 @@ import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.eclipse.jetty.server.session.Session;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
@ -48,7 +50,19 @@ import org.junit.Test;
public class AttributeNameTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
throws Exception

View File

@ -20,10 +20,26 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractClientCrossContextSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ClientCrossContextSessionTest extends AbstractClientCrossContextSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port)
{
return new MongoTestServer(port);

View File

@ -21,6 +21,8 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractForwardedSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
@ -30,7 +32,21 @@ import org.junit.Test;
*/
public class ForwardedSessionTest extends AbstractForwardedSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
/**
* @see org.eclipse.jetty.server.session.AbstractForwardedSessionTest#createServer(int)
*/

View File

@ -21,11 +21,27 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractInvalidationSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class InvalidateSessionTest extends AbstractInvalidationSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
@Override
public AbstractTestServer createServer(int port)
{

View File

@ -20,19 +20,40 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractLastAccessTimeTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class LastAccessTimeTest extends AbstractLastAccessTimeTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new MongoTestServer(port,max,scavenge);
}
@Test
public void testLastAccessTime() throws Exception
{
super.testLastAccessTime();
}
}

View File

@ -20,10 +20,26 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractLocalSessionScavengingTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class LocalSessionScavengingTest extends AbstractLocalSessionScavengingTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.nosql.mongodb;
import java.net.UnknownHostException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.SessionIdManager;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.server.session.AbstractSessionStore;
@ -28,9 +27,8 @@ import org.eclipse.jetty.server.session.AbstractTestServer;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.server.session.StalePeriodStrategy;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
@ -44,27 +42,25 @@ public class MongoTestServer extends AbstractTestServer
public static class TestMongoSessionIdManager extends MongoSessionIdManager
public static void dropCollection () throws MongoException, UnknownHostException
{
public TestMongoSessionIdManager(Server server) throws UnknownHostException, MongoException
{
super(server);
}
public void deleteAll ()
{
DBCursor checkSessions = _sessions.find();
for (DBObject session : checkSessions)
{
_sessions.remove(session);
}
}
new Mongo().getDB("HttpSessions").getCollection("testsessions").drop();
}
public static void createCollection() throws UnknownHostException, MongoException
{
new Mongo().getDB("HttpSessions").createCollection("testsessions", null);
}
public static DBCollection getCollection () throws UnknownHostException, MongoException
{
return new Mongo().getDB("HttpSessions").getCollection("testsessions");
}
public MongoTestServer(int port)
{
super(port, 30, 10);
@ -85,8 +81,7 @@ public class MongoTestServer extends AbstractTestServer
{
try
{
System.err.println("MongoTestServer:SessionIdManager scavenge: delay:"+ _scavengePeriod + " period:"+_scavengePeriod);
MongoSessionIdManager idManager = new TestMongoSessionIdManager(_server);
MongoSessionIdManager idManager = new MongoSessionIdManager(_server, getCollection());
idManager.setWorkerName("w"+(__workers++));
return idManager;

View File

@ -20,6 +20,8 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractNewSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
@ -27,6 +29,20 @@ import org.junit.Test;
*/
public class NewSessionTest extends AbstractNewSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
{

View File

@ -20,6 +20,8 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractOrphanedSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
@ -27,6 +29,21 @@ import org.junit.Test;
*/
public class OrphanedSessionTest extends AbstractOrphanedSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new MongoTestServer(port,max,scavenge);

View File

@ -36,7 +36,6 @@ import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.Ignore;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
@ -127,7 +126,7 @@ public class PurgeInvalidSessionTest
}
@Test
@Ignore
public void testPurgeInvalidSessionsWithLimit() throws Exception
{
String contextPath = "";

View File

@ -34,10 +34,8 @@ 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.nosql.mongodb.MongoTestServer.TestMongoSessionIdManager;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.Ignore;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
@ -125,7 +123,7 @@ public class PurgeValidSessionTest
}
@Test
@Ignore
public void testPurgeValidSessionWithPurgeLimitSet() throws Exception
{
String contextPath = "";
@ -153,7 +151,7 @@ public class PurgeValidSessionTest
try
{
// start with no sessions
((TestMongoSessionIdManager)idManager).deleteAll();
//((TestMongoSessionIdManager)idManager).deleteAll();
HttpClient client = new HttpClient();
client.start();

View File

@ -20,6 +20,8 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractReentrantRequestSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
@ -27,6 +29,21 @@ import org.junit.Test;
*/
public class ReentrantRequestSessionTest extends AbstractReentrantRequestSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port)
{
return new MongoTestServer(port);

View File

@ -20,11 +20,27 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractRemoveSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class RemoveSessionTest extends AbstractRemoveSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new MongoTestServer(port,max,scavenge);

View File

@ -20,6 +20,8 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractScatterGunLoadTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
@ -27,6 +29,20 @@ import org.junit.Test;
*/
public class ScatterGunLoadTest extends AbstractScatterGunLoadTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port)
{

View File

@ -20,11 +20,28 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractServerCrossContextSessionTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ServerCrossContextSessionTest extends AbstractServerCrossContextSessionTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port)
{
return new MongoTestServer(port);

View File

@ -20,11 +20,27 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractSessionExpiryTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SessionExpiryTest extends AbstractSessionExpiryTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{

View File

@ -20,11 +20,27 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractSessionInvalidateAndCreateTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SessionInvalidateAndCreateTest extends AbstractSessionInvalidateAndCreateTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{

View File

@ -20,11 +20,27 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractSessionMigrationTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SessionMigrationTest extends AbstractSessionMigrationTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
@Override
public AbstractTestServer createServer(int port)
{

View File

@ -20,11 +20,27 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractSessionRenewTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SessionRenewTest extends AbstractSessionRenewTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{

View File

@ -20,11 +20,27 @@ package org.eclipse.jetty.nosql.mongodb;
import org.eclipse.jetty.server.session.AbstractSessionValueSavingTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class SessionSavingValueTest extends AbstractSessionValueSavingTest
{
@BeforeClass
public static void beforeClass() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@AfterClass
public static void afterClass() throws Exception
{
MongoTestServer.dropCollection();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{

View File

@ -1,163 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2015 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 java.net.UnknownHostException;
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.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Ignore;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class StopSessionManagerDeleteSessionTest
{
public MongoTestServer createServer(int port, int max, int scavenge)
{
MongoTestServer server = new MongoTestServer(port,max,scavenge);
return server;
}
/**
* @throws Exception
*/
@Ignore
public void testStopSessionManagerDeleteSession() throws Exception
{
String contextPath = "";
String servletMapping = "/server";
MongoTestServer server = createServer(0, 1, 0);
ServletContextHandler context = server.addContext(contextPath);
ServletHolder holder = new ServletHolder();
TestServlet servlet = new TestServlet();
holder.setServlet(servlet);
context.addServlet(holder, servletMapping);
MongoSessionManager sessionManager = (MongoSessionManager)context.getSessionHandler().getSessionManager();
MongoSessionIdManager idManager = (MongoSessionIdManager)server.getServer().getSessionIdManager();
server.start();
int port=server.getPort();
try
{
HttpClient client = new HttpClient();
client.start();
try
{
//Create a session
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//stop the session manager
sessionManager.stop();
//check the database to see that the session has been marked invalid
servlet.checkSessionInDB(false);
}
finally
{
client.stop();
}
}
finally
{
server.stop();
}
}
public static class TestServlet extends HttpServlet
{
DBCollection _sessions;
String _id;
public TestServlet() throws UnknownHostException, MongoException
{
super();
_sessions = new Mongo().getDB("HttpSessions").getCollection("sessions");
}
public void checkSessionInDB (boolean expectedValid)
{
DBObject dbSession = _sessions.findOne(new BasicDBObject("id", _id));
assertTrue(dbSession != null);
assertEquals(expectedValid, dbSession.get("valid"));
/* if (!expectedValid)
assertNotNull(dbSession.get(MongoSessionDataStore.__INVALIDATED));*/
}
public String getId()
{
return _id;
}
@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);
session.setAttribute("foo", "bar");
assertTrue(session.isNew());
_id = session.getId();
}
else if ("test".equals(action))
{
String id = request.getRequestedSessionId();
assertNotNull(id);
id = id.substring(0, id.indexOf("."));
HttpSession existingSession = request.getSession(false);
assertTrue(existingSession == null);
//not in db any more
DBObject dbSession = _sessions.findOne(new BasicDBObject("id", id));
assertTrue(dbSession == null);
}
}
}
}

View File

@ -1,98 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2015 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.assertTrue;
import java.net.UnknownHostException;
import org.eclipse.jetty.server.session.AbstractStopSessionManagerPreserveSessionTest;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* StopSessionManagerPreserveSessionTest
*
*
*/
public class StopSessionManagerPreserveSessionTest extends AbstractStopSessionManagerPreserveSessionTest
{
DBCollection _sessions;
@Before
public void setUp() throws UnknownHostException, MongoException
{
_sessions = new Mongo().getDB("HttpSessions").getCollection("sessions");
}
public MongoTestServer createServer(int port)
{
MongoTestServer server = new MongoTestServer(port);
server.getServer().setStopTimeout(0);
return server;
}
@Override
public void checkSessionPersisted(boolean expected)
{
DBObject dbSession = _sessions.findOne(new BasicDBObject("id", _id));
if (expected)
{
assertTrue(dbSession != null);
assertEquals(expected, dbSession.get("valid"));
}
else
{
assertTrue(dbSession==null);
}
}
@Override
public void configureSessionManagement(ServletContextHandler context)
{
}
/**
* @throws Exception
*/
@Test
public void testStopSessionManagerPreserveSession() throws Exception
{
super.testStopSessionManagerPreserveSession();
}
}

View File

@ -85,7 +85,7 @@ public abstract class AbstractInvalidationSessionTest
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
// Be sure the session is also present in node2
Request request2 = client.newRequest(urls[1] + "?action=increment");
request2.header("Cookie", sessionCookie);

View File

@ -24,6 +24,8 @@ 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;
@ -51,7 +53,10 @@ import org.junit.Test;
*/
public abstract class AbstractLastAccessTimeTest
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge);
@Test
public void testLastAccessTime() throws Exception
@ -120,12 +125,13 @@ public abstract class AbstractLastAccessTimeTest
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(scavengePeriod * 2500L);
//check that the session was not scavenged over on server1 by ensuring that the SessionListener destroy method wasn't called
assertFalse(listener1.destroyed);
assertFalse(listener1._destroys.contains(AbstractTestServer.extractSessionId(sessionCookie)));
assertAfterScavenge(m1);
}
finally
@ -163,19 +169,19 @@ public abstract class AbstractLastAccessTimeTest
public static class TestSessionListener implements HttpSessionListener
{
public boolean destroyed = false;
public boolean created = false;
public Set<String> _creates = new HashSet<String>();
public Set<String> _destroys = new HashSet<String>();
@Override
public void sessionDestroyed(HttpSessionEvent se)
{
destroyed = true;
_destroys.add(se.getSession().getId());
}
@Override
public void sessionCreated(HttpSessionEvent se)
{
created = true;
_creates.add(se.getSession().getId());
}
}
@ -183,7 +189,10 @@ public abstract class AbstractLastAccessTimeTest
public static class TestServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
@ -212,14 +221,14 @@ public abstract class AbstractLastAccessTimeTest
private void sendResult(HttpSession session, PrintWriter writer)
{
if (session != null)
{
writer.print(session.getAttribute("test"));
}
else
{
writer.print("null");
}
if (session != null)
{
writer.print(session.getAttribute("test"));
}
else
{
writer.print("null");
}
}
}
}