Refactor session tests to common base; add option to save session when created.

This commit is contained in:
Jan Bartel 2016-05-20 15:46:38 +10:00
parent b53f2eca7f
commit 8dfcfd021c
79 changed files with 867 additions and 390 deletions

View File

@ -201,6 +201,8 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore
@Override
public void doStore(String id, SessionData data, long lastSaveTime) throws Exception
{
try
{
//Put an idle timeout on the cache entry if the session is not immortal -
//if no requests arrive at any node before this timeout occurs, or no node
//scavenges the session before this timeout occurs, the session will be removed.
@ -212,7 +214,10 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore
if (LOG.isDebugEnabled())
LOG.debug("Session {} saved to infinispan, expires {} ", id, data.getExpiry());
} catch (Exception e)
{
e.printStackTrace();
}
}

View File

@ -76,6 +76,11 @@ public abstract class AbstractSessionCache extends ContainerLifeCycle implements
*/
protected int _evictionPolicy = SessionCache.NEVER_EVICT;
/**
* If true, as soon as a new session is created, it will be persisted to the SessionDataStore
*/
protected boolean _saveOnCreate = false;
/**
* If true, a session that will be evicted from the cache because it has been
@ -262,6 +267,20 @@ public abstract class AbstractSessionCache extends ContainerLifeCycle implements
}
@Override
public boolean isSaveOnCreate()
{
return _saveOnCreate;
}
@Override
public void setSaveOnCreate(boolean saveOnCreate)
{
_saveOnCreate = saveOnCreate;
}
/**
* Get a session object.
*
@ -402,7 +421,8 @@ public abstract class AbstractSessionCache extends ContainerLifeCycle implements
if (data == null) //session doesn't exist
return null;
data.setLastNode(_context.getWorkerName());//we are going to manage the node
session = newSession(data);
return session;
}
@ -537,6 +557,19 @@ public abstract class AbstractSessionCache extends ContainerLifeCycle implements
//not there, so find out if session data exists for it
return _sessionDataStore.exists (id);
}
/**
* Check to see if this cache contains an entry for the session
* corresponding to the session id.
*
* @see org.eclipse.jetty.server.session.SessionCache#contains(java.lang.String)
*/
@Override
public boolean contains (String id) throws Exception
{
//just ask our object cache, not the store
return (doGet(id) != null);
}
/**
@ -700,8 +733,8 @@ public abstract class AbstractSessionCache extends ContainerLifeCycle implements
{
return _saveOnInactiveEviction;
}
/**
* @see org.eclipse.jetty.server.session.SessionCache#newSession(javax.servlet.http.HttpServletRequest, java.lang.String, long, long)
*/
@ -710,6 +743,16 @@ public abstract class AbstractSessionCache extends ContainerLifeCycle implements
{
if (LOG.isDebugEnabled()) LOG.debug("Creating new session id="+id);
Session session = newSession(request, _sessionDataStore.newSessionData(id, time, time, time, maxInactiveMs));
session.getSessionData().setLastNode(_context.getWorkerName());
try
{
if (isSaveOnCreate() && _sessionDataStore != null)
_sessionDataStore.store(id, session.getSessionData());
}
catch (Exception e)
{
LOG.warn("Save of new session {} failed", id, e);
}
return session;
}
}

View File

@ -242,6 +242,7 @@ public class FileSessionDataStore extends AbstractSessionDataStore
}
catch (Exception e)
{
e.printStackTrace();
if (file != null)
file.delete(); // No point keeping the file if we didn't save the whole session
throw new UnwriteableSessionDataException(id, _context,e);

View File

@ -66,6 +66,7 @@ public interface SessionCache extends LifeCycle
Session renewSessionId (String oldId, String newId) throws Exception;
Session get(String id) throws Exception;
void put(String id, Session session) throws Exception;
boolean contains (String id) throws Exception;
boolean exists (String id) throws Exception;
Session delete (String id) throws Exception;
void shutdown ();
@ -77,4 +78,6 @@ public interface SessionCache extends LifeCycle
int getEvictionPolicy ();
void setSaveOnInactiveEviction (boolean saveOnEvict);
boolean isSaveOnInactiveEviction ();
void setSaveOnCreate(boolean saveOnCreate);
boolean isSaveOnCreate();
}

View File

@ -954,7 +954,7 @@ public class SessionHandler extends ScopedHandler
}
session.setExtendedId(_sessionIdManager.getExtendedId(id, null));
session.getSessionData().setLastNode(_sessionIdManager.getWorkerName()); //TODO write through the change of node?
//session.getSessionData().setLastNode(_sessionIdManager.getWorkerName()); //TODO write through the change of node?
}
return session;
}

View File

@ -40,11 +40,15 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(final int port, final int max, final int scavenge, final int evictionPolicy)
{
return new FileTestServer(port);
return new FileTestServer(port,max,scavenge, evictionPolicy);
}
@Test
public void testCrossContextDispatch() throws Exception
{

View File

@ -120,10 +120,7 @@ public class FileTestServer extends AbstractTestServer
}
public FileTestServer(int port)
{
super(port, 30, 10,2);
}
public FileTestServer(int port, int maxInactivePeriod, int scavengePeriod, int idlePassivatePeriod)
{

View File

@ -42,11 +42,12 @@ public class ForwardedSessionTest extends AbstractForwardedSessionTest
{
FileTestServer.teardown();
}
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(final int port, final int max, final int scavenge, final int evictionPolicy)
{
return new FileTestServer(port);
return new FileTestServer(port,max,scavenge, evictionPolicy);
}

View File

@ -0,0 +1,58 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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 org.junit.After;
import org.junit.Before;
/**
* ImmediateSaveTest
*
*
*/
public class ImmediateSaveTest extends AbstractImmediateSaveTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{ return new FileTestServer(port, max, scavenge, evictionPolicy)
{
public SessionHandler newSessionHandler()
{
SessionHandler h = super.newSessionHandler();
h.getSessionCache().setSaveOnCreate(true);
return h;
}
};
}
}

View File

