Start making unit tests work.

This commit is contained in:
Jan Bartel 2015-10-23 16:52:05 +11:00
parent c5489bd7b2
commit 84239bc7f2
35 changed files with 1008 additions and 159 deletions

View File

@ -243,6 +243,7 @@ public abstract class AbstractSessionIdManager extends AbstractLifeCycle impleme
} }
_scavenger.start(); _scavenger.start();
System.err.println("Started scavenger "+_scavenger);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */

View File

@ -130,14 +130,17 @@ public abstract class AbstractSessionStore extends AbstractLifeCycle implements
{ {
//look locally //look locally
Session session = doGet(key); Session session = doGet(key);
//not in session store, load the data for the session if possible //not in session store, load the data for the session if possible
if (session == null && _sessionDataStore != null) if (session == null && _sessionDataStore != null)
{ {
SessionData data = _sessionDataStore.load(key); SessionData data = _sessionDataStore.load(key);
session = newSession(data); if (data != null)
session.setSessionManager(_manager); {
doPut(key, session); session = newSession(data);
session.setSessionManager(_manager);
doPut(key, session);
}
} }
return session; return session;
} }

View File

@ -105,7 +105,7 @@ public class FileSessionDataStore extends AbstractSessionDataStore
if (_storeDir != null) if (_storeDir != null)
{ {
file = new File(_storeDir, key.toString()); file = new File(_storeDir, key.toString());
if (file.exists()) if (file.exists() && file.getParentFile().equals(_storeDir))
{ {
file.delete(); file.delete();
return true; return true;
@ -150,13 +150,12 @@ public class FileSessionDataStore extends AbstractSessionDataStore
} }
catch (UnreadableSessionDataException e) catch (UnreadableSessionDataException e)
{ {
if (isDeleteUnrestorableFiles() && file.exists()) if (isDeleteUnrestorableFiles() && file.exists() && file.getParentFile().equals(_storeDir));
{ {
file.delete(); file.delete();
LOG.warn("Deleted unrestorable file for session {}", key); LOG.warn("Deleted unrestorable file for session {}", key);
return null;
} }
return null; throw e;
} }
} }

View File

@ -0,0 +1,57 @@
//
// ========================================================================
// 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.server.session;
/**
* FileHashSessionManager
*
* Session manager that stores its sessions in files on disk
*
*/
public class FileSessionManager extends SessionManager
{
protected FileSessionDataStore _sessionDataStore = new FileSessionDataStore();
@Override
public void doStart() throws Exception
{
_sessionStore = new MemorySessionStore();
((AbstractSessionStore)_sessionStore).setSessionDataStore(_sessionDataStore);
super.doStart();
}
@Override
public void doStop() throws Exception
{
super.doStop();
}
/**
* Get the SessionDataStore to configure it
* @return
*/
public FileSessionDataStore getSessionDataStore()
{
return _sessionDataStore;
}
}

View File

@ -22,11 +22,12 @@ package org.eclipse.jetty.server.session;
/** /**
* HashSessionManager * HashSessionManager
* *
* * In memory-only session manager.
*
*/ */
public class HashSessionManager extends SessionManager public class HashSessionManager extends SessionManager
{ {
protected FileSessionDataStore _sessionDataStore = new FileSessionDataStore(); protected NullSessionDataStore _sessionDataStore = new NullSessionDataStore();
@Override @Override
@ -44,13 +45,4 @@ public class HashSessionManager extends SessionManager
super.doStop(); super.doStop();
} }
/**
* Get the SessionDataStore to configure it
* @return
*/
public FileSessionDataStore getSessionDataStore()
{
return _sessionDataStore;
}
} }

View File

