From 29dec203ba037e081b90214f321f63ab6710ce46 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 8 Jan 2014 14:54:57 -0700 Subject: [PATCH] Turning on some leak detection in websocket testing --- .../server/AnnotatedServerEndpointTest.java | 8 +- .../jsr356/server/BasicEndpointTest.java | 7 +- .../jsr356/server/IdleTimeoutTest.java | 7 +- .../jsr356/server/LargeAnnotatedTest.java | 7 +- .../jsr356/server/LargeContainerTest.java | 7 +- .../jsr356/server/OnMessageReturnTest.java | 7 +- .../websocket/jsr356/server/SessionTest.java | 8 +- .../websocket/client/WebSocketClient.java | 12 ++- .../websocket/client/BadNetworkTest.java | 7 +- .../websocket/client/ClientConnectTest.java | 7 +- .../websocket/client/examples/TestClient.java | 7 +- .../common/GeneratorParserRoundtripTest.java | 10 ++- .../websocket/common/WebSocketFrameTest.java | 19 +++-- .../common/WebSocketRemoteEndpointTest.java | 9 ++- .../websocket/common/events/EventCapture.java | 6 +- .../common/events/EventDriverTest.java | 17 ++-- .../extensions/AbstractExtensionTest.java | 17 ++-- .../common/extensions/ExtensionStackTest.java | 7 +- .../extensions/FragmentExtensionTest.java | 23 +++--- .../compress/DeflateFrameExtensionTest.java | 16 ++-- .../PerMessageDeflateExtensionTest.java | 11 ++- .../common/io/LocalWebSocketConnection.java | 13 ++- .../common/io/LocalWebSocketSession.java | 5 +- .../message/MessageInputStreamTest.java | 13 ++- .../message/MessageOutputStreamTest.java | 7 +- .../common/message/MessageWriterTest.java | 7 +- .../common/test/BlockheadClient.java | 1 + .../common/test/BlockheadServer.java | 1 + .../common/test/LeakTrackingBufferPool.java | 80 +++++++++++++++++++ .../websocket/common/test/UnitGenerator.java | 8 +- .../websocket/common/test/UnitParser.java | 8 +- .../websocket/server/WebSocketHandler.java | 9 ++- .../server/WebSocketServerFactory.java | 11 ++- .../server/WebSocketUpgradeFilter.java | 9 ++- .../WebSocketUpgradeHandlerWrapper.java | 9 ++- .../server/WebSocketOverSSLTest.java | 11 ++- .../websocket/server/ab/AbstractABCase.java | 15 ++-- 37 files changed, 328 insertions(+), 98 deletions(-) create mode 100644 jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/LeakTrackingBufferPool.java diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java index dc65e1c7273..3791fb00cb4 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java @@ -26,11 +26,13 @@ import java.util.Queue; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.jsr356.server.samples.beans.DateDecoder; import org.eclipse.jetty.websocket.jsr356.server.samples.beans.TimeEncoder; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.ConfiguredEchoSocket; @@ -38,6 +40,7 @@ import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoSocketConfigur import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; /** @@ -45,6 +48,9 @@ import org.junit.Test; */ public class AnnotatedServerEndpointTest { + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private static WSServer server; @BeforeClass @@ -72,7 +78,7 @@ public class AnnotatedServerEndpointTest private void assertResponse(String message, String... expectedTexts) throws Exception { - WebSocketClient client = new WebSocketClient(); + WebSocketClient client = new WebSocketClient(bufferPool); try { client.start(); diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java index 176f36d27f8..03359878e97 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java @@ -23,10 +23,12 @@ import java.util.Queue; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.TestingDir; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoEndpoint; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoEndpointConfigContextListener; import org.junit.Assert; @@ -42,6 +44,9 @@ public class BasicEndpointTest @Rule public TestingDir testdir = new TestingDir(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + @Test public void testEcho() throws Exception { @@ -61,7 +66,7 @@ public class BasicEndpointTest wsb.deployWebapp(webapp); // wsb.dump(); - WebSocketClient client = new WebSocketClient(); + WebSocketClient client = new WebSocketClient(bufferPool); try { client.start(); diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java index 8e735ef23a0..db85dc06b78 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java @@ -29,6 +29,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.TestingDir; import org.eclipse.jetty.util.log.Log; @@ -36,6 +37,7 @@ import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.jsr356.server.samples.idletimeout.IdleTimeoutContextListener; import org.eclipse.jetty.websocket.jsr356.server.samples.idletimeout.OnOpenIdleTimeoutEndpoint; import org.eclipse.jetty.websocket.jsr356.server.samples.idletimeout.OnOpenIdleTimeoutSocket; @@ -51,6 +53,9 @@ public class IdleTimeoutTest @Rule public TestingDir testdir = new TestingDir(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private static WSServer server; @BeforeClass @@ -80,7 +85,7 @@ public class IdleTimeoutTest private void assertConnectionTimeout(URI uri) throws Exception, IOException, InterruptedException, ExecutionException, TimeoutException { - WebSocketClient client = new WebSocketClient(); + WebSocketClient client = new WebSocketClient(bufferPool); try { client.start(); diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java index 3db7247cc7d..f663fc210a8 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java @@ -25,10 +25,12 @@ import java.util.Queue; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.TestingDir; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoConfiguredSocket; import org.junit.Assert; import org.junit.Rule; @@ -42,6 +44,9 @@ public class LargeAnnotatedTest @Rule public TestingDir testdir = new TestingDir(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + @Test public void testEcho() throws Exception { @@ -58,7 +63,7 @@ public class LargeAnnotatedTest wsb.deployWebapp(webapp); // wsb.dump(); - WebSocketClient client = new WebSocketClient(); + WebSocketClient client = new WebSocketClient(bufferPool); try { client.getPolicy().setMaxTextMessageSize(128*1024); diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java index 3c47b6e1582..130f7f17ae9 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java @@ -25,10 +25,12 @@ import java.util.Queue; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.TestingDir; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoDefaultSocket; import org.junit.Assert; import org.junit.Rule; @@ -42,6 +44,9 @@ public class LargeContainerTest @Rule public TestingDir testdir = new TestingDir(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + @Test public void testEcho() throws Exception { @@ -58,7 +63,7 @@ public class LargeContainerTest wsb.deployWebapp(webapp); // wsb.dump(); - WebSocketClient client = new WebSocketClient(); + WebSocketClient client = new WebSocketClient(bufferPool); try { client.getPolicy().setMaxTextMessageSize(128*1024); diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java index ec3def09d7e..5185b6acdf2 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java @@ -23,10 +23,12 @@ import java.util.Queue; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.TestingDir; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoReturnEndpoint; import org.junit.Assert; import org.junit.Rule; @@ -37,6 +39,9 @@ public class OnMessageReturnTest @Rule public TestingDir testdir = new TestingDir(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + @Test public void testEchoReturn() throws Exception { @@ -53,7 +58,7 @@ public class OnMessageReturnTest wsb.deployWebapp(webapp); wsb.dump(); - WebSocketClient client = new WebSocketClient(); + WebSocketClient client = new WebSocketClient(bufferPool); try { client.start(); diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java index 9453d3a54fc..24e8cff6a92 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java @@ -28,14 +28,17 @@ import java.util.Queue; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -95,6 +98,9 @@ public class SessionTest return cases; } + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private final Case testcase; private WSServer server; private URI serverUri; @@ -127,7 +133,7 @@ public class SessionTest private void assertResponse(String requestPath, String requestMessage, String expectedResponse) throws Exception { - WebSocketClient client = new WebSocketClient(); + WebSocketClient client = new WebSocketClient(bufferPool); try { client.start(); diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java index 6d2144dd569..b9309f8db82 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java @@ -94,6 +94,11 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen { this(null,executor); } + + public WebSocketClient(ByteBufferPool bufferPool) + { + this(null,null,bufferPool); + } public WebSocketClient(SslContextFactory sslContextFactory) { @@ -101,11 +106,16 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen } public WebSocketClient(SslContextFactory sslContextFactory, Executor executor) + { + this(sslContextFactory,executor,new MappedByteBufferPool()); + } + + public WebSocketClient(SslContextFactory sslContextFactory, Executor executor, ByteBufferPool bufferPool) { this.executor = executor; this.sslContextFactory = sslContextFactory; this.policy = WebSocketPolicy.newClientPolicy(); - this.bufferPool = new MappedByteBufferPool(); + this.bufferPool = bufferPool; this.extensionRegistry = new WebSocketExtensionFactory(policy,bufferPool); this.masker = new RandomMasker(); this.eventDriverFactory = new EventDriverFactory(policy); diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/BadNetworkTest.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/BadNetworkTest.java index 7a342787512..19b740a7e19 100644 --- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/BadNetworkTest.java +++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/BadNetworkTest.java @@ -22,10 +22,12 @@ import java.net.URI; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.common.test.BlockheadServer; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.BlockheadServer.ServerConnection; import org.junit.After; import org.junit.Before; @@ -41,13 +43,16 @@ public class BadNetworkTest @Rule public TestTracker tt = new TestTracker(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private BlockheadServer server; private WebSocketClient client; @Before public void startClient() throws Exception { - client = new WebSocketClient(); + client = new WebSocketClient(bufferPool); client.getPolicy().setIdleTimeout(250); client.start(); } diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java index d4b269f1987..5b41986c2f8 100644 --- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java +++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java @@ -33,12 +33,14 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.OS; import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.UpgradeException; import org.eclipse.jetty.websocket.common.AcceptHash; import org.eclipse.jetty.websocket.common.test.BlockheadServer; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.BlockheadServer.ServerConnection; import org.junit.After; import org.junit.Assert; @@ -55,6 +57,9 @@ public class ClientConnectTest @Rule public TestTracker tt = new TestTracker(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private final int timeout = 500; private BlockheadServer server; private WebSocketClient client; @@ -86,7 +91,7 @@ public class ClientConnectTest @Before public void startClient() throws Exception { - client = new WebSocketClient(); + client = new WebSocketClient(bufferPool); client.setConnectTimeout(timeout); client.start(); } diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java index dfa9fdf7b8c..fe4a99f8899 100644 --- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java +++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java @@ -29,11 +29,13 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketAdapter; import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.common.OpCode; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; /** * This is not a general purpose websocket client. It's only for testing the websocket server and is hardwired to a specific draft version of the protocol. @@ -95,6 +97,8 @@ public class TestClient private static final Random __random = new Random(); + private static LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("TestClient",new MappedByteBufferPool()); + private final String _host; private final int _port; private final String _protocol; @@ -172,7 +176,7 @@ public class TestClient } TestClient[] client = new TestClient[clients]; - WebSocketClient wsclient = new WebSocketClient(); + WebSocketClient wsclient = new WebSocketClient(bufferPool); try { wsclient.start(); @@ -250,6 +254,7 @@ public class TestClient wsclient.stop(); } + bufferPool.assertNoLeaks(); } private static void usage(String[] args) diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/GeneratorParserRoundtripTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/GeneratorParserRoundtripTest.java index 5b4e6789eec..5b6ba00e93b 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/GeneratorParserRoundtripTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/GeneratorParserRoundtripTest.java @@ -18,27 +18,30 @@ package org.eclipse.jetty.websocket.common; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import java.nio.ByteBuffer; import java.util.Arrays; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.common.frames.TextFrame; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.IncomingFramesCapture; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; public class GeneratorParserRoundtripTest { + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + @Test public void testParserAndGenerator() throws Exception { WebSocketPolicy policy = WebSocketPolicy.newClientPolicy(); - ByteBufferPool bufferPool = new MappedByteBufferPool(); Generator gen = new Generator(policy,bufferPool); Parser parser = new Parser(policy,bufferPool); IncomingFramesCapture capture = new IncomingFramesCapture(); @@ -77,7 +80,6 @@ public class GeneratorParserRoundtripTest @Test public void testParserAndGeneratorMasked() throws Exception { - ByteBufferPool bufferPool = new MappedByteBufferPool(); Generator gen = new Generator(WebSocketPolicy.newClientPolicy(),bufferPool); Parser parser = new Parser(WebSocketPolicy.newServerPolicy(),bufferPool); IncomingFramesCapture capture = new IncomingFramesCapture(); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java index 3e864a5eaf2..c359a6a3460 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketFrameTest.java @@ -18,11 +18,10 @@ package org.eclipse.jetty.websocket.common; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import java.nio.ByteBuffer; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.api.StatusCode; @@ -31,15 +30,20 @@ import org.eclipse.jetty.websocket.api.extensions.Frame; import org.eclipse.jetty.websocket.common.frames.CloseFrame; import org.eclipse.jetty.websocket.common.frames.PingFrame; import org.eclipse.jetty.websocket.common.frames.TextFrame; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.util.Hex; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; public class WebSocketFrameTest { - private static Generator strictGenerator; - private static Generator laxGenerator; + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + + private Generator strictGenerator; + private Generator laxGenerator; private ByteBuffer generateWholeFrame(Generator generator, Frame frame) { @@ -49,11 +53,10 @@ public class WebSocketFrameTest return buf; } - @BeforeClass - public static void initGenerator() + @Before + public void initGenerator() { WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); - ByteBufferPool bufferPool = new MappedByteBufferPool(); strictGenerator = new Generator(policy,bufferPool); laxGenerator = new Generator(policy,bufferPool,false); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketRemoteEndpointTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketRemoteEndpointTest.java index 2c5336eaf67..a78b7e68703 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketRemoteEndpointTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/WebSocketRemoteEndpointTest.java @@ -23,7 +23,9 @@ import static org.hamcrest.Matchers.containsString; import java.io.IOException; import java.nio.ByteBuffer; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.websocket.common.io.LocalWebSocketConnection; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.OutgoingFramesCapture; import org.junit.Assert; import org.junit.Rule; @@ -35,10 +37,13 @@ public class WebSocketRemoteEndpointTest @Rule public TestName testname = new TestName(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + @Test public void testTextBinaryText() throws IOException { - LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); + LocalWebSocketConnection conn = new LocalWebSocketConnection(testname,bufferPool); OutgoingFramesCapture outgoing = new OutgoingFramesCapture(); WebSocketRemoteEndpoint remote = new WebSocketRemoteEndpoint(conn,outgoing); conn.connect(); @@ -68,7 +73,7 @@ public class WebSocketRemoteEndpointTest @Test public void testTextPingText() throws IOException { - LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); + LocalWebSocketConnection conn = new LocalWebSocketConnection(testname,bufferPool); OutgoingFramesCapture outgoing = new OutgoingFramesCapture(); WebSocketRemoteEndpoint remote = new WebSocketRemoteEndpoint(conn,outgoing); conn.connect(); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventCapture.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventCapture.java index 6fa5df377e3..38451bb3835 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventCapture.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventCapture.java @@ -25,11 +25,15 @@ import static org.hamcrest.Matchers.startsWith; import java.util.regex.Pattern; import org.eclipse.jetty.toolchain.test.EventQueue; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; import org.junit.Assert; @SuppressWarnings("serial") public class EventCapture extends EventQueue { + private static final Logger LOG = Log.getLogger(EventCapture.class); + public static class Assertable { private final String event; @@ -63,7 +67,7 @@ public class EventCapture extends EventQueue public void add(String format, Object... args) { String msg = String.format(format,args); - System.err.printf("### EVENT: %s%n",msg); + LOG.debug("EVENT: {}",msg); super.offer(msg); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java index dce54a91f81..fd837b45279 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common.events; import java.io.IOException; import java.util.concurrent.TimeoutException; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.WebSocketException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; @@ -30,6 +31,7 @@ import org.eclipse.jetty.websocket.common.frames.BinaryFrame; import org.eclipse.jetty.websocket.common.frames.PingFrame; import org.eclipse.jetty.websocket.common.frames.TextFrame; import org.eclipse.jetty.websocket.common.io.LocalWebSocketSession; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; @@ -46,6 +48,9 @@ public class EventDriverTest @Rule public TestName testname = new TestName(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private Frame makeBinaryFrame(String content, boolean fin) { return new BinaryFrame().setPayload(content).setFin(fin); @@ -57,7 +62,7 @@ public class EventDriverTest AdapterConnectCloseSocket socket = new AdapterConnectCloseSocket(); EventDriver driver = wrap(socket); - try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver)) + try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver,bufferPool)) { conn.open(); driver.incomingFrame(new CloseInfo(StatusCode.NORMAL).asFrame()); @@ -74,7 +79,7 @@ public class EventDriverTest AnnotatedBinaryArraySocket socket = new AnnotatedBinaryArraySocket(); EventDriver driver = wrap(socket); - try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver)) + try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver,bufferPool)) { conn.open(); driver.incomingFrame(makeBinaryFrame("Hello World",true)); @@ -93,7 +98,7 @@ public class EventDriverTest AnnotatedTextSocket socket = new AnnotatedTextSocket(); EventDriver driver = wrap(socket); - try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver)) + try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver,bufferPool)) { conn.open(); driver.incomingError(new WebSocketException("oof")); @@ -112,7 +117,7 @@ public class EventDriverTest AnnotatedFramesSocket socket = new AnnotatedFramesSocket(); EventDriver driver = wrap(socket); - try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver)) + try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver,bufferPool)) { conn.open(); driver.incomingFrame(new PingFrame().setPayload("PING")); @@ -136,7 +141,7 @@ public class EventDriverTest AnnotatedBinaryStreamSocket socket = new AnnotatedBinaryStreamSocket(); EventDriver driver = wrap(socket); - try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver)) + try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver,bufferPool)) { conn.open(); driver.incomingFrame(makeBinaryFrame("Hello World",true)); @@ -155,7 +160,7 @@ public class EventDriverTest ListenerBasicSocket socket = new ListenerBasicSocket(); EventDriver driver = wrap(socket); - try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver)) + try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver,bufferPool)) { conn.start(); conn.open(); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/AbstractExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/AbstractExtensionTest.java index 9801cd1e9e0..97e3bf04cf5 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/AbstractExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/AbstractExtensionTest.java @@ -18,10 +18,10 @@ package org.eclipse.jetty.websocket.common.extensions; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.websocket.api.WebSocketPolicy; -import org.junit.BeforeClass; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; +import org.junit.Before; import org.junit.Rule; import org.junit.rules.TestName; @@ -29,15 +29,16 @@ public abstract class AbstractExtensionTest { @Rule public TestName testname = new TestName(); + + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); - private static ByteBufferPool bufferPool; - protected static ExtensionTool clientExtensions; - protected static ExtensionTool serverExtensions; + protected ExtensionTool clientExtensions; + protected ExtensionTool serverExtensions; - @BeforeClass - public static void init() + @Before + public void init() { - bufferPool = new MappedByteBufferPool(); clientExtensions = new ExtensionTool(WebSocketPolicy.newClientPolicy(),bufferPool); serverExtensions = new ExtensionTool(WebSocketPolicy.newServerPolicy(),bufferPool); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java index 6a38af6c364..1863169f7e5 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/ExtensionStackTest.java @@ -24,7 +24,6 @@ import java.util.ArrayList; import java.util.List; import org.eclipse.jetty.io.ArrayByteBufferPool; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.api.WebSocketPolicy; @@ -32,12 +31,17 @@ import org.eclipse.jetty.websocket.api.extensions.Extension; import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig; import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory; import org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; public class ExtensionStackTest { private static final Logger LOG = Log.getLogger(ExtensionStackTest.class); + + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new ArrayByteBufferPool()); @SuppressWarnings("unchecked") private T assertIsExtension(String msg, Object obj, Class clazz) @@ -53,7 +57,6 @@ public class ExtensionStackTest private ExtensionStack createExtensionStack() { WebSocketPolicy policy = WebSocketPolicy.newClientPolicy(); - ByteBufferPool bufferPool = new ArrayByteBufferPool(); ExtensionFactory factory = new WebSocketExtensionFactory(policy,bufferPool); return new ExtensionStack(factory); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java index 9243332a918..b022c38e4aa 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java @@ -18,7 +18,7 @@ package org.eclipse.jetty.websocket.common.extensions; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import java.io.IOException; import java.nio.ByteBuffer; @@ -40,12 +40,17 @@ import org.eclipse.jetty.websocket.common.frames.PingFrame; import org.eclipse.jetty.websocket.common.frames.TextFrame; import org.eclipse.jetty.websocket.common.test.ByteBufferAssert; import org.eclipse.jetty.websocket.common.test.IncomingFramesCapture; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.OutgoingFramesCapture; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; public class FragmentExtensionTest { + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + /** * Verify that incoming frames are passed thru without modification */ @@ -55,7 +60,7 @@ public class FragmentExtensionTest IncomingFramesCapture capture = new IncomingFramesCapture(); FragmentExtension ext = new FragmentExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newClientPolicy()); ExtensionConfig config = ExtensionConfig.parse("fragment;maxLength=4"); ext.setConfig(config); @@ -107,7 +112,7 @@ public class FragmentExtensionTest IncomingFramesCapture capture = new IncomingFramesCapture(); FragmentExtension ext = new FragmentExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("fragment;maxLength=4"); ext.setConfig(config); @@ -142,7 +147,7 @@ public class FragmentExtensionTest OutgoingFramesCapture capture = new OutgoingFramesCapture(); FragmentExtension ext = new FragmentExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("fragment;maxLength=20"); ext.setConfig(config); @@ -173,7 +178,7 @@ public class FragmentExtensionTest expectedFrames.add(new TextFrame().setPayload("-- Albert Einstein").setFin(true)); - capture.dump(); + // capture.dump(); int len = expectedFrames.size(); capture.assertFrameCount(len); @@ -186,8 +191,8 @@ public class FragmentExtensionTest WebSocketFrame actualFrame = frames.get(i); WebSocketFrame expectedFrame = expectedFrames.get(i); - System.out.printf("actual: %s%n",actualFrame); - System.out.printf("expect: %s%n",expectedFrame); + // System.out.printf("actual: %s%n",actualFrame); + // System.out.printf("expect: %s%n",expectedFrame); // Validate Frame Assert.assertThat(prefix + ".opcode",actualFrame.getOpCode(),is(expectedFrame.getOpCode())); @@ -214,7 +219,7 @@ public class FragmentExtensionTest OutgoingFramesCapture capture = new OutgoingFramesCapture(); FragmentExtension ext = new FragmentExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("fragment"); ext.setConfig(config); @@ -278,7 +283,7 @@ public class FragmentExtensionTest OutgoingFramesCapture capture = new OutgoingFramesCapture(); FragmentExtension ext = new FragmentExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("fragment;maxLength=4"); ext.setConfig(config); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java index 55a00cd2302..d8dc65e351b 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java @@ -28,7 +28,6 @@ import java.util.List; import java.util.zip.Deflater; import java.util.zip.Inflater; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -45,19 +44,24 @@ import org.eclipse.jetty.websocket.common.extensions.ExtensionTool.Tester; import org.eclipse.jetty.websocket.common.frames.TextFrame; import org.eclipse.jetty.websocket.common.test.ByteBufferAssert; import org.eclipse.jetty.websocket.common.test.IncomingFramesCapture; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.OutgoingNetworkBytesCapture; import org.eclipse.jetty.websocket.common.test.UnitParser; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; public class DeflateFrameExtensionTest extends AbstractExtensionTest { + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private void assertIncoming(byte[] raw, String... expectedTextDatas) { WebSocketPolicy policy = WebSocketPolicy.newClientPolicy(); DeflateFrameExtension ext = new DeflateFrameExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(policy); ExtensionConfig config = ExtensionConfig.parse("deflate-frame"); @@ -101,13 +105,12 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest WebSocketPolicy policy = WebSocketPolicy.newClientPolicy(); DeflateFrameExtension ext = new DeflateFrameExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(policy); ExtensionConfig config = ExtensionConfig.parse("deflate-frame"); ext.setConfig(config); - ByteBufferPool bufferPool = new MappedByteBufferPool(); boolean validating = true; Generator generator = new Generator(policy,bufferPool,validating); generator.configureFromExtensions(Collections.singletonList(ext)); @@ -235,7 +238,7 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest private void init(DeflateFrameExtension ext) { ext.setConfig(new ExtensionConfig(ext.getName())); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); } @Test @@ -290,11 +293,10 @@ public class DeflateFrameExtensionTest extends AbstractExtensionTest WebSocketPolicy policy = WebSocketPolicy.newClientPolicy(); DeflateFrameExtension ext = new DeflateFrameExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(policy); ext.setConfig(new ExtensionConfig(ext.getName())); - ByteBufferPool bufferPool = new MappedByteBufferPool(); boolean validating = true; Generator generator = new Generator(policy,bufferPool,validating); generator.configureFromExtensions(Collections.singletonList(ext)); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java index 199a61befac..8d14481b4eb 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java @@ -40,8 +40,10 @@ import org.eclipse.jetty.websocket.common.frames.PingFrame; import org.eclipse.jetty.websocket.common.frames.TextFrame; import org.eclipse.jetty.websocket.common.test.ByteBufferAssert; import org.eclipse.jetty.websocket.common.test.IncomingFramesCapture; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.OutgoingFramesCapture; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; /** @@ -51,6 +53,9 @@ import org.junit.Test; */ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest { + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + /** * Decode payload example as seen in draft-ietf-hybi-permessage-compression-15. *

@@ -206,7 +211,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest public void testIncomingPing() { PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ext.setConfig(config); @@ -243,7 +248,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest public void testIncomingUncompressedFrames() { PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ext.setConfig(config); @@ -298,7 +303,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest public void testOutgoingPing() throws IOException { PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); - ext.setBufferPool(new MappedByteBufferPool()); + ext.setBufferPool(bufferPool); ext.setPolicy(WebSocketPolicy.newServerPolicy()); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ext.setConfig(config); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java index 2b5f4aec961..8d7b094ef69 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java @@ -22,7 +22,6 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executor; import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.thread.ExecutorThreadPool; @@ -49,22 +48,22 @@ public class LocalWebSocketConnection implements LogicalConnection, IncomingFram private IncomingFrames incoming; private IOState ioState = new IOState(); - public LocalWebSocketConnection() + public LocalWebSocketConnection(ByteBufferPool bufferPool) { - this("anon"); + this("anon",bufferPool); } - public LocalWebSocketConnection(String id) + public LocalWebSocketConnection(String id, ByteBufferPool bufferPool) { this.id = id; - this.bufferPool = new MappedByteBufferPool(); + this.bufferPool = bufferPool; this.executor = new ExecutorThreadPool(); this.ioState.addListener(this); } - public LocalWebSocketConnection(TestName testname) + public LocalWebSocketConnection(TestName testname, ByteBufferPool bufferPool) { - this(testname.getMethodName()); + this(testname.getMethodName(),bufferPool); } @Override diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketSession.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketSession.java index 1de1ced590c..6bf9224d1f6 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketSession.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketSession.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.common.io; import java.net.URI; +import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.websocket.common.SessionListener; import org.eclipse.jetty.websocket.common.WebSocketSession; import org.eclipse.jetty.websocket.common.events.EventDriver; @@ -31,9 +32,9 @@ public class LocalWebSocketSession extends WebSocketSession private String id; private OutgoingFramesCapture outgoingCapture; - public LocalWebSocketSession(TestName testname, EventDriver driver) + public LocalWebSocketSession(TestName testname, EventDriver driver, ByteBufferPool bufferPool) { - super(URI.create("ws://localhost/LocalWebSocketSesssion/" + testname.getMethodName()),driver,new LocalWebSocketConnection(testname), new SessionListener[0]); + super(URI.create("ws://localhost/LocalWebSocketSesssion/" + testname.getMethodName()),driver,new LocalWebSocketConnection(testname,bufferPool), new SessionListener[0]); this.id = testname.getMethodName(); outgoingCapture = new OutgoingFramesCapture(); setOutgoingHandler(outgoingCapture); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageInputStreamTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageInputStreamTest.java index 91cb416d0b7..e1dfc5f9d87 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageInputStreamTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageInputStreamTest.java @@ -27,8 +27,10 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.common.io.LocalWebSocketConnection; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -39,10 +41,13 @@ public class MessageInputStreamTest @Rule public TestName testname = new TestName(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + @Test(timeout=10000) public void testBasicAppendRead() throws IOException { - LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); + LocalWebSocketConnection conn = new LocalWebSocketConnection(testname,bufferPool); try (MessageInputStream stream = new MessageInputStream(conn)) { @@ -65,7 +70,7 @@ public class MessageInputStreamTest @Test(timeout=5000) public void testBlockOnRead() throws Exception { - LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); + LocalWebSocketConnection conn = new LocalWebSocketConnection(testname,bufferPool); try (MessageInputStream stream = new MessageInputStream(conn)) { @@ -116,7 +121,7 @@ public class MessageInputStreamTest @Test(timeout=10000) public void testBlockOnReadInitial() throws IOException { - LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); + LocalWebSocketConnection conn = new LocalWebSocketConnection(testname,bufferPool); try (MessageInputStream stream = new MessageInputStream(conn)) { @@ -155,7 +160,7 @@ public class MessageInputStreamTest @Test(timeout=10000) public void testReadByteNoBuffersClosed() throws IOException { - LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); + LocalWebSocketConnection conn = new LocalWebSocketConnection(testname,bufferPool); try (MessageInputStream stream = new MessageInputStream(conn)) { diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageOutputStreamTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageOutputStreamTest.java index 408b216e349..feb4a859928 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageOutputStreamTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageOutputStreamTest.java @@ -24,6 +24,7 @@ import static org.hamcrest.Matchers.is; import java.util.Arrays; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -33,6 +34,7 @@ import org.eclipse.jetty.websocket.common.events.EventDriver; import org.eclipse.jetty.websocket.common.events.EventDriverFactory; import org.eclipse.jetty.websocket.common.io.FramePipes; import org.eclipse.jetty.websocket.common.io.LocalWebSocketSession; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -50,6 +52,9 @@ public class MessageOutputStreamTest @Rule public TestName testname = new TestName(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private WebSocketPolicy policy; private TrackingSocket socket; private LocalWebSocketSession session; @@ -77,7 +82,7 @@ public class MessageOutputStreamTest socket = new TrackingSocket("remote"); OutgoingFrames socketPipe = FramePipes.to(factory.wrap(socket)); - session = new LocalWebSocketSession(testname,driver); + session = new LocalWebSocketSession(testname,driver,bufferPool); session.setPolicy(policy); // talk to our remote socket diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageWriterTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageWriterTest.java index ec1ae223b0c..43f68ecf00a 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageWriterTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/message/MessageWriterTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.is; import java.util.Arrays; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -31,6 +32,7 @@ import org.eclipse.jetty.websocket.common.events.EventDriver; import org.eclipse.jetty.websocket.common.events.EventDriverFactory; import org.eclipse.jetty.websocket.common.io.FramePipes; import org.eclipse.jetty.websocket.common.io.LocalWebSocketSession; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -48,6 +50,9 @@ public class MessageWriterTest @Rule public TestName testname = new TestName(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private WebSocketPolicy policy; private TrackingSocket socket; private LocalWebSocketSession session; @@ -75,7 +80,7 @@ public class MessageWriterTest socket = new TrackingSocket("remote"); OutgoingFrames socketPipe = FramePipes.to(factory.wrap(socket)); - session = new LocalWebSocketSession(testname,driver); + session = new LocalWebSocketSession(testname,driver,bufferPool); session.setPolicy(policy); // talk to our remote socket diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java index 21ab83e6050..349b7b49e20 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClient.java @@ -133,6 +133,7 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames, Connecti LOG.debug("WebSocket URI: {}",destWebsocketURI); LOG.debug(" HTTP URI: {}",destHttpURI); + // This is a blockhead client, no point tracking leaks on this object. this.bufferPool = new MappedByteBufferPool(8192); this.generator = new Generator(policy,bufferPool); this.parser = new Parser(policy,bufferPool); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadServer.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadServer.java index c1c46a80cf7..aa23cbde085 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadServer.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadServer.java @@ -102,6 +102,7 @@ public class BlockheadServer this.policy = WebSocketPolicy.newServerPolicy(); this.policy.setMaxBinaryMessageSize(100000); this.policy.setMaxTextMessageSize(100000); + // This is a blockhead server connection, no point tracking leaks on this object. this.bufferPool = new MappedByteBufferPool(BUFFER_SIZE); this.parser = new Parser(policy,bufferPool); this.parseCount = new AtomicInteger(0); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/LeakTrackingBufferPool.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/LeakTrackingBufferPool.java new file mode 100644 index 00000000000..76172d978ce --- /dev/null +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/LeakTrackingBufferPool.java @@ -0,0 +1,80 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 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.websocket.common.test; + +import static org.hamcrest.Matchers.*; + +import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicInteger; + +import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.LeakTrackingByteBufferPool; +import org.eclipse.jetty.util.LeakDetector; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; +import org.junit.Assert; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +public class LeakTrackingBufferPool extends LeakTrackingByteBufferPool implements TestRule +{ + private static final Logger LOG = Log.getLogger(LeakTrackingBufferPool.class); + private final String id; + private AtomicInteger leakCount = new AtomicInteger(0); + + public LeakTrackingBufferPool(String id, ByteBufferPool delegate) + { + super(delegate); + this.id = id; + } + + @Override + protected void leaked(LeakDetector.LeakInfo leakInfo) + { + String msg = String.format("%s ByteBuffer %s leaked at:",id,leakInfo.getResourceDescription()); + LOG.warn(msg,leakInfo.getStackFrames()); + leakCount.incrementAndGet(); + } + + public void assertNoLeaks() + { + Assert.assertThat("Leak Count for [" + id + "]",leakCount.get(),is(0)); + } + + public void clearTracking() + { + leakCount.set(0); + } + + @Override + public Statement apply(final Statement statement, Description description) + { + return new Statement() + { + @Override + public void evaluate() throws Throwable + { + clearTracking(); + statement.evaluate(); + assertNoLeaks(); + } + }; + } +} diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitGenerator.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitGenerator.java index c4628982c46..1dcf19d9d6b 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitGenerator.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitGenerator.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common.test; import java.nio.ByteBuffer; import java.util.List; +import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.log.Log; @@ -122,6 +123,11 @@ public class UnitGenerator extends Generator public UnitGenerator() { - super(WebSocketPolicy.newServerPolicy(),new MappedByteBufferPool()); + super(WebSocketPolicy.newServerPolicy(),new LeakTrackingBufferPool("UnitGenerator",new MappedByteBufferPool())); + } + + public UnitGenerator(ByteBufferPool bufferPool) + { + super(WebSocketPolicy.newServerPolicy(),bufferPool); } } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitParser.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitParser.java index ac15144b0db..d6a34eaee20 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitParser.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/UnitParser.java @@ -20,7 +20,6 @@ package org.eclipse.jetty.websocket.common.test; import java.nio.ByteBuffer; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.log.StacklessLogging; import org.eclipse.jetty.websocket.api.WebSocketPolicy; @@ -33,14 +32,9 @@ public class UnitParser extends Parser this(WebSocketPolicy.newServerPolicy()); } - public UnitParser(ByteBufferPool bufferPool, WebSocketPolicy policy) - { - super(policy,bufferPool); - } - public UnitParser(WebSocketPolicy policy) { - this(new MappedByteBufferPool(),policy); + super(policy,new LeakTrackingBufferPool("UnitParser",new MappedByteBufferPool())); } private void parsePartial(ByteBuffer buf, int numBytes) diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketHandler.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketHandler.java index bd026ced602..daf0c588a3f 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketHandler.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketHandler.java @@ -24,6 +24,8 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.HandlerWrapper; import org.eclipse.jetty.websocket.api.WebSocketBehavior; @@ -55,10 +57,15 @@ public abstract class WebSocketHandler extends HandlerWrapper private final WebSocketServletFactory webSocketFactory; public WebSocketHandler() + { + this(new MappedByteBufferPool()); + } + + public WebSocketHandler(ByteBufferPool bufferPool) { WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); configurePolicy(policy); - webSocketFactory = new WebSocketServerFactory(policy); + webSocketFactory = new WebSocketServerFactory(policy, bufferPool); addBean(webSocketFactory); } diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java index 6e2e624ffbe..46ab21e95d9 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java @@ -96,6 +96,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc private final String supportedVersions; private final WebSocketPolicy defaultPolicy; private final EventDriverFactory eventDriverFactory; + private final ByteBufferPool bufferPool; private final WebSocketExtensionFactory extensionFactory; private List sessionFactories; private Set openSessions = new CopyOnWriteArraySet<>(); @@ -111,6 +112,11 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc { this(policy,new MappedByteBufferPool()); } + + public WebSocketServerFactory(ByteBufferPool bufferPool) + { + this(WebSocketPolicy.newServerPolicy(),bufferPool); + } public WebSocketServerFactory(WebSocketPolicy policy, ByteBufferPool bufferPool) { @@ -121,7 +127,8 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc this.defaultPolicy = policy; this.eventDriverFactory = new EventDriverFactory(defaultPolicy); - this.extensionFactory = new WebSocketExtensionFactory(defaultPolicy,bufferPool); + this.bufferPool = bufferPool; + this.extensionFactory = new WebSocketExtensionFactory(defaultPolicy,this.bufferPool); this.sessionFactories = new ArrayList<>(); this.sessionFactories.add(new WebSocketSessionFactory(this)); this.creator = this; @@ -228,7 +235,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc @Override public WebSocketServletFactory createFactory(WebSocketPolicy policy) { - return new WebSocketServerFactory(policy); + return new WebSocketServerFactory(policy,bufferPool); } private WebSocketSession createSession(URI requestURI, EventDriver websocket, LogicalConnection connection) diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java index de3acbad635..76b7b06ad06 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java @@ -31,6 +31,8 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.util.annotation.ManagedAttribute; @@ -77,7 +79,12 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter public WebSocketUpgradeFilter(WebSocketPolicy policy) { - factory = new WebSocketServerFactory(policy); + this(policy, new MappedByteBufferPool()); + } + + public WebSocketUpgradeFilter(WebSocketPolicy policy, ByteBufferPool bufferPool) + { + factory = new WebSocketServerFactory(policy,bufferPool); addBean(factory,true); } diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeHandlerWrapper.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeHandlerWrapper.java index df406a3eb7d..c4bcc17ba2c 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeHandlerWrapper.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeHandlerWrapper.java @@ -24,6 +24,8 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.io.ByteBufferPool; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.HandlerWrapper; import org.eclipse.jetty.websocket.server.pathmap.PathMappings; @@ -38,7 +40,12 @@ public class WebSocketUpgradeHandlerWrapper extends HandlerWrapper implements Ma public WebSocketUpgradeHandlerWrapper() { - factory = new WebSocketServerFactory(); + this(new MappedByteBufferPool()); + } + + public WebSocketUpgradeHandlerWrapper(ByteBufferPool bufferPool) + { + factory = new WebSocketServerFactory(bufferPool); } @Override diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java index f70c723806b..34b5e40ac31 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java @@ -24,10 +24,12 @@ import java.net.URI; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.toolchain.test.EventQueue; import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.server.helper.CaptureSocket; import org.eclipse.jetty.websocket.server.helper.SessionServlet; import org.junit.AfterClass; @@ -41,6 +43,9 @@ public class WebSocketOverSSLTest @Rule public TestTracker tracker = new TestTracker(); + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + private static SimpleServletServer server; @BeforeClass @@ -64,7 +69,7 @@ public class WebSocketOverSSLTest public void testEcho() throws Exception { Assert.assertThat("server scheme",server.getServerUri().getScheme(),is("wss")); - WebSocketClient client = new WebSocketClient(server.getSslContextFactory()); + WebSocketClient client = new WebSocketClient(server.getSslContextFactory(),null,bufferPool); try { client.start(); @@ -102,7 +107,7 @@ public class WebSocketOverSSLTest public void testServerSessionIsSecure() throws Exception { Assert.assertThat("server scheme",server.getServerUri().getScheme(),is("wss")); - WebSocketClient client = new WebSocketClient(server.getSslContextFactory()); + WebSocketClient client = new WebSocketClient(server.getSslContextFactory(),null,bufferPool); try { client.setConnectTimeout(3000); @@ -140,7 +145,7 @@ public class WebSocketOverSSLTest public void testServerSessionRequestURI() throws Exception { Assert.assertThat("server scheme",server.getServerUri().getScheme(),is("wss")); - WebSocketClient client = new WebSocketClient(server.getSslContextFactory()); + WebSocketClient client = new WebSocketClient(server.getSslContextFactory(),null,bufferPool); try { client.setConnectTimeout(3000); diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java index 22609df6740..1b282b93026 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java @@ -21,7 +21,6 @@ package org.eclipse.jetty.websocket.server.ab; import java.net.URI; import java.nio.ByteBuffer; -import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -31,9 +30,11 @@ import org.eclipse.jetty.websocket.common.Generator; import org.eclipse.jetty.websocket.common.OpCode; import org.eclipse.jetty.websocket.common.WebSocketFrame; import org.eclipse.jetty.websocket.common.test.Fuzzed; +import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPool; import org.eclipse.jetty.websocket.common.test.RawFrameBuilder; import org.eclipse.jetty.websocket.server.SimpleServletServer; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.rules.TestName; @@ -73,15 +74,17 @@ public abstract class AbstractABCase implements Fuzzed protected static final byte[] MASK = { 0x12, 0x34, 0x56, 0x78 }; - protected static Generator strictGenerator; - protected static Generator laxGenerator; + protected Generator strictGenerator; + protected Generator laxGenerator; protected static SimpleServletServer server; - @BeforeClass - public static void initGenerators() + @Rule + public LeakTrackingBufferPool bufferPool = new LeakTrackingBufferPool("Test",new MappedByteBufferPool()); + + @Before + public void initGenerators() { WebSocketPolicy policy = WebSocketPolicy.newClientPolicy(); - ByteBufferPool bufferPool = new MappedByteBufferPool(); strictGenerator = new Generator(policy,bufferPool,true); laxGenerator = new Generator(policy,bufferPool,false); }