From 4685df37eea14fea06309d4c00616fd92cbe534b Mon Sep 17 00:00:00 2001 From: Lari Hotari Date: Wed, 10 May 2023 16:14:54 +0300 Subject: [PATCH] [fix][test] Fix flaky test ConnectionPoolTest.testSetProxyToTargetBrokerAddress Fixes #20290 --- .../client/impl/ConnectionPoolTest.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConnectionPoolTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConnectionPoolTest.java index fb564bd5083c1..fe0aa4dd4953b 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConnectionPoolTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConnectionPoolTest.java @@ -154,7 +154,7 @@ public void testEnableConnectionPool() throws Exception { @Test public void testSetProxyToTargetBrokerAddress() throws Exception { ClientConfigurationData conf = new ClientConfigurationData(); - conf.setConnectionsPerBroker(5); + conf.setConnectionsPerBroker(1); EventLoopGroup eventLoop = @@ -176,19 +176,24 @@ protected void doResolve(SocketAddress socketAddress, Promise promise) throws Ex @Override protected void doResolveAll(SocketAddress socketAddress, Promise promise) throws Exception { final InetSocketAddress socketAddress1 = (InetSocketAddress) socketAddress; - final boolean isProxy = socketAddress1.getHostName().equals("proxy"); - final boolean isBroker = socketAddress1.getHostName().equals("broker"); + String hostName = socketAddress1.getHostName(); + final boolean isProxy = hostName.equals("proxy"); + final boolean isBroker = hostName.startsWith("broker"); if (!isProxy && !isBroker) { promise.setFailure(new IllegalStateException()); throw new IllegalStateException(); } List result = new ArrayList<>(); + // All 127.0.0.0/8 addresses are valid local loopback addresses if (isProxy) { - result.add(new InetSocketAddress("localhost", brokerPort)); - result.add(InetSocketAddress.createUnresolved("proxy", brokerPort)); - } else { - result.add(new InetSocketAddress("127.0.0.1", brokerPort)); - result.add(InetSocketAddress.createUnresolved("broker", brokerPort)); + result.add(new InetSocketAddress("127.0.0.101", brokerPort)); + result.add(new InetSocketAddress("127.0.0.102", brokerPort)); + } else if (hostName.equals("broker1")){ + result.add(new InetSocketAddress("127.0.0.103", brokerPort)); + result.add(new InetSocketAddress("127.0.0.104", brokerPort)); + } else if (hostName.equals("broker2")){ + result.add(new InetSocketAddress("127.0.0.105", brokerPort)); + result.add(new InetSocketAddress("127.0.0.106", brokerPort)); } promise.setSuccess(result); } @@ -203,23 +208,19 @@ protected void doResolveAll(SocketAddress socketAddress, Promise promise) throws InetSocketAddress.createUnresolved("proxy", 9999)).get(); Assert.assertEquals(cnx.remoteHostName, "proxy"); Assert.assertNull(cnx.proxyToTargetBrokerAddress); - cnx.close(); cnx = pool.getConnection( - InetSocketAddress.createUnresolved("broker", 9999), + InetSocketAddress.createUnresolved("broker1", 9999), InetSocketAddress.createUnresolved("proxy", 9999)).get(); Assert.assertEquals(cnx.remoteHostName, "proxy"); - Assert.assertEquals(cnx.proxyToTargetBrokerAddress, "broker:9999"); - cnx.close(); + Assert.assertEquals(cnx.proxyToTargetBrokerAddress, "broker1:9999"); cnx = pool.getConnection( - InetSocketAddress.createUnresolved("broker", 9999), - InetSocketAddress.createUnresolved("broker", 9999)).get(); - Assert.assertEquals(cnx.remoteHostName, "broker"); + InetSocketAddress.createUnresolved("broker2", 9999), + InetSocketAddress.createUnresolved("broker2", 9999)).get(); + Assert.assertEquals(cnx.remoteHostName, "broker2"); Assert.assertNull(cnx.proxyToTargetBrokerAddress); - cnx.close(); - pool.closeAllConnections(); pool.close();