@ -199,6 +199,7 @@ public class MemorySessionStore extends AbstractSessionStore
@Override @Override
public Session newSession(SessionKey key, long created, long accessed, long lastAccessed, long maxInactiveMs) public Session newSession(SessionKey key, long created, long accessed, long lastAccessed, long maxInactiveMs)
{ {
//TODO - how to tell that the session is new?!
return new MemorySession(_sessionDataStore.newSessionData(key, created, accessed, lastAccessed, maxInactiveMs)); return new MemorySession(_sessionDataStore.newSessionData(key, created, accessed, lastAccessed, maxInactiveMs));
} }

View File

@ -83,6 +83,8 @@ public class SessionKey
public static String getContextPath (Context context) public static String getContextPath (Context context)
{ {
if (context == null)
return "";
return canonicalize (context.getContextPath()); return canonicalize (context.getContextPath());
} }

View File

@ -621,6 +621,7 @@ public class SessionManager extends ContainerLifeCycle implements org.eclipse.je
Session session = _sessionStore.newSession(key, created, created, created, (_dftMaxIdleSecs>0?_dftMaxIdleSecs*1000L:-1)); Session session = _sessionStore.newSession(key, created, created, created, (_dftMaxIdleSecs>0?_dftMaxIdleSecs*1000L:-1));
session.setExtendedId(_sessionIdManager.getExtendedId(id,request)); session.setExtendedId(_sessionIdManager.getExtendedId(id,request));
session.setSessionManager(this); session.setSessionManager(this);
session.setLastNode(_sessionIdManager.getWorkerName());
if (request.isSecure()) if (request.isSecure())
session.setAttribute(Session.SESSION_CREATED_SECURE, Boolean.TRUE); session.setAttribute(Session.SESSION_CREATED_SECURE, Boolean.TRUE);

View File

@ -167,6 +167,9 @@ public class SessionScavenger extends AbstractLifeCycle
if (isStopping() || isStopped()) if (isStopping() || isStopped())
return; return;
if (LOG.isDebugEnabled())
LOG.debug("Scavenging sessions");
//find the session managers //find the session managers
Handler[] contexts = ((AbstractSessionIdManager)_sessionIdManager).getServer().getChildHandlersByClass(ContextHandler.class); Handler[] contexts = ((AbstractSessionIdManager)_sessionIdManager).getServer().getChildHandlersByClass(ContextHandler.class);
for (int i=0; contexts!=null && i<contexts.length; i++) for (int i=0; contexts!=null && i<contexts.length; i++)
@ -199,4 +202,13 @@ public class SessionScavenger extends AbstractLifeCycle
} }
@Override
public String toString()
{
return super.toString()+"[interval="+_scavengeIntervalMs+", ownscheduler="+_ownScheduler+"]";
}
} }

View File

