Issue #5832 - Improve testing for WebSocket client shutdown.
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
ed86361581
commit
47f24db0be
|
@ -67,5 +67,4 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
|
|||
LifeCycle.start(clientContainer);
|
||||
return clientContainer;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,15 @@ import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientCon
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>This manages the LifeCycle of {@link javax.websocket.WebSocketContainer} instances that are created with
|
||||
* {@link javax.websocket.ContainerProvider}, if this code is being run from another ServletContainer, or if run inside a
|
||||
* Jetty Server with the WebSocket client classes provided by the webapp.</p>
|
||||
*
|
||||
* <p>This mechanism will not work if run with embedded Jetty or if the WebSocket client classes are provided by the server.
|
||||
* In this case then the client {@link javax.websocket.WebSocketContainer} will register itself to be automatically shutdown
|
||||
* with the Jetty {@code ContextHandler}.</p>
|
||||
*/
|
||||
public class JavaxWebSocketShutdownContainer extends ContainerLifeCycle implements ServletContainerInitializer, ServletContextListener
|
||||
{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JavaxWebSocketShutdownContainer.class);
|
||||
|
@ -55,4 +64,10 @@ public class JavaxWebSocketShutdownContainer extends ContainerLifeCycle implemen
|
|||
removeBeans();
|
||||
JavaxWebSocketClientContainer.setShutdownContainer(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.format("%s@%x{%s, size=%s}", getClass().getSimpleName(), hashCode(), getState(), getBeans().size());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,7 +71,6 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
|
|||
protected WebSocketCoreClient coreClient;
|
||||
protected Function<WebSocketComponents, WebSocketCoreClient> coreClientFactory;
|
||||
private final JavaxWebSocketClientFrameHandlerFactory frameHandlerFactory;
|
||||
private boolean allowShutdownWithContextHandler = true;
|
||||
|
||||
public JavaxWebSocketClientContainer()
|
||||
{
|
||||
|
@ -102,11 +101,6 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
|
|||
this.frameHandlerFactory = new JavaxWebSocketClientFrameHandlerFactory(this);
|
||||
}
|
||||
|
||||
public void allowShutdownWithContextHandler(boolean allowShutdownWithContextHandler)
|
||||
{
|
||||
this.allowShutdownWithContextHandler = allowShutdownWithContextHandler;
|
||||
}
|
||||
|
||||
protected HttpClient getHttpClient()
|
||||
{
|
||||
return getWebSocketCoreClient().getHttpClient();
|
||||
|
@ -304,7 +298,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
|
|||
LOG.debug("doClientStart() {}", this);
|
||||
|
||||
// If we are running in Jetty register shutdown with the ContextHandler.
|
||||
if (allowShutdownWithContextHandler && addToContextHandler())
|
||||
if (addToContextHandler())
|
||||
{
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("Shutdown registered with ContextHandler");
|
||||
|
|
|
@ -17,7 +17,9 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Random;
|
||||
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
|
@ -46,7 +48,16 @@ public class WSServer extends LocalServer implements LocalFuzzer.Provider
|
|||
{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WSServer.class);
|
||||
private final Path testDir;
|
||||
private ContextHandlerCollection contexts = new ContextHandlerCollection();
|
||||
private final ContextHandlerCollection contexts = new ContextHandlerCollection();
|
||||
|
||||
public WSServer()
|
||||
{
|
||||
String baseDirName = Long.toString(Math.abs(new Random().nextLong()));
|
||||
this.testDir = MavenTestingUtils.getTargetTestingPath(baseDirName);
|
||||
if (Files.exists(testDir))
|
||||
throw new IllegalStateException("TestDir already exists.");
|
||||
FS.ensureDirExists(testDir);
|
||||
}
|
||||
|
||||
public WSServer(Path testDir)
|
||||
{
|
||||
|
|
|
@ -28,8 +28,6 @@ import org.eclipse.jetty.server.Server;
|
|||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.component.LifeCycle;
|
||||
import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketShutdownContainer;
|
||||
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
|
@ -42,14 +40,13 @@ import static org.hamcrest.Matchers.instanceOf;
|
|||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class JavaxClientShutdownWithServerTest
|
||||
public class JavaxClientShutdownWithServerEmbeddedTest
|
||||
{
|
||||
private Server server;
|
||||
private ServletContextHandler contextHandler;
|
||||
private URI serverUri;
|
||||
private HttpClient httpClient;
|
||||
private volatile WebSocketContainer container;
|
||||
private ContainerLifeCycle shutdownContainer;
|
||||
|
||||
public class ContextHandlerShutdownServlet extends HttpServlet
|
||||
{
|
||||
|
@ -60,18 +57,6 @@ public class JavaxClientShutdownWithServerTest
|
|||
}
|
||||
}
|
||||
|
||||
public class ServletContainerInitializerShutdownServlet extends HttpServlet
|
||||
{
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
{
|
||||
JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer();
|
||||
clientContainer.allowShutdownWithContextHandler(false);
|
||||
LifeCycle.start(clientContainer);
|
||||
container = clientContainer;
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void before() throws Exception
|
||||
{
|
||||
|
@ -81,13 +66,11 @@ public class JavaxClientShutdownWithServerTest
|
|||
|
||||
contextHandler = new ServletContextHandler();
|
||||
contextHandler.setContextPath("/");
|
||||
contextHandler.addServlet(new ServletHolder(new ContextHandlerShutdownServlet()), "/contextHandler");
|
||||
contextHandler.addServlet(new ServletHolder(new ServletContainerInitializerShutdownServlet()), "/shutdownContainer");
|
||||
contextHandler.addServlet(new ServletHolder(new ContextHandlerShutdownServlet()), "/");
|
||||
server.setHandler(contextHandler);
|
||||
|
||||
// Because we are using embedded we must manually add the Javax WS Client Shutdown SCI.
|
||||
JavaxWebSocketShutdownContainer javaxWebSocketClientShutdown = new JavaxWebSocketShutdownContainer();
|
||||
shutdownContainer = javaxWebSocketClientShutdown;
|
||||
contextHandler.addServletContainerInitializer(javaxWebSocketClientShutdown);
|
||||
|
||||
server.start();
|
||||
|
@ -107,7 +90,7 @@ public class JavaxClientShutdownWithServerTest
|
|||
@Test
|
||||
public void testShutdownWithContextHandler() throws Exception
|
||||
{
|
||||
ContentResponse response = httpClient.GET(serverUri.resolve("/contextHandler"));
|
||||
ContentResponse response = httpClient.GET(serverUri);
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
|
||||
assertNotNull(container);
|
||||
|
@ -125,26 +108,4 @@ public class JavaxClientShutdownWithServerTest
|
|||
assertThat(clientContainer.isRunning(), is(false));
|
||||
assertThat(server.getContainedBeans(WebSocketContainer.class), empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdownWithShutdownContainer() throws Exception
|
||||
{
|
||||
ContentResponse response = httpClient.GET(serverUri.resolve("/shutdownContainer"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
|
||||
assertNotNull(container);
|
||||
assertThat(container, instanceOf(JavaxWebSocketClientContainer.class));
|
||||
JavaxWebSocketClientContainer clientContainer = (JavaxWebSocketClientContainer)container;
|
||||
assertThat(clientContainer.isRunning(), is(true));
|
||||
|
||||
// The container should be a bean on the ContextHandler.
|
||||
Collection<WebSocketContainer> containedBeans = shutdownContainer.getBeans(WebSocketContainer.class);
|
||||
assertThat(containedBeans.size(), is(1));
|
||||
assertThat(containedBeans.toArray()[0], is(container));
|
||||
|
||||
// The client should be attached to the servers LifeCycle and should stop with it.
|
||||
server.stop();
|
||||
assertThat(clientContainer.isRunning(), is(false));
|
||||
assertThat(server.getContainedBeans(WebSocketContainer.class), empty());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,191 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
|
||||
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.javax.tests;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.websocket.ContainerProvider;
|
||||
import javax.websocket.WebSocketContainer;
|
||||
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.client.api.ContentResponse;
|
||||
import org.eclipse.jetty.client.api.Response;
|
||||
import org.eclipse.jetty.http.BadMessageException;
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
import org.eclipse.jetty.io.ByteBufferPool;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.webapp.Configuration;
|
||||
import org.eclipse.jetty.webapp.Configurations;
|
||||
import org.eclipse.jetty.websocket.core.WebSocketComponents;
|
||||
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketClientContainerProvider;
|
||||
import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketShutdownContainer;
|
||||
import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketContainer;
|
||||
import org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketConfiguration;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class JavaxClientShutdownWithServerWebAppTest
|
||||
{
|
||||
private WSServer server;
|
||||
private HttpClient httpClient;
|
||||
|
||||
@FunctionalInterface
|
||||
interface ThrowingRunnable
|
||||
{
|
||||
void run() throws Exception;
|
||||
}
|
||||
|
||||
public void start(ThrowingRunnable configuration) throws Exception
|
||||
{
|
||||
server = new WSServer();
|
||||
configuration.run();
|
||||
server.start();
|
||||
httpClient = new HttpClient();
|
||||
httpClient.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void after() throws Exception
|
||||
{
|
||||
httpClient.stop();
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@WebServlet("/")
|
||||
public static class ContextHandlerShutdownServlet extends HttpServlet
|
||||
{
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
{
|
||||
ContainerProvider.getWebSocketContainer();
|
||||
}
|
||||
}
|
||||
|
||||
public WSServer.WebApp createWebSocketWebapp(String contextName) throws Exception
|
||||
{
|
||||
WSServer.WebApp app = server.createWebApp(contextName);
|
||||
|
||||
// Exclude the Javax WebSocket configuration from the webapp.
|
||||
Configuration[] configurations = Configurations.getKnown().stream()
|
||||
.filter(configuration -> !(configuration instanceof JavaxWebSocketConfiguration))
|
||||
.toArray(Configuration[]::new);
|
||||
app.getWebAppContext().setConfigurations(configurations);
|
||||
|
||||
// Copy over the individual jars required for Javax WebSocket.
|
||||
app.createWebInf();
|
||||
app.copyLib(JavaxWebSocketClientContainerProvider.class, "websocket-javax-client.jar");
|
||||
app.copyLib(JavaxWebSocketContainer.class, "websocket-javax-common.jar");
|
||||
app.copyLib(ContainerLifeCycle.class, "jetty-util.jar");
|
||||
app.copyLib(CoreClientUpgradeRequest.class, "websocket-core-client.jar");
|
||||
app.copyLib(WebSocketComponents.class, "websocket-core-common.jar");
|
||||
app.copyLib(Response.class, "jetty-client.jar");
|
||||
app.copyLib(ByteBufferPool.class, "jetty-io.jar");
|
||||
app.copyLib(BadMessageException.class, "jetty-http.jar");
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void websocketProvidedByServer() throws Exception
|
||||
{
|
||||
start(() ->
|
||||
{
|
||||
WSServer.WebApp app1 = server.createWebApp("app1");
|
||||
app1.createWebInf();
|
||||
app1.copyClass(ContextHandlerShutdownServlet.class);
|
||||
app1.deploy();
|
||||
|
||||
WSServer.WebApp app2 = server.createWebApp("app2");
|
||||
app2.createWebInf();
|
||||
app2.copyClass(ContextHandlerShutdownServlet.class);
|
||||
app2.deploy();
|
||||
|
||||
WSServer.WebApp app3 = server.createWebApp("app3");
|
||||
app3.createWebInf();
|
||||
app3.copyClass(ContextHandlerShutdownServlet.class);
|
||||
app3.deploy();
|
||||
});
|
||||
|
||||
// Before connecting to the server there is only the containers created for the server component of each WebApp.
|
||||
assertThat(server.isRunning(), is(true));
|
||||
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(3));
|
||||
|
||||
// After hitting each WebApp with a request we now have an additional 3 client containers.
|
||||
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app1"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
response = httpClient.GET(server.getServerUri().resolve("/app2"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
response = httpClient.GET(server.getServerUri().resolve("/app3"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(6));
|
||||
|
||||
// All the websocket containers are removed on stopping of the server.
|
||||
server.stop();
|
||||
assertThat(server.isRunning(), is(false));
|
||||
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void websocketProvidedByWebApp() throws Exception
|
||||
{
|
||||
start(() ->
|
||||
{
|
||||
WSServer.WebApp app1 = createWebSocketWebapp("app1");
|
||||
app1.copyClass(ContextHandlerShutdownServlet.class);
|
||||
app1.deploy();
|
||||
|
||||
WSServer.WebApp app2 = createWebSocketWebapp("app2");
|
||||
app2.copyClass(ContextHandlerShutdownServlet.class);
|
||||
app2.deploy();
|
||||
|
||||
WSServer.WebApp app3 = createWebSocketWebapp("app3");
|
||||
app3.copyClass(ContextHandlerShutdownServlet.class);
|
||||
app3.deploy();
|
||||
});
|
||||
|
||||
// Before connecting to the server there is only the containers created for the server component of each WebApp.
|
||||
assertThat(server.isRunning(), is(true));
|
||||
assertThat(server.getContainedBeans(WebSocketContainer.class).size(), is(0));
|
||||
|
||||
// After hitting each WebApp with a request we now have an additional 3 client containers.
|
||||
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app1"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
response = httpClient.GET(server.getServerUri().resolve("/app2"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
response = httpClient.GET(server.getServerUri().resolve("/app3"));
|
||||
assertThat(response.getStatus(), is(HttpStatus.OK_200));
|
||||
|
||||
// Collect the toString result of the ShutdownContainers from the dump.
|
||||
List<String> results = Arrays.stream(server.getServer().dump().split("\n"))
|
||||
.filter(line -> line.contains("+> " + JavaxWebSocketShutdownContainer.class.getSimpleName()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// We only have 3 Shutdown Containers and they all contain only 1 item to be shutdown.
|
||||
assertThat(results.size(), is(3));
|
||||
for (String result : results)
|
||||
{
|
||||
assertThat(result, containsString("size=1"));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue