Skip to content

Commit 537add5

Browse files
artembilantzolov
authored andcommitted
GH-8785: Propagate WebSocket client connect fail
Fixes #8785 The `ClientWebSocketContainer.start()` delegates to the `IntegrationWebSocketConnectionManager` which performs an async connection to the server. * Wait for `connectionLatch` in the `ClientWebSocketContainer.start()` and check for `this.openConnectionException != null` to re-throw. Mark `ClientWebSocketContainer` as stopped in that case **Cherry-pick to `6.1.x` & `6.0.x`**
1 parent bcfd81a commit 537add5

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/ClientWebSocketContainer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ public void start() {
192192
this.openConnectionException = null;
193193
this.connectionLatch = new CountDownLatch(1);
194194
this.connectionManager.start();
195+
196+
try {
197+
this.connectionLatch.await(this.connectionTimeout, TimeUnit.SECONDS);
198+
}
199+
catch (InterruptedException ex) {
200+
logger.error("'clientSession' has not been established during 'openConnection'");
201+
}
202+
203+
if (this.openConnectionException != null) {
204+
// The next 'this.connectionManager.stop()' call resets 'this.openConnectionException' to null
205+
IllegalStateException exceptionToRethrow = new IllegalStateException(this.openConnectionException);
206+
this.connectionManager.stop();
207+
throw exceptionToRethrow;
208+
}
195209
}
196210
}
197211
finally {

spring-integration-websocket/src/test/java/org/springframework/integration/websocket/ClientWebSocketContainerTests.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.concurrent.TimeUnit;
2929
import java.util.concurrent.atomic.AtomicBoolean;
3030

31+
import jakarta.websocket.DeploymentException;
3132
import org.apache.tomcat.websocket.Constants;
3233
import org.junit.jupiter.api.AfterAll;
3334
import org.junit.jupiter.api.BeforeAll;
@@ -126,10 +127,8 @@ protected CompletableFuture<WebSocketSession> executeInternal(WebSocketHandler w
126127

127128
failure.set(true);
128129

129-
container.start();
130-
131130
assertThatIllegalStateException()
132-
.isThrownBy(() -> container.getSession(null))
131+
.isThrownBy(container::start)
133132
.withCauseInstanceOf(CancellationException.class);
134133

135134
failure.set(false);
@@ -170,6 +169,20 @@ public void testWebSocketContainerOverflowStrategyPropagation() throws Exception
170169
.isEqualTo(ConcurrentWebSocketSessionDecorator.OverflowStrategy.DROP);
171170
}
172171

172+
@Test
173+
public void webSocketContainerFailsOnStartForInvalidUrl() {
174+
StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
175+
176+
ClientWebSocketContainer container =
177+
new ClientWebSocketContainer(webSocketClient, server.getWsBaseUrl() + "/no_such_endpoint");
178+
179+
assertThatIllegalStateException()
180+
.isThrownBy(container::start)
181+
.withCauseExactlyInstanceOf(DeploymentException.class)
182+
.withStackTraceContaining(
183+
"The HTTP response from the server [404] did not permit the HTTP upgrade to WebSocket");
184+
}
185+
173186
private static class TestWebSocketListener implements WebSocketListener {
174187

175188
public final CountDownLatch messageLatch = new CountDownLatch(1);

0 commit comments

Comments
 (0)