@ -25,62 +25,110 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StdErrLog;
import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
public class HashSessionManagerTest public class FileSessionManagerTest
{ {
private static StdErrLog _log;
private static boolean _stacks;
// @BeforeClass
public static void beforeClass ()
{
_log = ((StdErrLog)Log.getLogger("org.eclipse.jetty.server.session"));
_stacks = _log.isHideStacks();
_log.setHideStacks(true);
}
//@AfterClass
public static void afterClass()
{
_log.setHideStacks(_stacks);
}
@Test @Test
public void testDangerousSessionIdRemoval() throws Exception public void testDangerousSessionIdRemoval() throws Exception
{ {
final HashSessionManager manager = new HashSessionManager(); Server server = new Server();
SessionHandler handler = new SessionHandler();
handler.setServer(server);
final HashSessionIdManager idmgr = new HashSessionIdManager();
idmgr.setServer(server);
server.setSessionIdManager(idmgr);
final FileSessionManager manager = new FileSessionManager();
manager.getSessionDataStore().setDeleteUnrestorableFiles(true); manager.getSessionDataStore().setDeleteUnrestorableFiles(true);
//manager.setLazyLoad(true); //manager.setLazyLoad(true);
File testDir = MavenTestingUtils.getTargetTestingDir("hashes"); File testDir = MavenTestingUtils.getTargetTestingDir("hashes");
testDir.mkdirs(); testDir.mkdirs();
manager.getSessionDataStore().setStoreDir(testDir); manager.getSessionDataStore().setStoreDir(testDir);
manager.setSessionIdManager(idmgr);
MavenTestingUtils.getTargetFile("dangerFile.session").createNewFile(); handler.setSessionManager(manager);
manager.start();
Assert.assertTrue("File should exist!", MavenTestingUtils.getTargetFile("dangerFile.session").exists()); String expectedFilename = "../../_0.0.0.0_dangerFile";
manager.getSession("../../dangerFile.session");
Assert.assertTrue("File should exist!", MavenTestingUtils.getTargetFile("dangerFile.session").exists()); MavenTestingUtils.getTargetFile(expectedFilename).createNewFile();
Assert.assertTrue("File should exist!", MavenTestingUtils.getTargetFile(expectedFilename).exists());
manager.getSession("../../_0.0.0.0_dangerFile");
Assert.assertTrue("File should exist!", MavenTestingUtils.getTargetFile(expectedFilename).exists());
} }
@Test @Test
public void testValidSessionIdRemoval() throws Exception public void testValidSessionIdRemoval() throws Exception
{ {
final HashSessionManager manager = new HashSessionManager(); Server server = new Server();
SessionHandler handler = new SessionHandler();
handler.setServer(server);
final HashSessionIdManager idmgr = new HashSessionIdManager();
idmgr.setServer(server);
server.setSessionIdManager(idmgr);
final FileSessionManager manager = new FileSessionManager();
manager.getSessionDataStore().setDeleteUnrestorableFiles(true); manager.getSessionDataStore().setDeleteUnrestorableFiles(true);
// manager.setLazyLoad(true); manager.setSessionIdManager(idmgr);
handler.setSessionManager(manager);
// manager.setLazyLoad(true);
File testDir = MavenTestingUtils.getTargetTestingDir("hashes"); File testDir = MavenTestingUtils.getTargetTestingDir("hashes");
FS.ensureEmpty(testDir); FS.ensureEmpty(testDir);
manager.getSessionDataStore().setStoreDir(testDir); manager.getSessionDataStore().setStoreDir(testDir);
manager.start();
Assert.assertTrue(new File(testDir, "validFile.session").createNewFile()); //See SessionKey.getKey()
String expectedFilename = "_0.0.0.0_validFile123";
Assert.assertTrue("File should exist!", new File(testDir, "validFile.session").exists()); Assert.assertTrue(new File(testDir, expectedFilename).createNewFile());
manager.getSession("validFile.session");
Assert.assertTrue("File shouldn't exist!", !new File(testDir,"validFile.session").exists()); Assert.assertTrue("File should exist!", new File(testDir, expectedFilename).exists());
manager.getSession("validFile123");
Assert.assertTrue("File shouldn't exist!", !new File(testDir,expectedFilename).exists());
} }
@Test @Test
public void testHashSession() throws Exception public void testHashSession() throws Exception
{ {
File testDir = MavenTestingUtils.getTargetTestingDir("saved"); File testDir = MavenTestingUtils.getTargetTestingDir("saved");
IO.delete(testDir); IO.delete(testDir);
testDir.mkdirs(); testDir.mkdirs();
Server server = new Server(); Server server = new Server();
SessionHandler handler = new SessionHandler(); SessionHandler handler = new SessionHandler();
handler.setServer(server); handler.setServer(server);
HashSessionManager manager = new HashSessionManager(); FileSessionManager manager = new FileSessionManager();
manager.getSessionDataStore().setStoreDir(testDir); manager.getSessionDataStore().setStoreDir(testDir);
manager.setMaxInactiveInterval(5); manager.setMaxInactiveInterval(5);
Assert.assertTrue(testDir.exists()); Assert.assertTrue(testDir.exists());
@ -88,6 +136,7 @@ public class HashSessionManagerTest
handler.setSessionManager(manager); handler.setSessionManager(manager);
AbstractSessionIdManager idManager = new HashSessionIdManager(); AbstractSessionIdManager idManager = new HashSessionIdManager();
idManager.setServer(server);
idManager.setWorkerName("foo"); idManager.setWorkerName("foo");
manager.setSessionIdManager(idManager); manager.setSessionIdManager(idManager);
server.setSessionIdManager(idManager); server.setSessionIdManager(idManager);
@ -105,7 +154,11 @@ public class HashSessionManagerTest
manager.setMaxInactiveInterval(30); // change max inactive interval for *new* sessions manager.setMaxInactiveInterval(30); // change max inactive interval for *new* sessions
manager.stop(); manager.stop();
Assert.assertTrue("File should exist!", new File(testDir, session.getId()).exists()); for (String f: testDir.list())
System.err.println(f);
String expectedFilename = "_0.0.0.0_"+session.getId();
Assert.assertTrue("File should exist!", new File(testDir, expectedFilename).exists());
manager.start(); manager.start();

View File

@ -32,6 +32,7 @@
<modules> <modules>
<module>test-sessions-common</module> <module>test-sessions-common</module>
<module>test-hash-sessions</module> <module>test-hash-sessions</module>
<module>test-file-sessions</module>
<module>test-jdbc-sessions</module> <module>test-jdbc-sessions</module>
<module>test-mongodb-sessions</module> <module>test-mongodb-sessions</module>
<module>test-infinispan-sessions</module> <module>test-infinispan-sessions</module>

View File

@ -0,0 +1 @@
/target/

View File

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
// ========================================================================
// Copyright (c) Webtide LLC
//
// 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.apache.org/licenses/LICENSE-2.0.txt
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-sessions-parent</artifactId>
<version>9.3.4-SNAPSHOT</version>
</parent>
<artifactId>test-file-sessions</artifactId>
<name>Jetty Tests :: Sessions :: File</name>
<url>http://www.eclipse.org/jetty</url>
<properties>
<bundle-symbolic-name>${project.groupId}.sessions.file</bundle-symbolic-name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<!-- DO NOT DEPLOY (or Release) -->
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-sessions-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<!-- Leaving at compile scope for intellij bug reasons -->
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,54 @@
//
// ========================================================================
// 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.server.session;
import java.io.File;
import org.eclipse.jetty.util.IO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ClientCrossContextSessionTest extends AbstractClientCrossContextSessionTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port)
{
return new FileTestServer(port);
}
@Test
public void testCrossContextDispatch() throws Exception
{
super.testCrossContextDispatch();
}
}

View File

@ -0,0 +1,84 @@
//
// ========================================================================
// 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.server.session;
import java.io.File;
import org.eclipse.jetty.server.SessionIdManager;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.util.IO;
/**
* @version $Revision$ $Date$
*/
public class FileTestServer extends AbstractTestServer
{
static int __workers=0;
static File _tmpDir;
public static void setup ()
throws Exception
{
_tmpDir = File.createTempFile("file", null);
_tmpDir.delete();
_tmpDir.mkdirs();
_tmpDir.deleteOnExit();
}
public static void teardown ()
{
IO.delete(_tmpDir);
_tmpDir = null;
}
public FileTestServer(int port)
{
super(port, 30, 10);
}
public FileTestServer(int port, int maxInactivePeriod, int scavengePeriod)
{
super(port, maxInactivePeriod, scavengePeriod);
}
public SessionIdManager newSessionIdManager(Object config)
{
HashSessionIdManager mgr = new HashSessionIdManager();
mgr.setWorkerName("worker"+(__workers++));
return mgr;
}
public SessionManager newSessionManager()
{
FileSessionManager manager = new FileSessionManager();
manager.getSessionDataStore().setStoreDir(_tmpDir);
return manager;
}
public SessionHandler newSessionHandler(SessionManager sessionManager)
{
return new SessionHandler(sessionManager);
}
}

View File

@ -19,10 +19,6 @@
package org.eclipse.jetty.server.session; package org.eclipse.jetty.server.session;
import java.io.File;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.util.IO;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -34,47 +30,23 @@ import org.junit.Test;
*/ */
public class ForwardedSessionTest extends AbstractForwardedSessionTest public class ForwardedSessionTest extends AbstractForwardedSessionTest
{ {
File tmpDir;
@Before @Before
public void before() throws Exception public void before() throws Exception
{ {
tmpDir = File.createTempFile("hash-session-forward-test", null); FileTestServer.setup();
tmpDir.delete();
tmpDir.mkdirs();
tmpDir.deleteOnExit();
} }
@After @After
public void after() public void after()
{ {
IO.delete(tmpDir); FileTestServer.teardown();
} }
@Override @Override
public AbstractTestServer createServer(int port) public AbstractTestServer createServer(int port)
{ {
return new HashTestServer(port) return new FileTestServer(port);
{
@Override
public SessionManager newSessionManager()
{
HashSessionManager sessionManager = (HashSessionManager)super.newSessionManager();
sessionManager.setSavePeriod(2);
try
{
sessionManager.setStoreDirectory(tmpDir);
}
catch (Exception e)
{
throw new IllegalStateException(e);
}
return sessionManager;
}
};
} }

View File

@ -0,0 +1,45 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
public class ImmortalSessionTest extends AbstractImmortalSessionTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port,max,scavenge);
}
}

View File

@ -0,0 +1,53 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* NewSessionTest
*/
public class NewSessionTest extends AbstractNewSessionTest
{
@Before
public void before() throws Exception
{
System.setProperty("org.eclipse.jetty.server.session.LEVEL", "DEBUG");
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port,max,scavenge);
}
@Test
public void testNewSession() throws Exception
{
super.testNewSession();
}
}