@ -41,11 +41,15 @@ public class ScatterGunLoadTest extends AbstractScatterGunLoadTest
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(final int port, final int max, final int scavenge, final int evictionPolicy)
{
return new FileTestServer(port);
return new FileTestServer(port,max,scavenge, evictionPolicy);
}
@Test
public void testLightLoad() throws Exception
{

View File

@ -38,11 +38,15 @@ public class ServerCrossContextSessionTest extends AbstractServerCrossContextSes
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(final int port, final int max, final int scavenge, final int evictionPolicy)
{
return new FileTestServer(port);
return new FileTestServer(port,max,scavenge, evictionPolicy);
}
@Test
public void testCrossContextDispatch() throws Exception
{

View File

@ -47,13 +47,10 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
_testSupport.tearDown();
}
/**
* @see org.eclipse.jetty.server.session.AbstractClientCrossContextSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavengeMs,int evictionPolicy)
{
return new GCloudTestServer(port, _testSupport.getConfiguration());
return new GCloudTestServer(port, maxInactiveMs, scavengeMs, evictionPolicy, _testSupport.getConfiguration());
}
@Test

View File

@ -46,13 +46,10 @@ public class ForwardedSessionTest extends AbstractForwardedSessionTest
_testSupport.tearDown();
}
/**
* @see org.eclipse.jetty.server.session.AbstractForwardedSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
{
return new GCloudTestServer(port, _testSupport.getConfiguration());
}
@Override
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavengeMs,int evictionPolicy)
{
return new GCloudTestServer(port, maxInactiveMs, scavengeMs, evictionPolicy, _testSupport.getConfiguration());
}
}

View File

@ -345,7 +345,7 @@ public class GCloudSessionTestSupport
int actual = 0;
while (results.hasNext())
{
results.next();
ProjectionEntity e = results.next();
++actual;
}
assertEquals(count, actual);

View File

@ -30,11 +30,6 @@ import org.eclipse.jetty.server.session.SessionHandler;
*/
public class GCloudTestServer extends AbstractTestServer
{
static protected int __maxInactivePeriod = 30;
static protected int __scavengePeriod = 10;
static protected int __idlePeriod = 2;
/**
* @param port
@ -42,19 +37,11 @@ public class GCloudTestServer extends AbstractTestServer
* @param scavengePeriod
* @param sessionIdMgrConfig
*/
public GCloudTestServer(int port, int maxInactivePeriod, int scavengePeriod, int idlePassivatePeriod, GCloudConfiguration config)
public GCloudTestServer(int port, int maxInactivePeriod, int scavengePeriod, int evictionPolicy, GCloudConfiguration config)
{
super(port, maxInactivePeriod, scavengePeriod, idlePassivatePeriod, config);
super(port, maxInactivePeriod, scavengePeriod, evictionPolicy, config);
}
/**
* @param port
* @param configuration
*/
public GCloudTestServer(int port, GCloudConfiguration configuration)
{
super(port, 30,10, __idlePeriod, configuration);
}
/**

View File

@ -0,0 +1,76 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.gcloud.session;
import org.eclipse.jetty.server.session.AbstractImmediateSaveTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.eclipse.jetty.server.session.SessionHandler;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
/**
* ImmediateSaveTest
*
*
*/
public class ImmediateSaveTest extends AbstractImmediateSaveTest
{
static GCloudSessionTestSupport _testSupport;
@BeforeClass
public static void setup () throws Exception
{
_testSupport = new GCloudSessionTestSupport();
_testSupport.setUp();
}
@After
public void deleteSessions () throws Exception
{
_testSupport.deleteSessions();
}
@AfterClass
public static void teardown () throws Exception
{
_testSupport.tearDown();
}
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
return new GCloudTestServer(port, max, scavenge, evictionPolicy,_testSupport.getConfiguration())
{
public SessionHandler newSessionHandler()
{
SessionHandler h = super.newSessionHandler();
h.getSessionCache().setSaveOnCreate(true);
return h;
}
};
}
}

View File

@ -35,7 +35,7 @@ import org.junit.Ignore;
public class InvalidationSessionTest extends AbstractInvalidationSessionTest
{
static GCloudSessionTestSupport _testSupport;
public static final int EVICT_SEC = 3;
@BeforeClass
public static void setup () throws Exception
@ -65,7 +65,7 @@ public class InvalidationSessionTest extends AbstractInvalidationSessionTest
public SessionHandler newSessionHandler()
{
SessionHandler handler = super.newSessionHandler();
handler.getSessionCache().setEvictionPolicy(EVICT_SEC);
handler.getSessionCache().setEvictionPolicy(evictionPolicy);
return handler;
}
@ -73,37 +73,4 @@ public class InvalidationSessionTest extends AbstractInvalidationSessionTest
return server;
}
/**
* @see org.eclipse.jetty.server.session.AbstractInvalidationSessionTest#pause()
*/
@Override
public void pause()
{
//This test moves around a session between 2 nodes. After it is invalidated on the 1st node,
//it will still be in the memory of the 2nd node. We need to wait until after the stale time
//has expired on node2 for it to reload the session and discover it has been deleted.
try
{
Thread.currentThread().sleep((2*EVICT_SEC)*1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* @see org.eclipse.jetty.server.session.AbstractInvalidationSessionTest#testInvalidation()
*/
@Ignore
@Override
public void testInvalidation() throws Exception
{
// Ignore
//super.testInvalidation();
}
}

View File

@ -47,13 +47,11 @@ public class ServerCrossContextSessionTest extends AbstractServerCrossContextSes
{
_testSupport.tearDown();
}
/**
* @see org.eclipse.jetty.server.session.AbstractServerCrossContextSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavengeMs,int evictionPolicy)
{
return new GCloudTestServer(port, _testSupport.getConfiguration());
return new GCloudTestServer(port, maxInactiveMs, scavengeMs, evictionPolicy, _testSupport.getConfiguration());
}
@Test

View File

@ -47,15 +47,12 @@ public class SessionMigrationTest extends AbstractSessionMigrationTest
_testSupport.tearDown();
}
/**
* @see org.eclipse.jetty.server.session.AbstractSessionMigrationTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
{
return new GCloudTestServer(port, _testSupport.getConfiguration());
}
@Override
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavengeMs,int evictionPolicy)
{
return new GCloudTestServer(port, maxInactiveMs, scavengeMs, evictionPolicy, _testSupport.getConfiguration());
}
@Test
@Override

View File

@ -53,7 +53,7 @@ public class StopSessionManagerPreserveSessionTest extends AbstractStopSessionMa
* @see org.eclipse.jetty.server.session.AbstractStopSessionManagerPreserveSessionTest#checkSessionPersisted(boolean)
*/
@Override
public void checkSessionPersisted(boolean expected)
public void checkSessionPersisted(String id, boolean expected)
{
try
{
@ -66,15 +66,12 @@ public class StopSessionManagerPreserveSessionTest extends AbstractStopSessionMa
}
/**
* @see org.eclipse.jetty.server.session.AbstractStopSessionManagerPreserveSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
{
return new GCloudTestServer(port, _testSupport.getConfiguration());
}
@Override
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavengeMs,int evictionPolicy)
{
return new GCloudTestServer(port, maxInactiveMs, scavengeMs, evictionPolicy, _testSupport.getConfiguration());
}
/**
* @see org.eclipse.jetty.server.session.AbstractStopSessionManagerPreserveSessionTest#configureSessionManagement(org.eclipse.jetty.servlet.ServletContextHandler)
*/

View File

@ -35,13 +35,10 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
super.testCrossContextDispatch();
}
/**
* @see org.eclipse.jetty.server.session.AbstractClientCrossContextSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
{
return new HashTestServer(port);
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
return new HashTestServer(port,max,scavenge,evictionPolicy);
}
}

View File

@ -18,20 +18,12 @@
package org.eclipse.jetty.server.session;
import org.eclipse.jetty.server.SessionIdManager;
/**
* @version $Revision$ $Date$
*/
public class HashTestServer extends AbstractTestServer
{
static int __workers=0;
public HashTestServer(int port)
{
super(port, 30, 10, 2);
}
public HashTestServer(int port, int maxInactivePeriod, int scavengePeriod, int evictionPolicy)
{

View File

@ -26,11 +26,11 @@ import org.junit.Test;
public class ScatterGunLoadTest extends AbstractScatterGunLoadTest
{
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
return new HashTestServer(port);
return new HashTestServer(port,max,scavenge,evictionPolicy);
}
@Test
public void testLightLoad() throws Exception
{

View File

@ -22,11 +22,11 @@ import org.junit.Test;
public class ServerCrossContextSessionTest extends AbstractServerCrossContextSessionTest
{
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
return new HashTestServer(port);
return new HashTestServer(port,max,scavenge,evictionPolicy);
}
@Test
public void testCrossContextDispatch() throws Exception
{

View File

@ -44,11 +44,11 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
}
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavenge, int evictionPolicy)
{
InfinispanTestSessionServer server = new InfinispanTestSessionServer(port, __testSupport.getCache());
return server;
return new InfinispanTestSessionServer(port, maxInactiveMs, scavenge, evictionPolicy, __testSupport.getCache());
}
@Test

View File

@ -49,11 +49,11 @@ public class ForwardedSessionTest extends AbstractForwardedSessionTest
}
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavenge, int evictionPolicy)
{
InfinispanTestSessionServer server = new InfinispanTestSessionServer(port, __testSupport.getCache());
return server;
return new InfinispanTestSessionServer(port, maxInactiveMs, scavenge, evictionPolicy, __testSupport.getCache());
}
@Test

View File

@ -0,0 +1,64 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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 org.junit.AfterClass;
import org.junit.BeforeClass;
/**
* RemoteImmediateSaveTest
*
*
*/
public class ImmediateSaveTest extends AbstractImmediateSaveTest
{
InfinispanTestSessionServer _server;
public static InfinispanTestSupport __testSupport;
@BeforeClass
public static void setup () throws Exception
{
__testSupport = new InfinispanTestSupport();
__testSupport.setup();
}
@AfterClass
public static void teardown () throws Exception
{
__testSupport.teardown();
}
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
_server = new InfinispanTestSessionServer(port, max, scavenge, evictionPolicy, __testSupport.getCache())
{
public SessionHandler newSessionHandler()
{
SessionHandler h = super.newSessionHandler();
h.getSessionCache().setSaveOnCreate(true);
return h;
}
};
return _server;
}
}

View File

@ -29,12 +29,6 @@ import org.infinispan.commons.util.CloseableIteratorSet;
public class InfinispanTestSessionServer extends AbstractTestServer
{
public InfinispanTestSessionServer(int port, BasicCache config)
{
this(port, 30, 10, 2, config);
}
public InfinispanTestSessionServer(int port, int maxInactivePeriod, int scavengePeriod, int evictionPolicy, BasicCache config)

View File

@ -46,13 +46,11 @@ public class SessionMigrationTest extends AbstractSessionMigrationTest
__testSupport.teardown();
}
/**
* @see org.eclipse.jetty.server.session.AbstractSessionMigrationTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavenge, int evictionPolicy)
{
return new InfinispanTestSessionServer(port, __testSupport.getCache());
return new InfinispanTestSessionServer(port, maxInactiveMs, scavenge, evictionPolicy, __testSupport.getCache());
}
@Override

View File

@ -53,11 +53,11 @@ public class RemoteClientCrossContextSessionTest extends AbstractClientCrossCont
}
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavenge, int evictionPolicy)
{
InfinispanTestSessionServer server = new InfinispanTestSessionServer(port, __testSupport.getCache());
return server;
return new InfinispanTestSessionServer(port, maxInactiveMs, scavenge, evictionPolicy, __testSupport.getCache());
}
@Test

View File

@ -53,10 +53,9 @@ public class RemoteForwardedSessionTest extends AbstractForwardedSessionTest
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavenge, int evictionPolicy)
{
InfinispanTestSessionServer server = new InfinispanTestSessionServer(port, __testSupport.getCache());
return server;
return new InfinispanTestSessionServer(port, maxInactiveMs, scavenge, evictionPolicy, __testSupport.getCache());
}
@Test

View File

@ -0,0 +1,66 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.remote;
import org.eclipse.jetty.server.session.AbstractImmediateSaveTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.eclipse.jetty.server.session.InfinispanTestSessionServer;
import org.eclipse.jetty.server.session.SessionHandler;
import org.junit.AfterClass;
import org.junit.BeforeClass;
/**
* RemoteImmediateSaveTest
*
*
*/
public class RemoteImmediateSaveTest extends AbstractImmediateSaveTest
{
public static RemoteInfinispanTestSupport __testSupport;
@BeforeClass
public static void setup () throws Exception
{
__testSupport = new RemoteInfinispanTestSupport("remote-session-test");
__testSupport.setup();
}
@AfterClass
public static void teardown () throws Exception
{
__testSupport.teardown();
}
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
return new InfinispanTestSessionServer(port, max, scavenge, evictionPolicy, __testSupport.getCache())
{
public SessionHandler newSessionHandler()
{
SessionHandler h = super.newSessionHandler();
h.getSessionCache().setSaveOnCreate(true);
return h;
}
};
}
}

View File

@ -35,7 +35,7 @@ public class RemoteInvalidationSessionTest extends AbstractInvalidationSessionTe
{
public static RemoteInfinispanTestSupport __testSupport;
public static final int IDLE_PASSIVATE_SEC = 3;
@ -61,35 +61,6 @@ public class RemoteInvalidationSessionTest extends AbstractInvalidationSessionTe
return new InfinispanTestSessionServer(port, maxInterval, scavengeInterval, evictionPolicy, __testSupport.getCache());
}
@Ignore
@Override
public void testInvalidation() throws Exception
{
//Ignore
//super.testInvalidation();
}
/**
* @see org.eclipse.jetty.server.session.AbstractInvalidationSessionTest#pause()
*/
@Override
public void pause()
{
//This test moves a session from node 1 to node 2, then invalidates the session back on node1. This
//should never happen with a decent load balancer.
//The infinispan session manager on node 2 will hold the session in local memory for a specific (configurable)
//amount of time. We've set the stale session time to 3 sec, so we need to pause for at least this long before making
//another request to node2 so
//that the node will re-load the session from the database and discover that it has gone.
try
{
Thread.sleep(2 * IDLE_PASSIVATE_SEC * 1000);
}
catch (InterruptedException e)
{
}
}
}

