-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #5832 - shutdown WSClientContainer with ContextHandler if possible
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
- Loading branch information
1 parent
374e02c
commit 25f8c65
Showing
3 changed files
with
166 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/ClientInWebappTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995-2020 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.io.IOException; | ||
import java.net.URI; | ||
import javax.servlet.ServletException; | ||
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.http.HttpStatus; | ||
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.websocket.javax.client.internal.JavaxWebSocketClientContainer; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class ClientInWebappTest | ||
{ | ||
private Server server; | ||
private URI serverUri; | ||
private HttpClient httpClient; | ||
private volatile WebSocketContainer container; | ||
|
||
public class ServerSocket extends HttpServlet | ||
{ | ||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException | ||
{ | ||
container = ContainerProvider.getWebSocketContainer(); | ||
} | ||
} | ||
|
||
@BeforeEach | ||
public void before() throws Exception | ||
{ | ||
server = new Server(); | ||
ServerConnector connector = new ServerConnector(server); | ||
connector.setPort(8080); | ||
server.addConnector(connector); | ||
|
||
ServletContextHandler contextHandler = new ServletContextHandler(); | ||
contextHandler.setContextPath("/"); | ||
contextHandler.addServlet(new ServletHolder(new ServerSocket()), "/"); | ||
server.setHandler(contextHandler); | ||
server.start(); | ||
serverUri = WSURI.toWebsocket(server.getURI()); | ||
|
||
httpClient = new HttpClient(); | ||
httpClient.start(); | ||
} | ||
|
||
@AfterEach | ||
public void after() throws Exception | ||
{ | ||
httpClient.stop(); | ||
server.stop(); | ||
} | ||
|
||
@Test | ||
public void testWebSocketClientContainerInWebapp() throws Exception | ||
{ | ||
ContentResponse response = httpClient.GET(serverUri); | ||
assertThat(response.getStatus(), is(HttpStatus.OK_200)); | ||
|
||
assertNotNull(container); | ||
assertThat(container, instanceOf(JavaxWebSocketClientContainer.class)); | ||
JavaxWebSocketClientContainer clientContainer = (JavaxWebSocketClientContainer)container; | ||
assertThat(clientContainer.isRunning(), is(true)); | ||
|
||
// 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()); | ||
} | ||
} |