-
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.
Merge pull request #5024 from eclipse/jetty-9.4.x-5018-ClientUpgradeR…
…equestTimeout Issue #5018 - add request timeout onto ClientUpgradeRequest
- Loading branch information
Showing
3 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
...ocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientTimeoutTest.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,123 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. | ||
// ------------------------------------------------------------------------ | ||
// 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.tests.client; | ||
|
||
import java.util.EnumSet; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import javax.servlet.DispatcherType; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.api.UpgradeException; | ||
import org.eclipse.jetty.websocket.api.util.WSURI; | ||
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; | ||
import org.eclipse.jetty.websocket.client.WebSocketClient; | ||
import org.eclipse.jetty.websocket.server.NativeWebSocketServletContainerInitializer; | ||
import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; | ||
import org.eclipse.jetty.websocket.tests.EventSocket; | ||
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.instanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class ClientTimeoutTest | ||
{ | ||
private Server server; | ||
private WebSocketClient client; | ||
private final CountDownLatch createEndpoint = new CountDownLatch(1); | ||
|
||
@BeforeEach | ||
public void start() throws Exception | ||
{ | ||
server = new Server(); | ||
ServerConnector connector = new ServerConnector(server); | ||
server.addConnector(connector); | ||
|
||
ServletContextHandler contextHandler = new ServletContextHandler(); | ||
contextHandler.setContextPath("/"); | ||
server.setHandler(contextHandler); | ||
|
||
NativeWebSocketServletContainerInitializer.configure(contextHandler, (context, container) -> | ||
{ | ||
container.addMapping("/", (req, res) -> | ||
{ | ||
try | ||
{ | ||
createEndpoint.await(5, TimeUnit.SECONDS); | ||
return new EventSocket.EchoSocket(); | ||
} | ||
catch (InterruptedException e) | ||
{ | ||
throw new IllegalStateException(e); | ||
} | ||
}); | ||
}); | ||
contextHandler.addFilter(WebSocketUpgradeFilter.class, "/", EnumSet.of(DispatcherType.REQUEST)); | ||
server.start(); | ||
|
||
client = new WebSocketClient(); | ||
client.start(); | ||
} | ||
|
||
@AfterEach | ||
public void stop() throws Exception | ||
{ | ||
createEndpoint.countDown(); | ||
client.stop(); | ||
server.stop(); | ||
} | ||
|
||
@Test | ||
public void testWebSocketClientTimeout() throws Exception | ||
{ | ||
EventSocket clientSocket = new EventSocket(); | ||
long timeout = 1000; | ||
client.setMaxIdleTimeout(timeout); | ||
Future<Session> connect = client.connect(clientSocket, WSURI.toWebsocket(server.getURI())); | ||
|
||
ExecutionException executionException = assertThrows(ExecutionException.class, () -> connect.get(timeout * 2, TimeUnit.MILLISECONDS)); | ||
assertThat(executionException.getCause(), instanceOf(UpgradeException.class)); | ||
UpgradeException upgradeException = (UpgradeException)executionException.getCause(); | ||
assertThat(upgradeException.getCause(), instanceOf(TimeoutException.class)); | ||
} | ||
|
||
@Test | ||
public void testClientUpgradeRequestTimeout() throws Exception | ||
{ | ||
EventSocket clientSocket = new EventSocket(); | ||
long timeout = 1000; | ||
ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest(); | ||
upgradeRequest.setTimeout(timeout, TimeUnit.MILLISECONDS); | ||
Future<Session> connect = client.connect(clientSocket, WSURI.toWebsocket(server.getURI()), upgradeRequest); | ||
|
||
ExecutionException executionException = assertThrows(ExecutionException.class, () -> connect.get(timeout * 2, TimeUnit.MILLISECONDS)); | ||
assertThat(executionException.getCause(), instanceOf(UpgradeException.class)); | ||
UpgradeException upgradeException = (UpgradeException)executionException.getCause(); | ||
assertThat(upgradeException.getCause(), instanceOf(TimeoutException.class)); | ||
} | ||
} |
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