View File

@ -49,15 +49,12 @@ public class RemoteSessionMigrationTest extends AbstractSessionMigrationTest
__testSupport.teardown();
}
/**
* @see org.eclipse.jetty.server.session.AbstractSessionMigrationTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
{
return new InfinispanTestSessionServer(port, __testSupport.getCache());
}
@Override
public AbstractTestServer createServer(int port, int maxInactiveMs, int scavenge, int evictionPolicy)
{
return new InfinispanTestSessionServer(port, maxInactiveMs, scavenge, evictionPolicy, __testSupport.getCache());
}
@Override
public void testSessionMigration() throws Exception
{

View File

@ -29,9 +29,11 @@ import org.junit.Test;
public class ClientCrossContextSessionTest extends AbstractClientCrossContextSessionTest
{
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new JdbcTestServer(port);
return new JdbcTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test

View File

@ -30,13 +30,10 @@ import org.junit.Test;
public class ForwardedSessionTest extends AbstractForwardedSessionTest
{
/**
* @see org.eclipse.jetty.server.session.AbstractForwardedSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new JdbcTestServer(port);
return new JdbcTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test

View File

@ -0,0 +1,55 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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 org.junit.After;
import org.junit.AfterClass;
/**
* ImmediateSaveTest
*
*
*/
public class ImmediateSaveTest extends AbstractImmediateSaveTest
{
JdbcTestServer _server;
@AfterClass
public static void tearDown() throws Exception
{
JdbcTestServer.shutdown(null);
}
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
_server = new JdbcTestServer(port, max, scavenge, evictionPolicy)
{
public SessionHandler newSessionHandler()
{
SessionHandler h = super.newSessionHandler();
h.getSessionCache().setSaveOnCreate(true);
return h;
}
};
return _server;
}
}

View File

@ -27,28 +27,12 @@ import org.junit.Test;
*/
public class InvalidationSessionTest extends AbstractInvalidationSessionTest
{
public static final int IDLE_PASSIVATE_SEC = 3;
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new JdbcTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
public void pause()
{
//This test moves around a session between 2 nodes. Due to optimizations in the handling of
//the sessions for the JDBC SessionManager, this can mean that a session that may have been
//deleted on one node is then accessed again shortly afterwards, it can appear as if the
//session is still live in the memory of that node. By waiting a little time, we can ensure
//that the node will re-load the session from the database and discover that it has gone.
try
{
Thread.sleep(2 * IDLE_PASSIVATE_SEC * 1000);
}
catch (InterruptedException e)
{
}
}
@Test
public void testInvalidation() throws Exception

View File

@ -78,12 +78,7 @@ public class JdbcTestServer extends AbstractTestServer
}
}
public JdbcTestServer(int port)
{
super(port);
}
public JdbcTestServer(int port, int maxInactivePeriod, int scavengePeriod, int idlePassivatePeriod, String connectionUrl)
{
super(port, maxInactivePeriod, scavengePeriod, idlePassivatePeriod, connectionUrl);

View File

@ -89,7 +89,8 @@ public class ReloadedSessionMissingClassTest
URLClassLoader loaderWithoutFoo = new URLClassLoader(barUrls, Thread.currentThread().getContextClassLoader());
AbstractTestServer server1 = new JdbcTestServer(0);
AbstractTestServer server1 = new JdbcTestServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
WebAppContext webApp = server1.addWebAppContext(unpackedWarDir.getCanonicalPath(), contextPath);
webApp.setClassLoader(loaderWithFoo);
webApp.addServlet("Bar", "/bar");

View File

@ -27,11 +27,11 @@ import org.junit.Test;
public class ServerCrossContextSessionTest extends AbstractServerCrossContextSessionTest
{
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new JdbcTestServer(port);
return new JdbcTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test
public void testCrossContextDispatch() throws Exception
{

View File

@ -27,11 +27,11 @@ import org.junit.Test;
public class SessionMigrationTest extends AbstractSessionMigrationTest
{
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new JdbcTestServer(port);
return new JdbcTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test
public void testSessionMigration() throws Exception
{

View File

@ -37,23 +37,24 @@ public class StopSessionManagerPreserveSessionTest extends AbstractStopSessionMa
}
@Override
public void checkSessionPersisted(boolean expected)
public void checkSessionPersisted(String id, boolean expected)
{
try
{
boolean actual = _server.existsInSessionTable(_id, true);
boolean actual = _server.existsInSessionTable(id, true);
assertEquals(expected, actual);
}
catch (Exception e)
{
e.printStackTrace();
fail(e.getMessage());
}
}
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
_server = new JdbcTestServer(0);
_server = new JdbcTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
return _server;
}

View File

@ -29,13 +29,12 @@ import org.junit.Test;
*/
public class WebAppObjectInSessionTest extends AbstractWebAppObjectInSessionTest
{
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
Resource.setDefaultUseCaches(false);
return new JdbcTestServer(port);
return new JdbcTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test
public void testWebappObjectInSession() throws Exception
{

View File

@ -40,11 +40,14 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new MongoTestServer(port);
return new MongoTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test
public void testCrossContextDispatch() throws Exception
{

View File

@ -47,15 +47,12 @@ public class ForwardedSessionTest extends AbstractForwardedSessionTest
}
/**
* @see org.eclipse.jetty.server.session.AbstractForwardedSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new MongoTestServer(port);
return new MongoTestServer(port,maxInactive, scavengeInterval, evictionPolicy);
}
@Test
public void testSessionCreateInForward() throws Exception
{

View File

@ -0,0 +1,67 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.assertNotNull;
import org.eclipse.jetty.server.session.AbstractImmediateSaveTest;
import org.eclipse.jetty.server.session.AbstractTestServer;
import org.eclipse.jetty.server.session.SessionHandler;
import org.junit.After;
import org.junit.Before;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
* ImmediateSaveTest
*
*
*/
public class ImmediateSaveTest extends AbstractImmediateSaveTest
{
@Before
public void beforeTest() throws Exception
{
MongoTestServer.dropCollection();
MongoTestServer.createCollection();
}
@After
public void afterTest() throws Exception
{
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy)
{
return new MongoTestServer(port, max, scavenge, evictionPolicy)
{
public SessionHandler newSessionHandler()
{
SessionHandler h = super.newSessionHandler();
h.getSessionCache().setSaveOnCreate(true);
return h;
}
};
}
}

View File

@ -27,7 +27,6 @@ import org.junit.Test;
public class InvalidateSessionTest extends AbstractInvalidationSessionTest
{
public final static int IDLE_PASSIVATE_SEC = 1;
@BeforeClass
public static void beforeClass() throws Exception
@ -47,23 +46,4 @@ public class InvalidateSessionTest extends AbstractInvalidationSessionTest
{
return new MongoTestServer(port, maxInterval, scavengeInterval, evictionPolicy);
}
@Override
public void pause()
{
try
{
Thread.sleep(2 * IDLE_PASSIVATE_SEC * 1000);
}
catch (InterruptedException e)
{
}
}
@Test
public void testInvalidation() throws Exception
{
super.testInvalidation();
}
}

View File

@ -57,21 +57,12 @@ public class MongoTestServer extends AbstractTestServer
}
public MongoTestServer(int port)
{
super(port);
}
public MongoTestServer(int port, int idlePassivatePeriod)
{
super(port, 30, 10, idlePassivatePeriod);
}
public MongoTestServer(int port, int maxInactivePeriod, int scavengePeriod,int idlePassivatePeriod)
public MongoTestServer(int port, int maxInactivePeriod, int scavengePeriod, int idlePassivatePeriod)
{
super(port, maxInactivePeriod, scavengePeriod, idlePassivatePeriod);
}
public MongoTestServer(int port, int maxInactivePeriod, int scavengePeriod, int idlePassivatePeriod, boolean saveAllAttributes)
@ -105,7 +96,7 @@ public class MongoTestServer extends AbstractTestServer
public static void main(String... args) throws Exception
{
MongoTestServer server8080 = new MongoTestServer(8080);
/* MongoTestServer server8080 = new MongoTestServer(8080);
server8080.addContext("/").addServlet(SessionDump.class,"/");
server8080.start();
@ -114,7 +105,7 @@ public class MongoTestServer extends AbstractTestServer
server8081.start();
server8080.join();
server8081.join();
server8081.join();*/
}
}

View File

@ -44,9 +44,10 @@ public class ScatterGunLoadTest extends AbstractScatterGunLoadTest
}
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new MongoTestServer(port);
return new MongoTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test

View File

@ -41,12 +41,14 @@ public class ServerCrossContextSessionTest extends AbstractServerCrossContextSes
MongoTestServer.dropCollection();
}
public AbstractTestServer createServer(int port)
@Override
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new MongoTestServer(port);
return new MongoTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test
public void testCrossContextDispatch() throws Exception
{

View File

@ -42,11 +42,12 @@ public class SessionMigrationTest extends AbstractSessionMigrationTest
}
@Override
public AbstractTestServer createServer(int port)
public AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy)
{
return new MongoTestServer(port);
return new MongoTestServer(port, maxInactive, scavengeInterval, evictionPolicy);
}
@Test
public void testSessionMigration() throws Exception
{

View File

@ -40,10 +40,10 @@ import org.junit.Test;
/**
* AbstractClientCrossContextSessionTest
*/
public abstract class AbstractClientCrossContextSessionTest
public abstract class AbstractClientCrossContextSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port);
@Test
public void testCrossContextDispatch() throws Exception
@ -51,7 +51,7 @@ public abstract class AbstractClientCrossContextSessionTest
String contextA = "/contextA";
String contextB = "/contextB";
String servletMapping = "/server";
AbstractTestServer server = createServer(0);
AbstractTestServer server = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
TestServletA servletA = new TestServletA();
ServletHolder holderA = new ServletHolder(servletA);
ServletContextHandler ctxA = server.addContext(contextA);

