Mark ListenableFuture as deprecated for removal
Closes gh-33808
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -57,38 +57,34 @@ class StandardWebSocketClientTests {
|
||||
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void getLocalAddress() throws Exception {
|
||||
URI uri = URI.create("ws://localhost/abc");
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertThat(session.getLocalAddress()).isNotNull();
|
||||
assertThat(session.getLocalAddress().getPort()).isEqualTo(80);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void getLocalAddressWss() throws Exception {
|
||||
URI uri = URI.create("wss://localhost/abc");
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertThat(session.getLocalAddress()).isNotNull();
|
||||
assertThat(session.getLocalAddress().getPort()).isEqualTo(443);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void getLocalAddressNoScheme() {
|
||||
URI uri = URI.create("localhost/abc");
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
this.wsClient.doHandshake(this.wsHandler, this.headers, uri));
|
||||
this.wsClient.execute(this.wsHandler, this.headers, uri));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void getRemoteAddress() throws Exception {
|
||||
URI uri = URI.create("wss://localhost/abc");
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertThat(session.getRemoteAddress()).isNotNull();
|
||||
assertThat(session.getRemoteAddress().getHostName()).isEqualTo("localhost");
|
||||
@@ -96,27 +92,25 @@ class StandardWebSocketClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void handshakeHeaders() throws Exception {
|
||||
URI uri = URI.create("ws://localhost/abc");
|
||||
List<String> protocols = Collections.singletonList("abc");
|
||||
this.headers.setSecWebSocketProtocol(protocols);
|
||||
this.headers.add("foo", "bar");
|
||||
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertThat(session.getHandshakeHeaders()).hasSize(1);
|
||||
assertThat(session.getHandshakeHeaders().getFirst("foo")).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void clientEndpointConfig() throws Exception {
|
||||
URI uri = URI.create("ws://localhost/abc");
|
||||
List<String> protocols = Collections.singletonList("abc");
|
||||
this.headers.setSecWebSocketProtocol(protocols);
|
||||
|
||||
this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
ArgumentCaptor<ClientEndpointConfig> captor = ArgumentCaptor.forClass(ClientEndpointConfig.class);
|
||||
verify(this.wsContainer).connectToServer(any(Endpoint.class), captor.capture(), any(URI.class));
|
||||
@@ -126,13 +120,12 @@ class StandardWebSocketClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void clientEndpointConfigWithUserProperties() throws Exception {
|
||||
Map<String,Object> userProperties = Collections.singletonMap("foo", "bar");
|
||||
|
||||
URI uri = URI.create("ws://localhost/abc");
|
||||
this.wsClient.setUserProperties(userProperties);
|
||||
this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
ArgumentCaptor<ClientEndpointConfig> captor = ArgumentCaptor.forClass(ClientEndpointConfig.class);
|
||||
verify(this.wsContainer).connectToServer(any(Endpoint.class), captor.capture(), any(URI.class));
|
||||
@@ -142,12 +135,11 @@ class StandardWebSocketClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void standardWebSocketClientConfiguratorInsertsHandshakeHeaders() throws Exception {
|
||||
URI uri = URI.create("ws://localhost/abc");
|
||||
this.headers.add("foo", "bar");
|
||||
|
||||
this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
ArgumentCaptor<ClientEndpointConfig> captor = ArgumentCaptor.forClass(ClientEndpointConfig.class);
|
||||
verify(this.wsContainer).connectToServer(any(Endpoint.class), captor.capture(), any(URI.class));
|
||||
@@ -159,11 +151,10 @@ class StandardWebSocketClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void taskExecutor() throws Exception {
|
||||
URI uri = URI.create("ws://localhost/abc");
|
||||
this.wsClient.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
|
||||
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
|
||||
|
||||
assertThat(session).isNotNull();
|
||||
}
|
||||
|
||||
@@ -209,24 +209,16 @@ abstract class AbstractSockJsIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void infoRequestFailure() throws Exception {
|
||||
TestClientHandler handler = new TestClientHandler();
|
||||
this.testFilter.sendErrorMap.put("/info", 500);
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
initSockJsClient(createWebSocketTransport());
|
||||
this.sockJsClient.doHandshake(handler, this.baseUrl + "/echo").addCallback(
|
||||
new org.springframework.util.concurrent.ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(WebSocketSession result) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
);
|
||||
this.sockJsClient.execute(handler, this.baseUrl + "/echo").whenComplete((result, ex) -> {
|
||||
if (ex != null) {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
assertThat(latch.await(5000, TimeUnit.MILLISECONDS)).isTrue();
|
||||
}
|
||||
|
||||
@@ -245,14 +237,13 @@ abstract class AbstractSockJsIntegrationTests {
|
||||
|
||||
@Test
|
||||
@Timeout(5)
|
||||
@SuppressWarnings("deprecation")
|
||||
void fallbackAfterConnectTimeout() throws Exception {
|
||||
TestClientHandler clientHandler = new TestClientHandler();
|
||||
this.testFilter.sleepDelayMap.put("/xhr_streaming", 10000L);
|
||||
this.testFilter.sendErrorMap.put("/xhr_streaming", 503);
|
||||
initSockJsClient(createXhrTransport());
|
||||
// this.sockJsClient.setConnectTimeoutScheduler(this.wac.getBean(ThreadPoolTaskScheduler.class));
|
||||
WebSocketSession clientSession = sockJsClient.doHandshake(clientHandler, this.baseUrl + "/echo").get();
|
||||
WebSocketSession clientSession = sockJsClient.execute(clientHandler, this.baseUrl + "/echo").get();
|
||||
assertThat(clientSession.getClass()).as("Fallback didn't occur").isEqualTo(XhrClientSockJsSession.class);
|
||||
TextMessage message = new TextMessage("message1");
|
||||
clientSession.sendMessage(message);
|
||||
@@ -261,7 +252,6 @@ abstract class AbstractSockJsIntegrationTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void testEcho(int messageCount, Transport transport, WebSocketHttpHeaders headers) throws Exception {
|
||||
List<TextMessage> messages = new ArrayList<>();
|
||||
for (int i = 0; i < messageCount; i++) {
|
||||
@@ -270,7 +260,7 @@ abstract class AbstractSockJsIntegrationTests {
|
||||
TestClientHandler handler = new TestClientHandler();
|
||||
initSockJsClient(transport);
|
||||
URI url = URI.create(this.baseUrl + "/echo");
|
||||
WebSocketSession session = this.sockJsClient.doHandshake(handler, headers, url).get();
|
||||
WebSocketSession session = this.sockJsClient.execute(handler, headers, url).get();
|
||||
for (TextMessage message : messages) {
|
||||
session.sendMessage(message);
|
||||
}
|
||||
@@ -282,13 +272,10 @@ abstract class AbstractSockJsIntegrationTests {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void testReceiveOneMessage(Transport transport, WebSocketHttpHeaders headers)
|
||||
throws Exception {
|
||||
|
||||
private void testReceiveOneMessage(Transport transport, WebSocketHttpHeaders headers) throws Exception {
|
||||
TestClientHandler clientHandler = new TestClientHandler();
|
||||
initSockJsClient(transport);
|
||||
this.sockJsClient.doHandshake(clientHandler, headers, URI.create(this.baseUrl + "/test")).get();
|
||||
this.sockJsClient.execute(clientHandler, headers, URI.create(this.baseUrl + "/test")).get();
|
||||
TestServerHandler serverHandler = this.wac.getBean(TestServerHandler.class);
|
||||
|
||||
assertThat(clientHandler.session).as("afterConnectionEstablished should have been called").isNotNull();
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
@@ -129,26 +130,17 @@ class RestTemplateXhrTransportTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectFailure() {
|
||||
final HttpServerErrorException expected = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
RestOperations restTemplate = mock();
|
||||
given(restTemplate.execute(any(), eq(HttpMethod.POST), any(), any())).willThrow(expected);
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
connect(restTemplate).addCallback(
|
||||
new org.springframework.util.concurrent.ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(WebSocketSession result) {
|
||||
}
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
if (ex == expected) {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
connect(restTemplate).whenComplete((result, ex) -> {
|
||||
if (ex == expected) {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
verifyNoMoreInteractions(this.webSocketHandler);
|
||||
}
|
||||
|
||||
@@ -178,16 +170,11 @@ class RestTemplateXhrTransportTests {
|
||||
verify(response).close();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private org.springframework.util.concurrent.ListenableFuture<WebSocketSession> connect(
|
||||
ClientHttpResponse... responses) {
|
||||
private CompletableFuture<WebSocketSession> connect(ClientHttpResponse... responses) {
|
||||
return connect(new TestRestTemplate(responses));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private org.springframework.util.concurrent.ListenableFuture<WebSocketSession> connect(
|
||||
RestOperations restTemplate, ClientHttpResponse... responses) {
|
||||
|
||||
private CompletableFuture<WebSocketSession> connect(RestOperations restTemplate) {
|
||||
RestTemplateXhrTransport transport = new RestTemplateXhrTransport(restTemplate);
|
||||
transport.setTaskExecutor(new SyncTaskExecutor());
|
||||
|
||||
@@ -197,7 +184,7 @@ class RestTemplateXhrTransportTests {
|
||||
TransportRequest request = new DefaultTransportRequest(urlInfo, headers, headers,
|
||||
transport, TransportType.XHR, CODEC);
|
||||
|
||||
return transport.connect(request, this.webSocketHandler);
|
||||
return transport.connectAsync(request, this.webSocketHandler);
|
||||
}
|
||||
|
||||
private ClientHttpResponse response(HttpStatus status, String body) throws IOException {
|
||||
@@ -218,7 +205,6 @@ class RestTemplateXhrTransportTests {
|
||||
|
||||
private Queue<ClientHttpResponse> responses = new LinkedBlockingDeque<>();
|
||||
|
||||
|
||||
private TestRestTemplate(ClientHttpResponse... responses) {
|
||||
this.responses.addAll(Arrays.asList(responses));
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.socket.sockjs.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -50,17 +51,15 @@ class SockJsClientTests {
|
||||
|
||||
private static final WebSocketHandler handler = mock();
|
||||
|
||||
|
||||
private final InfoReceiver infoReceiver = mock();
|
||||
|
||||
private final TestTransport webSocketTransport = new TestTransport("WebSocketTestTransport");
|
||||
|
||||
private final XhrTestTransport xhrTransport = new XhrTestTransport("XhrTestTransport");
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private org.springframework.util.concurrent.ListenableFutureCallback<WebSocketSession> connectCallback = mock();
|
||||
private final BiConsumer<WebSocketSession, Throwable> connectCallback = mock();
|
||||
|
||||
private SockJsClient sockJsClient = new SockJsClient(List.of(this.webSocketTransport, this.xhrTransport));
|
||||
private final SockJsClient sockJsClient = new SockJsClient(List.of(this.webSocketTransport, this.xhrTransport));
|
||||
|
||||
|
||||
@BeforeEach
|
||||
@@ -69,40 +68,36 @@ class SockJsClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectWebSocket() {
|
||||
setupInfoRequest(true);
|
||||
this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback);
|
||||
this.sockJsClient.execute(handler, URL).whenComplete(this.connectCallback);
|
||||
assertThat(this.webSocketTransport.invoked()).isTrue();
|
||||
WebSocketSession session = mock();
|
||||
this.webSocketTransport.getConnectCallback().accept(session, null);
|
||||
verify(this.connectCallback).onSuccess(session);
|
||||
verify(this.connectCallback).accept(session, null);
|
||||
verifyNoMoreInteractions(this.connectCallback);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectWebSocketDisabled() {
|
||||
setupInfoRequest(false);
|
||||
this.sockJsClient.doHandshake(handler, URL);
|
||||
this.sockJsClient.execute(handler, URL);
|
||||
assertThat(this.webSocketTransport.invoked()).isFalse();
|
||||
assertThat(this.xhrTransport.invoked()).isTrue();
|
||||
assertThat(this.xhrTransport.getRequest().getTransportUrl().toString()).endsWith("xhr_streaming");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectXhrStreamingDisabled() {
|
||||
setupInfoRequest(false);
|
||||
this.xhrTransport.setStreamingDisabled(true);
|
||||
this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback);
|
||||
this.sockJsClient.execute(handler, URL).whenComplete(this.connectCallback);
|
||||
assertThat(this.webSocketTransport.invoked()).isFalse();
|
||||
assertThat(this.xhrTransport.invoked()).isTrue();
|
||||
assertThat(this.xhrTransport.getRequest().getTransportUrl().toString()).endsWith("xhr");
|
||||
}
|
||||
|
||||
@Test // SPR-13254
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectWithHandshakeHeaders() {
|
||||
ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
|
||||
this.xhrTransport.setStreamingDisabled(true);
|
||||
@@ -110,7 +105,7 @@ class SockJsClientTests {
|
||||
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
|
||||
headers.set("foo", "bar");
|
||||
headers.set("auth", "123");
|
||||
this.sockJsClient.doHandshake(handler, headers, URI.create(URL)).addCallback(this.connectCallback);
|
||||
this.sockJsClient.execute(handler, headers, URI.create(URL)).whenComplete(this.connectCallback);
|
||||
|
||||
HttpHeaders httpHeaders = headersCaptor.getValue();
|
||||
assertThat(httpHeaders).hasSize(2);
|
||||
@@ -124,7 +119,6 @@ class SockJsClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectAndUseSubsetOfHandshakeHeadersForHttpRequests() {
|
||||
ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
|
||||
this.xhrTransport.setStreamingDisabled(true);
|
||||
@@ -133,7 +127,7 @@ class SockJsClientTests {
|
||||
headers.set("foo", "bar");
|
||||
headers.set("auth", "123");
|
||||
this.sockJsClient.setHttpHeaderNames("auth");
|
||||
this.sockJsClient.doHandshake(handler, headers, URI.create(URL)).addCallback(this.connectCallback);
|
||||
this.sockJsClient.execute(handler, headers, URI.create(URL)).whenComplete(this.connectCallback);
|
||||
|
||||
assertThat(headersCaptor.getValue()).hasSize(1);
|
||||
assertThat(headersCaptor.getValue().getFirst("auth")).isEqualTo("123");
|
||||
@@ -142,30 +136,27 @@ class SockJsClientTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectSockJsInfo() {
|
||||
setupInfoRequest(true);
|
||||
this.sockJsClient.doHandshake(handler, URL);
|
||||
this.sockJsClient.execute(handler, URL);
|
||||
verify(this.infoReceiver, times(1)).executeInfoRequest(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectSockJsInfoCached() {
|
||||
setupInfoRequest(true);
|
||||
this.sockJsClient.doHandshake(handler, URL);
|
||||
this.sockJsClient.doHandshake(handler, URL);
|
||||
this.sockJsClient.doHandshake(handler, URL);
|
||||
this.sockJsClient.execute(handler, URL);
|
||||
this.sockJsClient.execute(handler, URL);
|
||||
this.sockJsClient.execute(handler, URL);
|
||||
verify(this.infoReceiver, times(1)).executeInfoRequest(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void connectInfoRequestFailure() {
|
||||
HttpServerErrorException exception = new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE);
|
||||
given(this.infoReceiver.executeInfoRequest(any(), any())).willThrow(exception);
|
||||
this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback);
|
||||
verify(this.connectCallback).onFailure(exception);
|
||||
this.sockJsClient.execute(handler, URL).whenComplete(this.connectCallback);
|
||||
verify(this.connectCallback).accept(null, exception);
|
||||
assertThat(this.webSocketTransport.invoked()).isFalse();
|
||||
assertThat(this.xhrTransport.invoked()).isFalse();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user