View File

@ -0,0 +1,52 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* OrphanedSessionTest
*/
public class OrphanedSessionTest extends AbstractOrphanedSessionTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port,max,scavenge);
}
@Test
public void testOrphanedSession() throws Exception
{
super.testOrphanedSession();
}
}

View File

@ -23,6 +23,8 @@ import java.io.File;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/** /**
@ -32,40 +34,29 @@ import org.junit.Test;
*/ */
public class ProxySerializationTest extends AbstractProxySerializationTest public class ProxySerializationTest extends AbstractProxySerializationTest
{ {
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
/** /**
* @see org.eclipse.jetty.server.session.AbstractProxySerializationTest#createServer(int, int, int) * @see org.eclipse.jetty.server.session.AbstractProxySerializationTest#createServer(int, int, int)
*/ */
@Override @Override
public AbstractTestServer createServer(int port, int max, int scavenge) public AbstractTestServer createServer(int port, int max, int scavenge)
{ {
return new HashTestServer(port,max,scavenge); return new FileTestServer(port,max,scavenge);
} }
@Override
public void customizeContext(ServletContextHandler c)
{
if (c == null)
return;
//Ensure that the HashSessionManager will persist sessions on passivation
HashSessionManager manager = (HashSessionManager)c.getSessionHandler().getSessionManager();
manager.setLazyLoad(false);
manager.setIdleSavePeriod(1);
try
{
File testDir = MavenTestingUtils.getTargetTestingDir("foo");
testDir.mkdirs();
manager.setStoreDirectory(testDir);
}
catch (Exception e)
{
throw new IllegalStateException(e);
}
}
@ -75,4 +66,14 @@ public class ProxySerializationTest extends AbstractProxySerializationTest
super.testProxySerialization(); super.testProxySerialization();
} }
/**
* @see org.eclipse.jetty.server.session.AbstractProxySerializationTest#customizeContext(org.eclipse.jetty.servlet.ServletContextHandler)
*/
@Override
public void customizeContext(ServletContextHandler c)
{
// TODO Auto-generated method stub
}
} }

