zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddiede...@apache.org
Subject [zookeeper] branch master updated: ZOOKEEPER-3992: addWatch api should check the null watch
Date Mon, 09 Nov 2020 11:21:28 GMT
This is an automated email from the ASF dual-hosted git repository.

ddiederen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new c9a1b59  ZOOKEEPER-3992: addWatch api should check the null watch
c9a1b59 is described below

commit c9a1b595f9afd07ce6359810a5ca8e4f4088c5a2
Author: Matteo Minardi <matteo.minardi@diennea.com>
AuthorDate: Mon Nov 9 11:20:11 2020 +0000

    ZOOKEEPER-3992: addWatch api should check the null watch
    
    ```
    public void addWatch(String basePath, Watcher watcher, AddWatchMode mode)
            throws KeeperException, InterruptedException {
        PathUtils.validatePath(basePath);
        String serverPath = prependChroot(basePath);
    
        RequestHeader h = new RequestHeader();
        h.setType(ZooDefs.OpCode.addWatch);
        AddWatchRequest request = new AddWatchRequest(serverPath, mode.getMode());
        ReplyHeader r = cnxn.submitRequest(h, request, new ErrorResponse(),
    ```
    we need to validateWatcher(watcher) to avoid the case:
    ```
    zk.addWatch("/a/b", null, PERSISTENT_RECURSIVE);
    ```
    
    Author: Matteo Minardi <matteo.minardi@diennea.com>
    
    Reviewers: Enrico Olivelli <eolivelli@apache.org>, Damien Diederen <ddiederen@apache.org>
    
    Closes #1529 from mino181295/fix/ZOOKEEPER-3992/addwatch-check-null
---
 .../src/main/java/org/apache/zookeeper/ZooKeeper.java     |  2 ++
 .../org/apache/zookeeper/test/PersistentWatcherTest.java  | 15 +++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java
index 622ccd5..9fba7a5 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java
@@ -2806,6 +2806,7 @@ public class ZooKeeper implements AutoCloseable {
     public void addWatch(String basePath, Watcher watcher, AddWatchMode mode)
             throws KeeperException, InterruptedException {
         PathUtils.validatePath(basePath);
+        validateWatcher(watcher);
         String serverPath = prependChroot(basePath);
 
         RequestHeader h = new RequestHeader();
@@ -2857,6 +2858,7 @@ public class ZooKeeper implements AutoCloseable {
             Object ctx
     ) {
         PathUtils.validatePath(basePath);
+        validateWatcher(watcher);
         String serverPath = prependChroot(basePath);
 
         RequestHeader h = new RequestHeader();
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java
index 754ba76..cad260c 100644
--- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java
@@ -21,6 +21,7 @@ package org.apache.zookeeper.test;
 import static org.apache.zookeeper.AddWatchMode.PERSISTENT;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import java.io.IOException;
 import java.util.concurrent.BlockingQueue;
@@ -63,6 +64,20 @@ public class PersistentWatcherTest extends ClientBase {
     }
 
     @Test
+    public void testNullWatch()
+            throws IOException, InterruptedException, KeeperException {
+        try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+            assertThrows(IllegalArgumentException.class, () -> {
+                zk.addWatch("/a/b", null, PERSISTENT);
+            });
+            assertThrows(IllegalArgumentException.class, () -> {
+                AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {};
+                zk.addWatch("/a/b", null, PERSISTENT, cb, null);
+            });
+        }
+    }
+
+    @Test
     public void testDefaultWatcher()
             throws IOException, InterruptedException, KeeperException {
         CountdownWatcher watcher = new CountdownWatcher() {


Mime
View raw message