Repository: zookeeper Updated Branches: refs/heads/branch-3.4 2de93fe45 -> 373b82bab
ZOOKEEPER-2692: Fix race condition in testWatchAutoResetWithPending Author: Abraham Fine <[email protected]> Reviewers: Michael Han <[email protected]> Closes #177 from afine/ZOOKEEPER-2692_34 Project: http://git-wip-us.apache.org/repos/asf/zookeeper/repo Commit: http://git-wip-us.apache.org/repos/asf/zookeeper/commit/373b82ba Tree: http://git-wip-us.apache.org/repos/asf/zookeeper/tree/373b82ba Diff: http://git-wip-us.apache.org/repos/asf/zookeeper/diff/373b82ba Branch: refs/heads/branch-3.4 Commit: 373b82bab843cc84c22b99f6511f8fea974fd2b4 Parents: 2de93fe Author: Abraham Fine <[email protected]> Authored: Wed Feb 15 16:18:15 2017 -0800 Committer: Michael Han <[email protected]> Committed: Wed Feb 15 16:18:15 2017 -0800 ---------------------------------------------------------------------- .../org/apache/zookeeper/test/WatcherTest.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zookeeper/blob/373b82ba/src/java/test/org/apache/zookeeper/test/WatcherTest.java ---------------------------------------------------------------------- diff --git a/src/java/test/org/apache/zookeeper/test/WatcherTest.java b/src/java/test/org/apache/zookeeper/test/WatcherTest.java index 893f335..1c06690 100644 --- a/src/java/test/org/apache/zookeeper/test/WatcherTest.java +++ b/src/java/test/org/apache/zookeeper/test/WatcherTest.java @@ -45,6 +45,8 @@ import org.junit.Test; public class WatcherTest extends ClientBase { protected static final Logger LOG = LoggerFactory.getLogger(WatcherTest.class); + private long timeOfLastWatcherInvocation; + private final class MyStatCallback implements StatCallback { int rc; public void processResult(int rc, String path, Object ctx, Stat stat) { @@ -60,6 +62,7 @@ public class WatcherTest extends ClientBase { public void process(WatchedEvent event) { super.process(event); if (event.getType() != Event.EventType.None) { + timeOfLastWatcherInvocation = System.currentTimeMillis(); try { events.put(event); } catch (InterruptedException e) { @@ -173,7 +176,6 @@ public class WatcherTest extends ClientBase { } final static int COUNT = 100; - boolean hasSeenDelete = true; /** * This test checks that watches for pending requests do not get triggered, * but watches set by previous requests do. @@ -207,7 +209,7 @@ public class WatcherTest extends ClientBase { startServer(); watches[COUNT/2-1].waitForConnected(60000); Assert.assertEquals(null, zk.exists("/test", false)); - Thread.sleep(10); + waitForAllWatchers(); for(int i = 0; i < COUNT/2; i++) { Assert.assertEquals("For " + i, 1, watches[i].events.size()); } @@ -221,6 +223,18 @@ public class WatcherTest extends ClientBase { Assert.assertEquals(COUNT, count[0]); zk.close(); } + + /** + * Wait until no watcher has been fired in the last second to ensure that all watches + * that are waiting to be fired have been fired + * @throws Exception + */ + private void waitForAllWatchers() throws Exception { + timeOfLastWatcherInvocation = System.currentTimeMillis(); + while (System.currentTimeMillis() - timeOfLastWatcherInvocation < 1000) { + Thread.sleep(1000); + } + } final int TIMEOUT = 5000;
