From 8f0793a0fc46f89aa93b813ea5b1faa9826e951f Mon Sep 17 00:00:00 2001 From: monosoul Date: Wed, 2 Dec 2026 12:58:34 +0100 Subject: [PATCH] test(clickhouse-client): fix test failures when using VPN/proxy --- .../client/ClientIntegrationTest.java | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/clickhouse-client/src/test/java/com/clickhouse/client/ClientIntegrationTest.java b/clickhouse-client/src/test/java/com/clickhouse/client/ClientIntegrationTest.java index 07333646d..9041b5a71 100644 --- a/clickhouse-client/src/test/java/com/clickhouse/client/ClientIntegrationTest.java +++ b/clickhouse-client/src/test/java/com/clickhouse/client/ClientIntegrationTest.java @@ -22,6 +22,7 @@ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; +import java.net.ServerSocket; import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -2733,16 +2734,18 @@ public void testRowBinaryWithDefaults() throws ClickHouseException, IOException, } @Test(dataProvider = "loadBalancingPolicies", groups = {"unit"}) - public void testLoadBalancingPolicyFailover(ClickHouseLoadBalancingPolicy loadBalancingPolicy) { - String firstEndpoint = "111.1.1.1"; - String secondEndpoint = "222.2.2.2"; + public void testLoadBalancingPolicyFailover(ClickHouseLoadBalancingPolicy loadBalancingPolicy) throws IOException { + String endpoint = "127.0.0.1"; + int[] ports = findUnusedPorts(endpoint, 2); + int firstPort = ports[0]; + int secondPort = ports[1]; Properties props = new Properties(); props.setProperty("failover", "1"); // nodes where the first node is failed ClickHouseNodes nodes = ClickHouseNodes.of( - getProtocol() + "://" + firstEndpoint + "," + secondEndpoint, + getProtocol() + "://" + endpoint + ":" + firstPort + "," + endpoint + ":" + secondPort, props ); @@ -2753,12 +2756,36 @@ public void testLoadBalancingPolicyFailover(ClickHouseLoadBalancingPolicy loadBa Assert.fail("Exception expected for query on failed node"); } catch (Exception failoverException) { ClickHouseNode failoverNode = loadBalancingPolicy.suggest(nodes, nodes.nodes.getFirst(), failoverException); - Assert.assertEquals(failoverNode.getHost(), secondEndpoint, "The next node is expected to be suggested by the load balancing policy"); + Assert.assertEquals(failoverNode.getHost(), endpoint, "The next node is expected to be suggested by the load balancing policy"); + Assert.assertEquals(failoverNode.getPort(), secondPort, "The next node is expected to be suggested by the load balancing policy"); + } + } + + /** + * Returns distinct TCP ports on the given local address that have no listener, so a connection to them is + * refused at once. All sockets stay open until every port is known, so the OS cannot return the same port twice. + */ + private static int[] findUnusedPorts(String localAddress, int count) throws IOException { + InetAddress address = InetAddress.getByName(localAddress); + ServerSocket[] sockets = new ServerSocket[count]; + try { + int[] ports = new int[count]; + for (int i = 0; i < count; i++) { + sockets[i] = new ServerSocket(0, 1, address); + ports[i] = sockets[i].getLocalPort(); + } + return ports; + } finally { + for (ServerSocket socket : sockets) { + if (socket != null) { + socket.close(); + } + } } } @Test(groups = {"integration"}) - public void testFailover() throws ClickHouseException { + public void testFailover() throws ClickHouseException, IOException { if (isCloud()) return; //TODO: testFailover - Revisit, see: https://github.com/ClickHouse/clickhouse-java/issues/1747 ClickHouseNode availableNode = getServer(); Properties props = new Properties(); @@ -2766,8 +2793,10 @@ public void testFailover() throws ClickHouseException { props.setProperty(ClickHouseDefaults.PASSWORD.getKey(), getPassword()); // nodes with the first node is unavailable + String unavailableEndpoint = "127.0.0.1"; + int unavailablePort = findUnusedPorts(unavailableEndpoint, 1)[0]; ClickHouseNodes nodes = ClickHouseNodes.of( - getProtocol() + "://111.1.1.1," + availableNode.getBaseUri(), + getProtocol() + "://" + unavailableEndpoint + ":" + unavailablePort + "," + availableNode.getBaseUri(), props );