[ 
https://issues.apache.org/jira/browse/CURATOR-610?focusedWorklogId=635796&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-635796
 ]

ASF GitHub Bot logged work on CURATOR-610:
------------------------------------------

                Author: ASF GitHub Bot
            Created on: 09/Aug/21 08:04
            Start Date: 09/Aug/21 08:04
    Worklog Time Spent: 10m 
      Work Description: wx930910 commented on a change in pull request #397:
URL: https://github.com/apache/curator/pull/397#discussion_r684988692



##########
File path: 
curator-framework/src/test/java/org/apache/curator/framework/imps/TestWatcherIdentity.java
##########
@@ -30,169 +36,141 @@
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
+import com.google.common.collect.Sets;
 
-public class TestWatcherIdentity extends BaseClassForTests
-{
-    private static final String PATH = "/foo";
-
-    private static class CountCuratorWatcher implements CuratorWatcher
-    {
-        private final AtomicInteger count = new AtomicInteger(0);
-
-        @Override
-        public void process(WatchedEvent event) throws Exception
-        {
-            count.incrementAndGet();
-        }
-    }
-
-    private static class CountZKWatcher implements Watcher
-    {
-        private final AtomicInteger count = new AtomicInteger(0);
-
-        @Override
-        public void process(WatchedEvent event)
-        {
-            count.incrementAndGet();
-        }
-    }
-
-    @Test
-    public void testSameWatcherPerZKDocs() throws Exception
-    {
-        CountZKWatcher actualWatcher = new CountZKWatcher();
-        Timing timing = new Timing();
-        CuratorFramework client = 
CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 
timing.connection(), new RetryOneTime(1));
-        try
-        {
-            client.start();
-            client.create().forPath("/test");
-
-            // per ZK docs, this watcher should only trigger once
-            client.checkExists().usingWatcher(actualWatcher).forPath("/test");
-            client.getData().usingWatcher(actualWatcher).forPath("/test");
-
-            client.setData().forPath("/test", "foo".getBytes());
-            client.delete().forPath("/test");
-            timing.sleepABit();
-            assertEquals(actualWatcher.count.getAndSet(0), 1);
-
-            client.create().forPath("/test");
-            client.checkExists().usingWatcher(actualWatcher).forPath("/test");
-            client.delete().forPath("/test");
-            timing.sleepABit();
-            assertEquals(actualWatcher.count.get(), 1);
-        }
-        finally
-        {
-            CloseableUtils.closeQuietly(client);
-        }
-    }
-
-    @Test
-    public void testSameCuratorWatcherPerZKDocs() throws Exception
-    {
-        CountCuratorWatcher actualWatcher = new CountCuratorWatcher();
-        Timing timing = new Timing();
-        CuratorFramework client = 
CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 
timing.connection(), new RetryOneTime(1));
-        try
-        {
-            client.start();
-            client.create().forPath("/test");
-
-            // per ZK docs, this watcher should only trigger once
-            client.checkExists().usingWatcher(actualWatcher).forPath("/test");
-            client.getData().usingWatcher(actualWatcher).forPath("/test");
-
-            client.setData().forPath("/test", "foo".getBytes());
-            client.delete().forPath("/test");
-            timing.sleepABit();
-            assertEquals(actualWatcher.count.getAndSet(0), 1);
-
-            client.create().forPath("/test");
-            client.checkExists().usingWatcher(actualWatcher).forPath("/test");
-            client.delete().forPath("/test");
-            timing.sleepABit();
-            assertEquals(actualWatcher.count.get(), 1);
-        }
-        finally
-        {
-            CloseableUtils.closeQuietly(client);
-        }
-    }
-
-    @Test
-    public void testSetAddition()
-    {
-        Watcher watcher = new Watcher()
-        {
-            @Override
-            public void process(WatchedEvent event)
-            {
-
-            }
-        };
-        NamespaceWatcher namespaceWatcher1 = new NamespaceWatcher(null, 
watcher, "/foo");
-        NamespaceWatcher namespaceWatcher2 = new NamespaceWatcher(null, 
watcher, "/foo");
-        assertEquals(namespaceWatcher1, namespaceWatcher2);
-        assertFalse(namespaceWatcher1.equals(watcher));
-        assertFalse(watcher.equals(namespaceWatcher1));
-        Set<Watcher> set = Sets.newHashSet();
-        set.add(namespaceWatcher1);
-        set.add(namespaceWatcher2);
-        assertEquals(set.size(), 1);
-    }
-
-    @Test
-    public void testCuratorWatcher() throws Exception
-    {
-        Timing timing = new Timing();
-        CountCuratorWatcher watcher = new CountCuratorWatcher();
-        CuratorFramework client = 
CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 
timing.connection(), new RetryOneTime(1));
-        try
-        {
-            client.start();
-            client.create().forPath(PATH);
-            // Add twice the same watcher on the same path
-            client.getData().usingWatcher(watcher).forPath(PATH);
-            client.getData().usingWatcher(watcher).forPath(PATH);
-            // Ok, let's test it
-            client.setData().forPath(PATH, new byte[]{});
-            timing.sleepABit();
-            assertEquals(1, watcher.count.get());
-        }
-        finally
-        {
-            CloseableUtils.closeQuietly(client);
-        }
-    }
-
-
-    @Test
-    public void testZKWatcher() throws Exception
-    {
-        Timing timing = new Timing();
-        CountZKWatcher watcher = new CountZKWatcher();
-        CuratorFramework client = 
CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 
timing.connection(), new RetryOneTime(1));
-        try
-        {
-            client.start();
-            client.create().forPath(PATH);
-            // Add twice the same watcher on the same path
-            client.getData().usingWatcher(watcher).forPath(PATH);
-            client.getData().usingWatcher(watcher).forPath(PATH);
-            // Ok, let's test it
-            client.setData().forPath(PATH, new byte[]{});
-            timing.sleepABit();
-            assertEquals(1, watcher.count.get());
-        }
-        finally
-        {
-            CloseableUtils.closeQuietly(client);
-        }
-    }
+public class TestWatcherIdentity extends BaseClassForTests {
+       private static final String PATH = "/foo";
+       private static final int TIMEOUT_MS = 100000;
+
+       private static class CountZKWatcher implements Watcher {
+               private final AtomicInteger count = new AtomicInteger(0);
+
+               @Override
+               public void process(WatchedEvent event) {
+                       count.incrementAndGet();
+               }
+       }
+
+       @Test
+       public void testSameWatcherPerZKDocs() throws Exception {
+               CountZKWatcher actualWatcher = new CountZKWatcher();
+               Timing timing = new Timing();
+               CuratorFramework client = 
CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
+                               timing.connection(), new RetryOneTime(1));
+               try {
+                       client.start();
+                       client.create().forPath("/test");
+
+                       // per ZK docs, this watcher should only trigger once
+                       
client.checkExists().usingWatcher(actualWatcher).forPath("/test");
+                       
client.getData().usingWatcher(actualWatcher).forPath("/test");
+
+                       client.setData().forPath("/test", "foo".getBytes());
+                       client.delete().forPath("/test");
+                       Awaitility.waitAtMost(Duration.ofMillis(TIMEOUT_MS))

Review comment:
       Changed `waitAtMost(Duration)` to `await()`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 635796)
    Time Spent: 50m  (was: 40m)

> Refactor CountCuratorWatcher in TestWatcherIdentity.java to improve test logic
> ------------------------------------------------------------------------------
>
>                 Key: CURATOR-610
>                 URL: https://issues.apache.org/jira/browse/CURATOR-610
>             Project: Apache Curator
>          Issue Type: Improvement
>          Components: Framework
>            Reporter: Xiao Wang
>            Priority: Minor
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> h3. Description
> I noticed that there is a test class 
> [CountCuratorWatcher|https://github.com/apache/curator/blob/4a11aaef8b190dc220d35b7a91df294bfa06250e/curator-framework/src/test/java/org/apache/curator/framework/imps/TestWatcherIdentity.java#L42]
>  implements production interface 
> [curatorWatcher|https://github.com/apache/curator/blob/4a11aaef8b190dc220d35b7a91df294bfa06250e/curator-framework/src/main/java/org/apache/curator/framework/api/CuratorWatcher.java#L27]
>  to assist testing production class 
> [CuratorFrameworkImpl|https://github.com/apache/curator/blob/4a11aaef8b190dc220d35b7a91df294bfa06250e/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java#L66].
>  This might not be the best priactice in unit testing and can be improved by 
> leveraging mocking frameworks.
> h3. Current Implementation
> *  {{CountCuratorWatcher}} implements {{CuratorWatcher}} and creates a new 
> variable to keep tracking of the method invocation status for 
> {{process(WatchedEvent)}}.
> * In test cases, after executing test target, the new variable will be used 
> in assertion statement to check the execution status of 
> {{process(WatchedEvent)}}.
> h3. Proposed Implementation
>  * Replace {{CountCuratorWatcher}} with a mocking object created by Mockito.
>  * Extract the AtomicLong attribute and use the extracted attribute to check 
> method invocation status.
>  * Use method stub to control the behavior of the mocking object.
> h3. Motivation
>  - Decouple test class `CountCuratorWatcher` from production interface 
> `CuratorWatcher`.
>  - Make test logic more clear by using method stub instead of method 
> overriding.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to