View File

@ -0,0 +1,54 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* ReentrantRequestSessionTest
*/
public class ReentrantRequestSessionTest extends AbstractReentrantRequestSessionTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port)
{
return new FileTestServer(port);
}
@Test
public void testReentrantRequestSession() throws Exception
{
super.testReentrantRequestSession();
}
}

View File

@ -0,0 +1,52 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class RemoveSessionTest extends AbstractRemoveSessionTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port,max,scavenge);
}
@Test
public void testRemoveSession() throws Exception
{
super.testRemoveSession();
}
}

View File

@ -0,0 +1,55 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* ScatterGunLoadTest
*/
public class ScatterGunLoadTest extends AbstractScatterGunLoadTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port)
{
return new FileTestServer(port);
}
@Test
public void testLightLoad() throws Exception
{
super.testLightLoad();
}
}

View File

@ -0,0 +1,51 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ServerCrossContextSessionTest extends AbstractServerCrossContextSessionTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
public AbstractTestServer createServer(int port)
{
return new FileTestServer(port);
}
@Test
public void testCrossContextDispatch() throws Exception
{
super.testCrossContextDispatch();
}
}

View File

@ -0,0 +1,48 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
public class SessionCookieTest extends AbstractSessionCookieTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port, max, scavenge);
}
}

