Skip to content

Commit 840a666

Browse files
authored
ZOOKEEPER-4920: Fix flaky test ZooKeeperServerMaxCnxnsTest
Reviewers: cnauroth Author: kezhuw Closes #2249 from kezhuw/ZOOKEEPER-4920-fix-flaky-ZooKeeperServerMaxCnxnsTest
1 parent ac19f22 commit 840a666

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testMaxZooKeeperClientsWithNettyServerCnxnFactory() throws Exception
6767

6868
private void testMaxZooKeeperClients(String serverCnxnFactory) throws Exception {
6969
final int clientPorts[] = new int[SERVER_COUNT];
70-
int maxCnxns = 2;
70+
final int maxCnxns = 2;
7171
StringBuilder sb = new StringBuilder();
7272
sb.append("maxCnxns=" + maxCnxns + "\n");
7373
sb.append("serverCnxnFactory=" + serverCnxnFactory + "\n");
@@ -108,9 +108,13 @@ public void process(WatchedEvent event) {
108108
}
109109
}
110110
};
111-
for (int i = 0; i < maxAllowedConnection; i++) {
112-
clients[i] = new ZooKeeper(cxnString, ClientBase.CONNECTION_TIMEOUT, watcher);
113-
Thread.sleep(100);
111+
// There are could be concurrent races to make maxCnxns restriction not accurate.
112+
// So we connect to each server with maxCnxns connections to overcome this.
113+
for (int i = 0; i < SERVER_COUNT; i++) {
114+
String addr = "127.0.0.1:" + clientPorts[i];
115+
for (int j = 0; j < maxCnxns; j++) {
116+
clients[i * maxCnxns + j] = new ZooKeeper(addr, ClientBase.CONNECTION_TIMEOUT, watcher);
117+
}
114118
}
115119
countDownLatch.await();
116120
// reaching this point indicates that all maxAllowedConnection connected

0 commit comments

Comments
 (0)