View File

@ -56,18 +56,12 @@ import org.junit.Test;
* Test that creating a session and invalidating it before the request exits the session
* does not result in the session being persisted
*/
public abstract class AbstractCreateAndInvalidateTest{
public abstract class AbstractCreateAndInvalidateTest extends AbstractTestBase
{
protected TestServlet _servlet = new TestServlet();
protected AbstractTestServer _server1 = null;
public abstract AbstractTestServer createServer (int port, int max, int scavenge, int evictionPolicy);
/**
* @param sessionId

View File

@ -48,17 +48,14 @@ import org.junit.Test;
* This test requires that the sessions will be persisted, as the server is stopped and
* then restarted in order to check that all the attributes were saved.
*/
public abstract class AbstractForwardedSessionTest
public abstract class AbstractForwardedSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port);
@Test
public void testSessionCreateInForward() throws Exception
{
AbstractTestServer testServer = createServer(0);
AbstractTestServer testServer = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
ServletContextHandler testServletContextHandler = testServer.addContext("/context");
testServletContextHandler.addServlet(Servlet1.class, "/one");
testServletContextHandler.addServlet(Servlet2.class, "/two");

View File

@ -49,21 +49,11 @@ import org.junit.Test;
*
*
*/
public abstract class AbstractIdleSessionTest
public abstract class AbstractIdleSessionTest extends AbstractTestBase
{
protected TestServlet _servlet = new TestServlet();
protected AbstractTestServer _server1 = null;
/**
* @param port
* @param max
* @param scavenge
* @param evictionPolicy
* @return
*/
public abstract AbstractTestServer createServer (int port, int max, int scavenge, int evictionPolicy);
/**

View File

@ -0,0 +1,194 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.assertTrue;
import static org.junit.Assert.fail;
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.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.Test;
/**
* AbstractImmediateSaveTest
*
*
*/
public abstract class AbstractImmediateSaveTest extends AbstractTestBase
{
protected ServletContextHandler _context;
public void checkSessionSaved (String id) throws Exception
{
assertTrue(_context.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
}
public void pause(int scavenge)
{
try
{
Thread.sleep(scavenge * 1000L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
@Test
public void testSaveNewSession() throws Exception
{
String servletMapping = "/server";
int scavengePeriod = 3;
int maxInactivePeriod = -1;
AbstractTestServer server = createServer(0, maxInactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT);
_context = server.addContext("/");
ServletHolder h = new ServletHolder();
h.setServlet(new TestServlet());
_context.addServlet(h, servletMapping);
String contextPath = "";
try
{
server.start();
int port=server.getPort();
HttpClient client = new HttpClient();
client.start();
try
{
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=");
}
finally
{
client.stop();
}
}
finally
{
server.stop();
}
}
@Test
public void testSaveNewSessionWithEviction() throws Exception
{
String servletMapping = "/server";
int scavengePeriod = 3;
int maxInactivePeriod = 1;
AbstractTestServer server = createServer(0, maxInactivePeriod, scavengePeriod, SessionCache.EVICT_ON_SESSION_EXIT);
_context = server.addContext("/");
ServletHolder h = new ServletHolder();
h.setServlet(new TestServlet());
_context.addServlet(h, servletMapping);
String contextPath = "";
try
{
server.start();
int port=server.getPort();
HttpClient client = new HttpClient();
client.start();
try
{
//make request to make a save-on-create 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=");
//session should now be evicted from the cache
assertFalse(_context.getSessionHandler().getSessionCache().contains(AbstractTestServer.extractSessionId(sessionCookie)));
//make another request for the same session
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
//session should now be evicted from the cache again
assertFalse(_context.getSessionHandler().getSessionCache().contains(AbstractTestServer.extractSessionId(sessionCookie)));
}
finally
{
client.stop();
}
}
finally
{
server.stop();
}
}
public class TestServlet extends HttpServlet
{
String 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);
assertNotNull(session);
try
{
checkSessionSaved(session.getId());
}
catch (Exception e)
{
fail(e.getMessage());
}
}
else if ("test".equals(action))
{
HttpSession session = request.getSession(false);
assertNotNull(session);
}
}
}
}

View File

@ -41,9 +41,8 @@ import org.junit.Test;
/**
* AbstractImmortalSessionTest
*/
public abstract class AbstractImmortalSessionTest
public abstract class AbstractImmortalSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int maxInactiveMs, int scavengeMs, int evictionPolicy);
@Test
public void testImmortalSession() throws Exception

View File

@ -38,20 +38,25 @@ import org.junit.Test;
/**
* AbstractInvalidationSessionTest
*
* Goal of the test is to be sure that invalidating a session on one node
* result in the session being unavailable in the other node also.
* result in the session being unavailable in the other node also. This
* simulates an environment without a sticky load balancer. In this case,
* you must use session eviction, to try to ensure that as the session
* bounces around it gets a fresh load of data from the SessionDataStore.
*/
public abstract class AbstractInvalidationSessionTest
public abstract class AbstractInvalidationSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy);
public abstract void pause();
@Test
public void testInvalidation() throws Exception
{
String contextPath = "";
String servletMapping = "/server";
AbstractTestServer server1 = createServer(0, 30, 1, 1);
int maxInactiveInterval = 30;
int scavengeInterval = 1;
AbstractTestServer server1 = createServer(0, maxInactiveInterval, scavengeInterval, SessionCache.EVICT_ON_SESSION_EXIT);
server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
@ -59,7 +64,7 @@ public abstract class AbstractInvalidationSessionTest
{
server1.start();
int port1 = server1.getPort();
AbstractTestServer server2 = createServer(0, 30, 1, 1);
AbstractTestServer server2 = createServer(0, maxInactiveInterval, scavengeInterval, SessionCache.EVICT_ON_SESSION_EXIT);
server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try
@ -96,9 +101,7 @@ public abstract class AbstractInvalidationSessionTest
Request request1 = client.newRequest(urls[0] + "?action=invalidate");
request1.header("Cookie", sessionCookie);
response1 = request1.send();
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
pause();
assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
// Be sure on node2 we don't see the session anymore
request2 = client.newRequest(urls[1] + "?action=test");

View File

@ -51,13 +51,9 @@ import org.junit.Test;
* scavenged by node A. In other words, it tests that a session that migrates from one node
* to another is not timed out on the original node.
*/
public abstract class AbstractLastAccessTimeTest
public abstract class AbstractLastAccessTimeTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int idlePassivatePeriod);
@Test
public void testLastAccessTime() throws Exception
{

View File

@ -39,9 +39,8 @@ import org.junit.Test;
/**
* AbstractLocalSessionScavengingTest
*/
public abstract class AbstractLocalSessionScavengingTest
public abstract class AbstractLocalSessionScavengingTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
public void pause(int scavengePeriod)
{

View File

@ -20,7 +20,9 @@ package org.eclipse.jetty.server.session;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import javax.servlet.ServletException;
@ -38,18 +40,17 @@ import org.junit.Test;
/**
* AbstractNewSessionTest
*
* Create a session, wait for it to be scanvenged, represent the cookie and check that a
* Create a session, wait for it to be scavenged, re-present the cookie and check that a
* new session is created.
*/
public abstract class AbstractNewSessionTest
public abstract class AbstractNewSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
public void pause(int scavenge)
{
try
{
Thread.sleep(scavenge * 2500L);
Thread.sleep(scavenge * 1000L);
}
catch (InterruptedException e)
{
@ -62,7 +63,8 @@ public abstract class AbstractNewSessionTest
{
String servletMapping = "/server";
int scavengePeriod = 3;
AbstractTestServer server = createServer(0, 1, scavengePeriod, SessionCache.NEVER_EVICT);
int maxInactivePeriod = 1;
AbstractTestServer server = createServer(0, maxInactivePeriod, scavengePeriod, SessionCache.NEVER_EVICT);
ServletContextHandler context = server.addContext("/");
context.addServlet(TestServlet.class, servletMapping);
String contextPath = "";
@ -82,11 +84,11 @@ public abstract class AbstractNewSessionTest
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
// Let's wait for the scavenger to run, waiting 2.5 times the scavenger period
pause(scavengePeriod);
// Let's wait for the scavenger to run
pause(maxInactivePeriod + scavengePeriod);
// The session is not there anymore, but we present an old cookie
// The server creates a new session, we must ensure we released all locks
// 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 + "?action=old-create");
request.header("Cookie", sessionCookie);
response = request.send();
@ -105,6 +107,8 @@ public abstract class AbstractNewSessionTest
}
public static class TestServlet extends HttpServlet
{
String id;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
@ -113,10 +117,15 @@ public abstract class AbstractNewSessionTest
{
HttpSession session = request.getSession(true);
assertTrue(session.isNew());
id = session.getId();
}
else if ("old-create".equals(action))
{
request.getSession(true);
HttpSession s = request.getSession(false);
assertNull(s);
s = request.getSession(true);
assertNotNull(s);
assertFalse(s.getId().equals(id));
}
else
{

View File

@ -38,11 +38,9 @@ import org.junit.Test;
/**
* AbstractOrphanedSessionTest
*/
public abstract class AbstractOrphanedSessionTest
public abstract class AbstractOrphanedSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
/**
* If nodeA creates a session, and just afterwards crashes, it is the only node that knows about the session.
* We want to test that the session data is gone after scavenging.

View File

@ -44,13 +44,11 @@ import org.junit.Test;
*
*
*/
public abstract class AbstractProxySerializationTest
public abstract class AbstractProxySerializationTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
public abstract void customizeContext (ServletContextHandler c);
/**
* @param msec milliseconds to sleep

View File

@ -41,9 +41,9 @@ import org.junit.Test;
* While a request is still active in a context, make another
* request to it to ensure both share same session.
*/
public abstract class AbstractReentrantRequestSessionTest
public abstract class AbstractReentrantRequestSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
@Test
public void testReentrantRequestSession() throws Exception

View File

@ -45,9 +45,8 @@ import org.junit.Test;
* Test that invalidating a session does not return the session on the next request.
*
*/
public abstract class AbstractRemoveSessionTest
public abstract class AbstractRemoveSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int idlePassivationPeriod);
@Test

View File

@ -48,12 +48,10 @@ import org.junit.Test;
* This test performs multiple concurrent requests for the same session on the same node.
*
*/
public abstract class AbstractSameNodeLoadTest
public abstract class AbstractSameNodeLoadTest extends AbstractTestBase
{
protected boolean _stress = Boolean.getBoolean( "STRESS" );
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
@Test

View File

@ -50,11 +50,10 @@ import org.junit.Test;
* In the real world, we must have a load balancer that uses sticky sessions
* to keep the session pinned to a particular node.
*/
public abstract class AbstractScatterGunLoadTest
public abstract class AbstractScatterGunLoadTest extends AbstractTestBase
{
protected boolean _stress = Boolean.getBoolean( "STRESS" );
public abstract AbstractTestServer createServer(int port);
@Test
public void testLightLoad()
@ -64,14 +63,14 @@ public abstract class AbstractScatterGunLoadTest
{
String contextPath = "";
String servletMapping = "/server";
AbstractTestServer server1 = createServer( 0 );
AbstractTestServer server1 = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
server1.addContext( contextPath ).addServlet( TestServlet.class, servletMapping );
try
{
server1.start();
int port1 = server1.getPort();
AbstractTestServer server2 = createServer( 0 );
AbstractTestServer server2 = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
server2.addContext( contextPath ).addServlet( TestServlet.class, servletMapping );
try

View File

@ -39,18 +39,16 @@ import org.junit.Test;
/**
* AbstractServerCrossContextSessionTest
*/
public abstract class AbstractServerCrossContextSessionTest
public abstract class AbstractServerCrossContextSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port);
@Test
public void testCrossContextDispatch() throws Exception
{
String contextA = "/contextA";
String contextB = "/contextB";
String servletMapping = "/server";
AbstractTestServer server = createServer(0);
AbstractTestServer server = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
ServletContextHandler ctxA = server.addContext(contextA);
ctxA.addServlet(TestServletA.class, servletMapping);
ServletContextHandler ctxB = server.addContext(contextB);

View File

@ -41,9 +41,8 @@ import junit.framework.Assert;
/**
* AbstractSessionCookieTest
*/
public abstract class AbstractSessionCookieTest
public abstract class AbstractSessionCookieTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
public void pause(int scavenge)
{

View File

@ -46,9 +46,8 @@ import org.junit.Test;
*
*
*/
public abstract class AbstractSessionExpiryTest
public abstract class AbstractSessionExpiryTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
public void pause(int scavengePeriod)
{

View File

@ -53,7 +53,7 @@ import org.junit.Test;
* 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 AbstractSessionInvalidateAndCreateTest
public abstract class AbstractSessionInvalidateAndCreateTest extends AbstractTestBase
{
public class MySessionListener implements HttpSessionListener
{
@ -70,8 +70,6 @@ public abstract class AbstractSessionInvalidateAndCreateTest
}
}
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
public void pause(int scavengePeriod)

View File

@ -40,16 +40,16 @@ import org.junit.Test;
*
* Check that a session that is active on node 1 can be accessed on node2.
*/
public abstract class AbstractSessionMigrationTest
public abstract class AbstractSessionMigrationTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port);
@Test
public void testSessionMigration() throws Exception
{
String contextPath = "";
String servletMapping = "/server";
AbstractTestServer server1 = createServer(0);
AbstractTestServer server1 = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try
@ -57,7 +57,7 @@ public abstract class AbstractSessionMigrationTest
server1.start();
int port1=server1.getPort();
AbstractTestServer server2 = createServer(0);
AbstractTestServer server2 = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
try

View File

@ -46,11 +46,10 @@ import org.eclipse.jetty.webapp.WebAppContext;
*
* Test that changes the session id during a request.
*/
public abstract class AbstractSessionRenewTest
public abstract class AbstractSessionRenewTest extends AbstractTestBase
{
protected AbstractTestServer _server;
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
public abstract boolean verifyChange (WebAppContext context, String oldSessionId, String newSessionId);

View File

@ -39,9 +39,8 @@ import org.junit.Test;
/**
* AbstractSessionValueSavingTest
*/
public abstract class AbstractSessionValueSavingTest
public abstract class AbstractSessionValueSavingTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int max, int scavenge, int evictionPolicy);
@Test
public void testSessionValueSaving() throws Exception

View File

@ -40,15 +40,14 @@ import org.junit.Test;
*
*
*/
public abstract class AbstractStopSessionManagerPreserveSessionTest
public abstract class AbstractStopSessionManagerPreserveSessionTest extends AbstractTestBase
{
public String _id;
public abstract void checkSessionPersisted (boolean expected);
public abstract AbstractTestServer createServer (int port);
public abstract void checkSessionPersisted (String id, boolean expected);
public abstract void configureSessionManagement(ServletContextHandler context);
@Test
@ -57,7 +56,7 @@ public abstract class AbstractStopSessionManagerPreserveSessionTest
String contextPath = "";
String servletMapping = "/server";
AbstractTestServer server = createServer(0);
AbstractTestServer server = createServer(0, -1, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
ServletContextHandler context = server.addContext(contextPath);
ServletHolder holder = new ServletHolder();
TestServlet servlet = new TestServlet();
@ -87,7 +86,7 @@ public abstract class AbstractStopSessionManagerPreserveSessionTest
context.getSessionHandler().stop();
//check the database to see that the session is still valid
checkSessionPersisted(true);
checkSessionPersisted(_id, true);
}
finally

View File

@ -0,0 +1,30 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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;
/**
* AbstractTestBase
*
*
*/
public abstract class AbstractTestBase
{
public abstract AbstractTestServer createServer(int port, int maxInactive, int scavengeInterval, int evictionPolicy);
}

View File

@ -34,7 +34,6 @@ import org.eclipse.jetty.webapp.WebAppContext;
public abstract class AbstractTestServer
{
public static int DEFAULT_MAX_INACTIVE = 30;
public static int DEFAULT_SCAVENGE_SEC = 10;
public static int DEFAULT_EVICTIONPOLICY = SessionCache.NEVER_EVICT;
@ -68,11 +67,6 @@ public abstract class AbstractTestServer
}
public AbstractTestServer(int port)
{
this(port, DEFAULT_MAX_INACTIVE, DEFAULT_SCAVENGE_SEC, DEFAULT_EVICTIONPOLICY);
}
public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod, int evictionPolicy)
{

View File

@ -44,10 +44,9 @@ import org.junit.Test;
*
* This test is only appropriate for clustered session managers.
*/
public abstract class AbstractWebAppObjectInSessionTest
public abstract class AbstractWebAppObjectInSessionTest extends AbstractTestBase
{
public abstract AbstractTestServer createServer(int port);
@Test
public void testWebappObjectInSession() throws Exception
{
@ -94,7 +93,7 @@ public abstract class AbstractWebAppObjectInSessionTest
//copy(sourceFile, targetFile);
IO.copy(resource.getInputStream(), new FileOutputStream(targetFile));
AbstractTestServer server1 = createServer(0);
AbstractTestServer server1 = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
server1.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
try
@ -102,7 +101,7 @@ public abstract class AbstractWebAppObjectInSessionTest
server1.start();
int port1 = server1.getPort();
AbstractTestServer server2 = createServer(0);
AbstractTestServer server2 = createServer(0, AbstractTestServer.DEFAULT_MAX_INACTIVE, AbstractTestServer.DEFAULT_SCAVENGE_SEC, AbstractTestServer.DEFAULT_EVICTIONPOLICY);
server2.addWebAppContext(warDir.getCanonicalPath(), contextPath).addServlet(WebAppObjectInSessionServlet.class.getName(), servletMapping);
try