View File

@ -0,0 +1,51 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SessionInvalidateAndCreateTest extends AbstractSessionInvalidateAndCreateTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port,max,scavenge);
}
@Test
public void testSessionScavenge() throws Exception
{
super.testSessionScavenge();
}
}

View File

@ -0,0 +1,58 @@
//
// ========================================================================
// 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.server.session;
import java.io.File;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.util.IO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SessionRenewTest extends AbstractSessionRenewTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port, max, scavenge);
}
@Test
public void testSessionRenewal() throws Exception
{
super.testSessionRenewal();
}
}

View File

@ -0,0 +1,46 @@
//
// ========================================================================
// 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.server.session;
import org.junit.After;
import org.junit.Before;
public class SessionValueSharedSaving extends AbstractSessionValueSavingTest
{
@Before
public void before() throws Exception
{
FileTestServer.setup();
}
@After
public void after()
{
FileTestServer.teardown();
}
@Override
public AbstractTestServer createServer(int port, int max, int scavenge)
{
return new FileTestServer(port,max,scavenge);
}
}

View File

@ -18,14 +18,16 @@
package org.eclipse.jetty.server.session; package org.eclipse.jetty.server.session;
import java.io.File;
import org.eclipse.jetty.util.IO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class ClientCrossContextSessionTest extends AbstractClientCrossContextSessionTest public class ClientCrossContextSessionTest extends AbstractClientCrossContextSessionTest
{ {
public AbstractTestServer createServer(int port)
{
return new HashTestServer(port);
}
@Test @Test
public void testCrossContextDispatch() throws Exception public void testCrossContextDispatch() throws Exception
@ -33,4 +35,13 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
super.testCrossContextDispatch(); super.testCrossContextDispatch();
} }
/**
* @see org.eclipse.jetty.server.session.AbstractClientCrossContextSessionTest#createServer(int)
*/
@Override
public AbstractTestServer createServer(int port)
{
return new HashTestServer(port);
}
} }

View File

