2222import java .net .Inet4Address ;
2323import java .net .Inet6Address ;
2424import java .net .InetAddress ;
25+ import java .net .ServerSocket ;
2526import java .net .UnknownHostException ;
2627import java .nio .charset .StandardCharsets ;
2728import java .nio .file .Files ;
@@ -2733,16 +2734,18 @@ public void testRowBinaryWithDefaults() throws ClickHouseException, IOException,
27332734 }
27342735
27352736 @ Test (dataProvider = "loadBalancingPolicies" , groups = {"unit" })
2736- public void testLoadBalancingPolicyFailover (ClickHouseLoadBalancingPolicy loadBalancingPolicy ) {
2737- String firstEndpoint = "111.1.1.1" ;
2738- String secondEndpoint = "222.2.2.2" ;
2737+ public void testLoadBalancingPolicyFailover (ClickHouseLoadBalancingPolicy loadBalancingPolicy ) throws IOException {
2738+ String endpoint = "127.0.0.1" ;
2739+ int [] ports = findUnusedPorts (endpoint , 2 );
2740+ int firstPort = ports [0 ];
2741+ int secondPort = ports [1 ];
27392742
27402743 Properties props = new Properties ();
27412744 props .setProperty ("failover" , "1" );
27422745
27432746 // nodes where the first node is failed
27442747 ClickHouseNodes nodes = ClickHouseNodes .of (
2745- getProtocol () + "://" + firstEndpoint + "," + secondEndpoint ,
2748+ getProtocol () + "://" + endpoint + ":" + firstPort + " ," + endpoint + ":" + secondPort ,
27462749 props
27472750 );
27482751
@@ -2753,21 +2756,47 @@ public void testLoadBalancingPolicyFailover(ClickHouseLoadBalancingPolicy loadBa
27532756 Assert .fail ("Exception expected for query on failed node" );
27542757 } catch (Exception failoverException ) {
27552758 ClickHouseNode failoverNode = loadBalancingPolicy .suggest (nodes , nodes .nodes .getFirst (), failoverException );
2756- Assert .assertEquals (failoverNode .getHost (), secondEndpoint , "The next node is expected to be suggested by the load balancing policy" );
2759+ Assert .assertEquals (failoverNode .getHost (), endpoint , "The next node is expected to be suggested by the load balancing policy" );
2760+ Assert .assertEquals (failoverNode .getPort (), secondPort , "The next node is expected to be suggested by the load balancing policy" );
2761+ }
2762+ }
2763+
2764+ /**
2765+ * Returns distinct TCP ports on the given local address that have no listener, so a connection to them is
2766+ * refused at once. All sockets stay open until every port is known, so the OS cannot return the same port twice.
2767+ */
2768+ private static int [] findUnusedPorts (String localAddress , int count ) throws IOException {
2769+ InetAddress address = InetAddress .getByName (localAddress );
2770+ ServerSocket [] sockets = new ServerSocket [count ];
2771+ try {
2772+ int [] ports = new int [count ];
2773+ for (int i = 0 ; i < count ; i ++) {
2774+ sockets [i ] = new ServerSocket (0 , 1 , address );
2775+ ports [i ] = sockets [i ].getLocalPort ();
2776+ }
2777+ return ports ;
2778+ } finally {
2779+ for (ServerSocket socket : sockets ) {
2780+ if (socket != null ) {
2781+ socket .close ();
2782+ }
2783+ }
27572784 }
27582785 }
27592786
27602787 @ Test (groups = {"integration" })
2761- public void testFailover () throws ClickHouseException {
2788+ public void testFailover () throws ClickHouseException , IOException {
27622789 if (isCloud ()) return ; //TODO: testFailover - Revisit, see: https://github.com/ClickHouse/clickhouse-java/issues/1747
27632790 ClickHouseNode availableNode = getServer ();
27642791 Properties props = new Properties ();
27652792 props .setProperty ("failover" , "1" );
27662793 props .setProperty (ClickHouseDefaults .PASSWORD .getKey (), getPassword ());
27672794
27682795 // nodes with the first node is unavailable
2796+ String unavailableEndpoint = "127.0.0.1" ;
2797+ int unavailablePort = findUnusedPorts (unavailableEndpoint , 1 )[0 ];
27692798 ClickHouseNodes nodes = ClickHouseNodes .of (
2770- getProtocol () + "://111.1.1.1 ," + availableNode .getBaseUri (),
2799+ getProtocol () + "://" + unavailableEndpoint + ":" + unavailablePort + " ," + availableNode .getBaseUri (),
27712800 props
27722801 );
27732802
0 commit comments