diff --git a/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/client/WebSocketClientHandshakeListener.java b/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/client/WebSocketClientHandshakeListener.java index 2d3c3ca474ac..00abb5401b7d 100644 --- a/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/client/WebSocketClientHandshakeListener.java +++ b/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/client/WebSocketClientHandshakeListener.java @@ -76,8 +76,7 @@ public void onSuccess(WebSocketConnection webSocketConnection, HttpCarbonRespons } setWebSocketClient(webSocketClient, carbonResponse, webSocketConnection, retryConfig); WebSocketConnectionInfo connectionInfo = WebSocketUtil.getWebSocketOpenConnectionInfo(webSocketConnection, - webSocketConnector, - webSocketClient, wsService); + webSocketConnector, webSocketClient, wsService); clientConnectorListener.setConnectionInfo(connectionInfo); // Read the next frame when readyOnConnect is true or isReady is true WebSocketUtil.readNextFrame(readyOnConnect, webSocketClient, webSocketConnection); diff --git a/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/observability/WebSocketObservabilityUtil.java b/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/observability/WebSocketObservabilityUtil.java index 4c94cc2ac5f5..c465a747f1a1 100644 --- a/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/observability/WebSocketObservabilityUtil.java +++ b/stdlib/http/src/main/java/org/ballerinalang/net/http/websocket/observability/WebSocketObservabilityUtil.java @@ -22,6 +22,7 @@ import org.ballerinalang.jvm.values.ObjectValue; import org.ballerinalang.net.http.websocket.WebSocketConstants; import org.ballerinalang.net.http.websocket.WebSocketService; +import org.ballerinalang.net.http.websocket.client.FailoverContext; import org.ballerinalang.net.http.websocket.server.WebSocketConnectionInfo; import org.ballerinalang.net.http.websocket.server.WebSocketServerService; import org.slf4j.Logger; @@ -209,7 +210,14 @@ static String getServicePathOrClientUrl(WebSocketConnectionInfo connectionInfo) if (service instanceof WebSocketServerService) { return ((WebSocketServerService) service).getBasePath(); } else { - return connectionInfo.getWebSocketEndpoint().getStringValue("url"); + if (connectionInfo.getWebSocketEndpoint().getType().getName(). + equalsIgnoreCase(WebSocketConstants.FAILOVER_WEBSOCKET_CLIENT)) { + FailoverContext failoverConfig = (FailoverContext) connectionInfo.getWebSocketEndpoint(). + getNativeData(WebSocketConstants.FAILOVER_CONFIG); + return failoverConfig.getTargetUrls().get(failoverConfig.getCurrentIndex()); + } else { + return connectionInfo.getWebSocketEndpoint().getStringValue("url"); + } } } diff --git a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/websocket/FailoverClientTest.java b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/websocket/FailoverClientTest.java index 654ff2c7006a..9344f29c44bf 100644 --- a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/websocket/FailoverClientTest.java +++ b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/service/websocket/FailoverClientTest.java @@ -40,6 +40,7 @@ public class FailoverClientTest extends WebSocketTestCommons { private WebSocketRemoteServer remoteServer15200; private String url = "ws://localhost:21033"; private int port = 15300; + private String textSent = "hi all"; @Test(description = "Tests starting the second server in the target URLs, sending and receiving text frames " + "using the failover websocket client") @@ -50,7 +51,6 @@ public void testTextFrameWithSecondServer() throws URISyntaxException, Interrupt remoteServer15200.run(); WebSocketTestClient client = new WebSocketTestClient(url); client.handshake(); - String textSent = "hi all"; client.sendText(textSent); countDownLatch.await(TIMEOUT_IN_SECS, TimeUnit.SECONDS); Assert.assertEquals(client.getTextReceived(), textSent); @@ -102,14 +102,12 @@ public void testFailoverWithBothServer() throws URISyntaxException, InterruptedE ByteBuffer bufferSent = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 6}); WebSocketTestClient client = new WebSocketTestClient(url); client.handshake(); - String textSent = "hi all"; + client.sendText(textSent); countDownLatchFor15300.await(TIMEOUT_IN_SECS, TimeUnit.SECONDS); Assert.assertEquals(client.getTextReceived(), textSent); remoteServer15300.stop(); CountDownLatch countDownLatchFor15200 = new CountDownLatch(1); - int time = 2; - countDownLatchFor15200.await(time, TimeUnit.SECONDS); String text = "hi"; client.sendText(text); countDownLatchFor15200.await(TIMEOUT_IN_SECS, TimeUnit.SECONDS); @@ -120,4 +118,31 @@ public void testFailoverWithBothServer() throws URISyntaxException, InterruptedE client.shutDown(); remoteServer15200.stop(); } + + @Test(description = "Test the Failover websocket client (starting the second server in the target URLs, " + + "first sending and receiving text frames Afterthat stop the second server and start the in the target " + + "URLs, sending and receiving binary frames) ") + public void testFailoverWithBothServerFirstStartSecondServer() throws URISyntaxException, InterruptedException, + BallerinaTestException { + CountDownLatch countDownLatch = new CountDownLatch(1); + remoteServer15200 = new WebSocketRemoteServer(15200); + remoteServer15300 = new WebSocketRemoteServer(port); + remoteServer15200.run(); + ByteBuffer bufferSent = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 6}); + WebSocketTestClient client = new WebSocketTestClient(url); + client.handshake(); + client.sendText(textSent); + countDownLatch.await(TIMEOUT_IN_SECS, TimeUnit.SECONDS); + Assert.assertEquals(client.getTextReceived(), textSent); + remoteServer15300.run(); + countDownLatch.await(TIMEOUT_IN_SECS, TimeUnit.SECONDS); + remoteServer15200.stop(); + CountDownLatch countDownLatchFor15300 = new CountDownLatch(1); + String text = "hi"; + client.sendText(text); + countDownLatchFor15300.await(TIMEOUT_IN_SECS, TimeUnit.SECONDS); + Assert.assertEquals(client.getTextReceived(), text); + client.shutDown(); + remoteServer15300.stop(); + } }