Introduce ListenableFuture to WebSocketClient
Issue: SPR-10888
This commit is contained in:
@@ -26,6 +26,7 @@ import org.junit.runners.Parameterized.Parameter;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.socket.client.WebSocketClient;
|
||||
import org.springframework.web.socket.server.DefaultHandshakeHandler;
|
||||
@@ -108,7 +109,7 @@ public abstract class AbstractWebSocketIntegrationTests {
|
||||
return "ws://localhost:" + this.server.getPort();
|
||||
}
|
||||
|
||||
protected WebSocketSession doHandshake(WebSocketHandler clientHandler, String endpointPath) {
|
||||
protected ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler clientHandler, String endpointPath) {
|
||||
return this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + endpointPath);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.coyote.http11.Http11NioProtocol;
|
||||
import org.apache.tomcat.util.descriptor.web.ApplicationListener;
|
||||
import org.apache.tomcat.websocket.server.WsListener;
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
import org.springframework.util.SocketUtils;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
@@ -74,7 +73,7 @@ public class TomcatWebSocketTestServer implements WebSocketTestServer {
|
||||
return tempFolder;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new NestedRuntimeException("Unable to create temp directory", ex) {};
|
||||
throw new RuntimeException("Unable to create temp directory", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,19 +19,21 @@ package org.springframework.web.socket.client;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.util.concurrent.ListenableFutureTask;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.adapter.WebSocketHandlerAdapter;
|
||||
import org.springframework.web.socket.support.LoggingWebSocketHandlerDecorator;
|
||||
import org.springframework.web.socket.support.WebSocketHandlerDecorator;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link WebSocketConnectionManager}.
|
||||
@@ -45,26 +47,20 @@ public class WebSocketConnectionManagerTests {
|
||||
|
||||
List<String> subprotocols = Arrays.asList("abc");
|
||||
|
||||
WebSocketClient client = mock(WebSocketClient.class);
|
||||
TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
|
||||
WebSocketHandler handler = new WebSocketHandlerAdapter();
|
||||
|
||||
WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/path/{id}", "123");
|
||||
manager.setSubProtocols(subprotocols);
|
||||
manager.openConnection();
|
||||
|
||||
ArgumentCaptor<WebSocketHandlerDecorator> captor = ArgumentCaptor.forClass(WebSocketHandlerDecorator.class);
|
||||
ArgumentCaptor<HttpHeaders> headersCaptor = ArgumentCaptor.forClass(HttpHeaders.class);
|
||||
ArgumentCaptor<URI> uriCaptor = ArgumentCaptor.forClass(URI.class);
|
||||
|
||||
verify(client).doHandshake(captor.capture(), headersCaptor.capture(), uriCaptor.capture());
|
||||
|
||||
HttpHeaders expectedHeaders = new HttpHeaders();
|
||||
expectedHeaders.setSecWebSocketProtocol(subprotocols);
|
||||
|
||||
assertEquals(expectedHeaders, headersCaptor.getValue());
|
||||
assertEquals(new URI("/path/123"), uriCaptor.getValue());
|
||||
assertEquals(expectedHeaders, client.headers);
|
||||
assertEquals(new URI("/path/123"), client.uri);
|
||||
|
||||
WebSocketHandlerDecorator loggingHandler = captor.getValue();
|
||||
WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler;
|
||||
assertEquals(LoggingWebSocketHandlerDecorator.class, loggingHandler.getClass());
|
||||
|
||||
assertSame(handler, loggingHandler.getDelegate());
|
||||
@@ -103,6 +99,13 @@ public class WebSocketConnectionManagerTests {
|
||||
|
||||
private boolean running;
|
||||
|
||||
private WebSocketHandler webSocketHandler;
|
||||
|
||||
private HttpHeaders headers;
|
||||
|
||||
private URI uri;
|
||||
|
||||
|
||||
public TestLifecycleWebSocketClient(boolean running) {
|
||||
this.running = running;
|
||||
}
|
||||
@@ -138,15 +141,27 @@ public class WebSocketConnectionManagerTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVariables)
|
||||
throws WebSocketConnectFailureException {
|
||||
return null;
|
||||
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler,
|
||||
String uriTemplate, Object... uriVars) {
|
||||
|
||||
URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri();
|
||||
return doHandshake(webSocketHandler, null, uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, HttpHeaders headers, URI uri)
|
||||
throws WebSocketConnectFailureException {
|
||||
return null;
|
||||
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler,
|
||||
HttpHeaders headers, URI uri) {
|
||||
|
||||
this.webSocketHandler = webSocketHandler;
|
||||
this.headers = headers;
|
||||
this.uri = uri;
|
||||
|
||||
return new ListenableFutureTask<WebSocketSession>(new Callable<WebSocketSession>() {
|
||||
@Override
|
||||
public WebSocketSession call() throws Exception {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ public class StandardWebSocketClientTests {
|
||||
@Test
|
||||
public void localAddress() throws Exception {
|
||||
URI uri = new URI("ws://example.com/abc");
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertNotNull(session.getLocalAddress());
|
||||
assertEquals(80, session.getLocalAddress().getPort());
|
||||
@@ -75,7 +75,7 @@ public class StandardWebSocketClientTests {
|
||||
@Test
|
||||
public void localAddressWss() throws Exception {
|
||||
URI uri = new URI("wss://example.com/abc");
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertNotNull(session.getLocalAddress());
|
||||
assertEquals(443, session.getLocalAddress().getPort());
|
||||
@@ -90,7 +90,7 @@ public class StandardWebSocketClientTests {
|
||||
@Test
|
||||
public void remoteAddress() throws Exception {
|
||||
URI uri = new URI("wss://example.com/abc");
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertNotNull(session.getRemoteAddress());
|
||||
assertEquals("example.com", session.getRemoteAddress().getHostName());
|
||||
@@ -105,7 +105,7 @@ public class StandardWebSocketClientTests {
|
||||
this.headers.setSecWebSocketProtocol(protocols);
|
||||
this.headers.add("foo", "bar");
|
||||
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertEquals(Collections.singletonMap("foo", Arrays.asList("bar")), session.getHandshakeHeaders());
|
||||
}
|
||||
@@ -118,7 +118,7 @@ public class StandardWebSocketClientTests {
|
||||
this.headers.setSecWebSocketProtocol(protocols);
|
||||
this.headers.add("foo", "bar");
|
||||
|
||||
this.wsClient.doHandshake(this.wsHandler, this.headers, uri);
|
||||
this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
ArgumentCaptor<Endpoint> arg1 = ArgumentCaptor.forClass(Endpoint.class);
|
||||
ArgumentCaptor<ClientEndpointConfig> arg2 = ArgumentCaptor.forClass(ClientEndpointConfig.class);
|
||||
|
||||
@@ -83,7 +83,7 @@ public class JettyWebSocketClientTests {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setSecWebSocketProtocol(Arrays.asList("echo"));
|
||||
|
||||
this.wsSession = this.client.doHandshake(new TextWebSocketHandlerAdapter(), headers, new URI(this.wsUrl));
|
||||
this.wsSession = this.client.doHandshake(new TextWebSocketHandlerAdapter(), headers, new URI(this.wsUrl)).get();
|
||||
|
||||
assertEquals(this.wsUrl, this.wsSession.getUri().toString());
|
||||
assertEquals("echo", this.wsSession.getAcceptedProtocol());
|
||||
|
||||
@@ -65,8 +65,8 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes
|
||||
@Test
|
||||
public void registerWebSocketHandler() throws Exception {
|
||||
|
||||
WebSocketSession session =
|
||||
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/ws");
|
||||
WebSocketSession session = this.webSocketClient.doHandshake(
|
||||
new WebSocketHandlerAdapter(), getWsBaseUrl() + "/ws").get();
|
||||
|
||||
TestWebSocketHandler serverHandler = this.wac.getBean(TestWebSocketHandler.class);
|
||||
assertTrue(serverHandler.latch.await(2, TimeUnit.SECONDS));
|
||||
@@ -77,8 +77,8 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes
|
||||
@Test
|
||||
public void registerWebSocketHandlerWithSockJS() throws Exception {
|
||||
|
||||
WebSocketSession session =
|
||||
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/sockjs/websocket");
|
||||
WebSocketSession session = this.webSocketClient.doHandshake(
|
||||
new WebSocketHandlerAdapter(), getWsBaseUrl() + "/sockjs/websocket").get();
|
||||
|
||||
TestWebSocketHandler serverHandler = this.wac.getBean(TestWebSocketHandler.class);
|
||||
assertTrue(serverHandler.latch.await(2, TimeUnit.SECONDS));
|
||||
|
||||
Reference in New Issue
Block a user