From 902c4f2f367f82f4f17bee13a0ae01f8f2dd6e1b Mon Sep 17 00:00:00 2001 From: Kezhu Wang Date: Thu, 3 Jul 2025 20:41:36 +0800 Subject: [PATCH] Fix flaky tests in TestReconfiguration `testAdd`, `testAddAsync` and `testAddAndRemove` asserts that `EnsembleProvider::setConnectionString` is updated with new connect string after ensemble changed. Since the update is asynchronous, so they should wait before assertion. --- .../framework/imps/TestReconfiguration.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java index 11a17421d..74f6850d1 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java @@ -256,13 +256,14 @@ public void testAddWithoutEnsembleTracker() throws Exception { @Test public void testAdd() throws Exception { - try (CuratorFramework client = newClient()) { + CountDownLatch ensembleLatch = new CountDownLatch(1); + try (CuratorFramework client = newClient(cluster.getConnectString(), ensembleLatch)) { client.start(); QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble()); assertConfig(oldConfig, cluster.getInstances()); - CountDownLatch latch = setChangeWaiter(client); + CountDownLatch eventLatch = setChangeWaiter(client); try (TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false))) { newCluster.start(); @@ -271,7 +272,8 @@ public void testAdd() throws Exception { .fromConfig(oldConfig.getVersion()) .forEnsemble(); - assertTrue(timing.awaitLatch(latch)); + assertTrue(timing.awaitLatch(eventLatch)); + assertTrue(timing.awaitLatch(ensembleLatch)); byte[] newConfigData = client.getConfig().forEnsemble(); QuorumVerifier newConfig = toQuorumVerifier(newConfigData); @@ -286,13 +288,14 @@ public void testAdd() throws Exception { @Test public void testAddAsync() throws Exception { - try (CuratorFramework client = newClient()) { + CountDownLatch ensembleLatch = new CountDownLatch(1); + try (CuratorFramework client = newClient(cluster.getConnectString(), ensembleLatch)) { client.start(); QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble()); assertConfig(oldConfig, cluster.getInstances()); - CountDownLatch latch = setChangeWaiter(client); + CountDownLatch eventLatch = setChangeWaiter(client); try (TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false))) { newCluster.start(); @@ -312,7 +315,8 @@ public void processResult(CuratorFramework client, CuratorEvent event) throws Ex .forEnsemble(); assertTrue(timing.awaitLatch(callbackLatch)); - assertTrue(timing.awaitLatch(latch)); + assertTrue(timing.awaitLatch(eventLatch)); + assertTrue(timing.awaitLatch(ensembleLatch)); byte[] newConfigData = client.getConfig().forEnsemble(); QuorumVerifier newConfig = toQuorumVerifier(newConfigData); @@ -327,13 +331,14 @@ public void processResult(CuratorFramework client, CuratorEvent event) throws Ex @Test public void testAddAndRemove() throws Exception { - try (CuratorFramework client = newClient()) { + CountDownLatch ensembleLatch = new CountDownLatch(1); + try (CuratorFramework client = newClient(cluster.getConnectString(), ensembleLatch)) { client.start(); QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble()); assertConfig(oldConfig, cluster.getInstances()); - CountDownLatch latch = setChangeWaiter(client); + CountDownLatch eventLatch = setChangeWaiter(client); try (TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false))) { newCluster.start(); @@ -355,7 +360,8 @@ public void testAddAndRemove() throws Exception { .fromConfig(oldConfig.getVersion()) .forEnsemble(); - assertTrue(timing.awaitLatch(latch)); + assertTrue(timing.awaitLatch(eventLatch)); + assertTrue(timing.awaitLatch(ensembleLatch)); byte[] newConfigData = client.getConfig().forEnsemble(); QuorumVerifier newConfig = toQuorumVerifier(newConfigData);