ZOOKEEPER-3996: Fix flaky ReadOnlyModeTest.testConnectionEvents
The same watcher was resued across different clients. It is hard to know which event will last during verification, disconnected from old client or connected from new client.
A brand new watcher solves this.
Failed ci:
- https://github.com/apache/zookeeper/runs/6982815844?check_suite_focus=true
- https://github.com/kezhuw/zookeeper/runs/6996423868?check_suite_focus=true
#1667(ZOOKEEPER-4271) proposed another approach. But it have not updated for almost one year.
The old test could not last 10 runs after adding Thread.sleep(700) before watcher.waitForReadOnlyConnected(5000). The assertion message proves that "disconnected from old client" comes after "connected from new client".
After patch with watcher = new CountdownWatcher(), the test last 100 runs with no failure.
Submitted and Jira resolved. Thanks @kezhuw !