This is an automated email from the ASF dual-hosted git repository.

kezhuw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 840a666ce ZOOKEEPER-4920: Fix flaky test ZooKeeperServerMaxCnxnsTest
840a666ce is described below

commit 840a666cea95bb0c1e3cf59a83d9a112ae1c3dbe
Author: Kezhu Wang <[email protected]>
AuthorDate: Fri Apr 25 09:59:12 2025 +0800

    ZOOKEEPER-4920: Fix flaky test ZooKeeperServerMaxCnxnsTest
    
    Reviewers: cnauroth
    Author: kezhuw
    Closes #2249 from 
kezhuw/ZOOKEEPER-4920-fix-flaky-ZooKeeperServerMaxCnxnsTest
---
 .../apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git 
a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java
 
b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java
index fdb0b381f..35fe62bdc 100644
--- 
a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java
+++ 
b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java
@@ -67,7 +67,7 @@ public void 
testMaxZooKeeperClientsWithNettyServerCnxnFactory() throws Exception
 
     private void testMaxZooKeeperClients(String serverCnxnFactory) throws 
Exception {
         final int clientPorts[] = new int[SERVER_COUNT];
-        int maxCnxns = 2;
+        final int maxCnxns = 2;
         StringBuilder sb = new StringBuilder();
         sb.append("maxCnxns=" + maxCnxns + "\n");
         sb.append("serverCnxnFactory=" + serverCnxnFactory + "\n");
@@ -108,9 +108,13 @@ public void process(WatchedEvent event) {
                 }
             }
         };
-        for (int i = 0; i < maxAllowedConnection; i++) {
-            clients[i] = new ZooKeeper(cxnString, 
ClientBase.CONNECTION_TIMEOUT, watcher);
-            Thread.sleep(100);
+        // There are could be concurrent races to make maxCnxns restriction 
not accurate.
+        // So we connect to each server with maxCnxns connections to overcome 
this.
+        for (int i = 0; i < SERVER_COUNT; i++) {
+            String addr = "127.0.0.1:" + clientPorts[i];
+            for (int j = 0; j < maxCnxns; j++) {
+                clients[i * maxCnxns + j] = new ZooKeeper(addr, 
ClientBase.CONNECTION_TIMEOUT, watcher);
+            }
         }
         countDownLatch.await();
         // reaching this point indicates that all maxAllowedConnection 
connected

Reply via email to