@ -26,7 +26,8 @@ import org.eclipse.jetty.server.SessionManager;
*/ */
public class HashTestServer extends AbstractTestServer public class HashTestServer extends AbstractTestServer
{ {
static int __workers=0;
public HashTestServer(int port) public HashTestServer(int port)
{ {
super(port, 30, 10); super(port, 30, 10);
@ -40,13 +41,14 @@ public class HashTestServer extends AbstractTestServer
public SessionIdManager newSessionIdManager(Object config) public SessionIdManager newSessionIdManager(Object config)
{ {
return new HashSessionIdManager(); HashSessionIdManager mgr = new HashSessionIdManager();
mgr.setWorkerName("worker"+(__workers++));
return mgr;
} }
public SessionManager newSessionManager() public SessionManager newSessionManager()
{ {
HashSessionManager manager = new HashSessionManager(); HashSessionManager manager = new HashSessionManager();
manager.setScavengePeriod(_scavengePeriod);
return manager; return manager;
} }

View File

@ -47,6 +47,8 @@ import org.junit.Test;
* IdleSessionTest * IdleSessionTest
* *
* Checks that a session can be idled and de-idled on the next request if it hasn't expired. * Checks that a session can be idled and de-idled on the next request if it hasn't expired.
*
* TODO support session idling in FileSessionDataStore?
* *
*/ */
public class IdleSessionTest public class IdleSessionTest
@ -67,17 +69,10 @@ public class IdleSessionTest
@Override @Override
public SessionManager newSessionManager() public SessionManager newSessionManager()
{ {
try HashSessionManager manager = (HashSessionManager)super.newSessionManager();
{ //manager.getSessionDataStore().setStoreDir(_storeDir);
HashSessionManager manager = (HashSessionManager)super.newSessionManager(); //manager.setIdleSavePeriod(_idlePeriod);
manager.setStoreDirectory(_storeDir); return manager;
manager.setIdleSavePeriod(_idlePeriod);
return manager;
}
catch ( IOException e)
{
return null;
}
} }
@ -103,7 +98,6 @@ public class IdleSessionTest
} }
} }
@Test
public void testSessionIdle() throws Exception public void testSessionIdle() throws Exception
{ {
String contextPath = ""; String contextPath = "";
@ -111,7 +105,7 @@ public class IdleSessionTest
int inactivePeriod = 200; int inactivePeriod = 200;
int scavengePeriod = 3; int scavengePeriod = 3;
int idlePeriod = 5; int idlePeriod = 5;
((StdErrLog)Log.getLogger(org.eclipse.jetty.server.session.HashedSession.class)).setHideStacks(true); ((StdErrLog)Log.getLogger("org.eclipse.jetty.server.session")).setHideStacks(true);
System.setProperty("org.eclipse.jetty.STACKS", "false"); System.setProperty("org.eclipse.jetty.STACKS", "false");
File storeDir = new File (System.getProperty("java.io.tmpdir"), "idle-test"); File storeDir = new File (System.getProperty("java.io.tmpdir"), "idle-test");
storeDir.deleteOnExit(); storeDir.deleteOnExit();
@ -229,7 +223,7 @@ public class IdleSessionTest
HttpSession session = request.getSession(true); HttpSession session = request.getSession(true);
session.setAttribute("test", "test"); session.setAttribute("test", "test");
originalId = session.getId(); originalId = session.getId();
assertTrue(!((HashedSession)session).isIdled()); // assertTrue(!((HashedSession)session).isIdled());
} }
else if ("test".equals(action)) else if ("test".equals(action))
{ {
@ -237,7 +231,7 @@ public class IdleSessionTest
assertTrue(session != null); assertTrue(session != null);
assertTrue(originalId.equals(session.getId())); assertTrue(originalId.equals(session.getId()));
assertEquals("test", session.getAttribute("test")); assertEquals("test", session.getAttribute("test"));
assertTrue(!((HashedSession)session).isIdled()); // assertTrue(!((HashedSession)session).isIdled());
} }
else if ("testfail".equals(action)) else if ("testfail".equals(action))
{ {

View File

@ -28,47 +28,11 @@ import org.junit.Test;
public class SessionRenewTest extends AbstractSessionRenewTest public class SessionRenewTest extends AbstractSessionRenewTest
{ {
File tmpDir;
@Before
public void before() throws Exception
{
tmpDir = File.createTempFile("hash-session-renew-test", null);
tmpDir.delete();
tmpDir.mkdirs();
tmpDir.deleteOnExit();
}
@After
public void after()
{
IO.delete(tmpDir);
}
@Override @Override
public AbstractTestServer createServer(int port, int max, int scavenge) public AbstractTestServer createServer(int port, int max, int scavenge)
{ {
return new HashTestServer(port, max, scavenge) return new HashTestServer(port, max, scavenge);
{
@Override
public SessionManager newSessionManager()
{
HashSessionManager sessionManager = (HashSessionManager)super.newSessionManager();
sessionManager.setSavePeriod(2);
try
{
sessionManager.setStoreDirectory(tmpDir);
}
catch (Exception e)
{
throw new IllegalStateException(e);
}
return sessionManager;
}
};
} }
@Test @Test

View File

@ -46,7 +46,9 @@ public abstract class AbstractNewSessionTest
{ {
try try
{ {
System.err.println("Sleeping "+(scavenge * 2500L));
Thread.sleep(scavenge * 2500L); Thread.sleep(scavenge * 2500L);
System.err.println("Sleeping "+(scavenge * 2500L));
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {

View File

@ -131,7 +131,7 @@ public abstract class AbstractSessionRenewTest
String beforeSessionId = beforeSession.getId(); String beforeSessionId = beforeSession.getId();
((AbstractSession)beforeSession).renewId(request); ((Session)beforeSession).renewId(request);
HttpSession afterSession = request.getSession(false); HttpSession afterSession = request.getSession(false);
assertTrue(afterSession != null); assertTrue(afterSession != null);
@ -140,7 +140,7 @@ public abstract class AbstractSessionRenewTest
assertTrue(beforeSession==afterSession); assertTrue(beforeSession==afterSession);
assertFalse(beforeSessionId.equals(afterSessionId)); assertFalse(beforeSessionId.equals(afterSessionId));
AbstractSessionManager sessionManager = (AbstractSessionManager)((AbstractSession)afterSession).getSessionManager(); SessionManager sessionManager = ((Session)afterSession).getSessionManager();
AbstractSessionIdManager sessionIdManager = (AbstractSessionIdManager)sessionManager.getSessionIdManager(); AbstractSessionIdManager sessionIdManager = (AbstractSessionIdManager)sessionManager.getSessionIdManager();
assertTrue(sessionIdManager.isIdInUse(afterSessionId)); assertTrue(sessionIdManager.isIdInUse(afterSessionId));
@ -151,7 +151,7 @@ public abstract class AbstractSessionRenewTest
session = sessionManager.getSession(beforeSessionId); session = sessionManager.getSession(beforeSessionId);
assertNull(session); assertNull(session);
if (((AbstractSession)afterSession).isIdChanged()) if (((Session)afterSession).isIdChanged())
{ {
((org.eclipse.jetty.server.Response)response).addCookie(sessionManager.getSessionCookie(afterSession, request.getContextPath(), request.isSecure())); ((org.eclipse.jetty.server.Response)response).addCookie(sessionManager.getSessionCookie(afterSession, request.getContextPath(), request.isSecure()));
} }

View File

@ -42,6 +42,7 @@ public abstract class AbstractTestServer
protected final int _scavengePeriod; protected final int _scavengePeriod;
protected final ContextHandlerCollection _contexts; protected final ContextHandlerCollection _contexts;
protected SessionIdManager _sessionIdManager; protected SessionIdManager _sessionIdManager;
private SessionScavenger _scavenger;
@ -81,6 +82,10 @@ public abstract class AbstractTestServer
_contexts = new ContextHandlerCollection(); _contexts = new ContextHandlerCollection();
_sessionIdManager = newSessionIdManager(sessionIdMgrConfig); _sessionIdManager = newSessionIdManager(sessionIdMgrConfig);
_server.setSessionIdManager(_sessionIdManager); _server.setSessionIdManager(_sessionIdManager);
((AbstractSessionIdManager) _sessionIdManager).setServer(_server);
_scavenger = new SessionScavenger();
_scavenger.setScavengeIntervalSec(scavengePeriod);
((AbstractSessionIdManager)_sessionIdManager).setSessionScavenger(_scavenger);
} }