public void testACLs() { ZooKeeper zk = null; try { zk = createClient(); try { zk.create("/acltest", new byte[0], ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); Assert.fail("Should have received an invalid acl error"); } catch (KeeperException.InvalidACLException e) { LOG.info("Test successful, invalid acl received : " + e.Message); } try { List <ACL> testACL = new List <ACL>(); testACL.Add(new ACL((int)(ZooDefs.Perms.ALL | ZooDefs.Perms.ADMIN), ZooDefs.Ids.AUTH_IDS)); testACL.Add(new ACL((int)(ZooDefs.Perms.ALL | ZooDefs.Perms.ADMIN), new Id("ip", "127.0.0.1/8"))); zk.create("/acltest", new byte[0], testACL, CreateMode.PERSISTENT); Assert.fail("Should have received an invalid acl error"); } catch (KeeperException.InvalidACLException e) { LOG.info("Test successful, invalid acl received : " + e.Message); } zk.addAuthInfo("digest", "ben:passwd".getBytes()); zk.create("/acltest", new byte[0], ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); zk.close(); zk = createClient(); zk.addAuthInfo("digest", "ben:passwd2".getBytes()); try { zk.getData("/acltest", false, new Stat()); Assert.fail("Should have received a permission error"); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NOAUTH, e.getCode()); } zk.addAuthInfo("digest", "ben:passwd".getBytes()); zk.getData("/acltest", false, new Stat()); zk.setACL("/acltest", ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); zk.close(); zk = createClient(); zk.getData("/acltest", false, new Stat()); IList <ACL> acls = zk.getACL("/acltest", new Stat()); Assert.assertEquals(1, acls.Count); Assert.assertEquals(ZooDefs.Ids.OPEN_ACL_UNSAFE, acls); zk.close(); } finally { if (zk != null) { zk.close(); } } }
protected override void run() { byte[] b = new byte[256]; try { for (; current < count; current++) { // Simulate a bit of network latency... Thread.Sleep(HAMMERTHREAD_LATENCY); zk.create(prefix + current, b, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (Exception t) { LOG.error("Client create operation Assert.failed", t); } finally { try { zk.close(); } catch (ThreadInterruptedException e) { LOG.warn("Unexpected", e); } } }
protected override void run() { byte[] b = new byte[256]; try { for (; current < count; current++) { ZooKeeper zk = parent.createClient(); try { zk.create(prefix + current, b, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } finally { try { zk.close(); } catch (ThreadInterruptedException e) { LOG.warn("Unexpected", e); } } } } catch (Exception t) { LOG.error("Client create operation Assert.failed", t); } }
public void KillSession(TimeSpan timeout) { if (!IsConnected) { return; } var connectionString = Curator.getZookeeperClient().getCurrentConnectionString(); var zooKeeper = new ZooKeeper(connectionString, 5000, null, SessionId, SessionPassword); try { var watch = Stopwatch.StartNew(); while (watch.Elapsed < timeout) { if (zooKeeper.getState().Equals(ZooKeeper.States.CONNECTED)) { return; } Thread.Sleep(100); } throw new TimeoutException($"Expected to kill session within {timeout}, but failed to do so."); } finally { zooKeeper.close(); } }
public void testDeleteWithChildren() { ZooKeeper zk = createClient(); zk.create("/parent", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/parent/child", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); try { zk.delete("/parent", -1); Assert.fail("Should have received a not equals message"); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NOTEMPTY, e.getCode()); } zk.delete("/parent/child", -1); zk.delete("/parent", -1); zk.close(); }
public void testLargeNodeData() { ZooKeeper zk = null; const string queue_handle = "/large"; try { zk = createClient(); zk.create(queue_handle, new byte[500000], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } finally { if (zk != null) { zk.close(); } } }
public void testWatcherCorrectness() { ZooKeeper zk = null; try { MyWatcher watcher = new MyWatcher(); zk = createClient(watcher); string[] names = new string[10]; for (int i = 0; i < names.Length; i++) { string name = zk.create("/tc-", "initialvalue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); names[i] = name; Stat stat = new Stat(); zk.getData(name, watcher, stat); zk.setData(name, "new".getBytes(), stat.getVersion()); stat = zk.exists(name, watcher); zk.delete(name, stat.getVersion()); } for (int i = 0; i < names.Length; i++) { string name = names[i]; WatchedEvent @event = watcher.events.poll(10 * 1000); Assert.assertEquals(name, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); @event = watcher.events.poll(10 * 1000); Assert.assertEquals(name, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); } } finally { if (zk != null) { zk.close(); } } }
public void testSequentialNodeNames() { string path = "/SEQUENCE"; string file = "TEST"; string filepath = path + "/" + file; ZooKeeper zk = null; try { zk = createClient(); zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); IList <string> children = zk.getChildren(path, false); Assert.assertEquals(1, children.Count); Assert.assertEquals(file + "0000000000", children[0]); zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); children = zk.getChildren(path, false); Assert.assertEquals(2, children.Count); Assert.assertTrue("contains child 1", children.Contains(file + "0000000001")); zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); children = zk.getChildren(path, false); Assert.assertEquals(3, children.Count); Assert.assertTrue("contains child 2", children.Contains(file + "0000000002")); // The pattern is holding so far. Let's run the counter a bit // to be sure it continues to spit out the correct answer for (int i = children.Count; i < 105; i++) { zk.create(filepath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); } children = zk.getChildren(path, false); Assert.assertTrue("contains child 104", children.Contains(file + "0000000104")); } finally { if (zk != null) { zk.close(); } } }
public void testSequentialNodeData() { ZooKeeper zk = null; const string queue_handle = "/queue"; try { zk = createClient(); zk.create(queue_handle, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create(queue_handle + "/element", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); zk.create(queue_handle + "/element", "1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); IList <string> children = zk.getChildren(queue_handle, true); Assert.assertEquals(children.Count, 2); string child1 = children[0]; string child2 = children[1]; int compareResult = child1.CompareTo(child2); Assert.assertNotEquals(compareResult, 0); if (compareResult < 0) { } else { string temp = child1; child1 = child2; child2 = temp; } string child1data = Encoding.UTF8.GetString(zk.getData(queue_handle + "/" + child1, false, null)); string child2data = Encoding.UTF8.GetString(zk.getData(queue_handle + "/" + child2, false, null)); Assert.assertEquals(child1data, "0"); Assert.assertEquals(child2data, "1"); } finally { if (zk != null) { zk.close(); } } }
public void testHammerSuper() { try { const int threadCount = 5; const int childCount = 10; HammerThread[] threads = new HammerThread[threadCount]; long start = TimeHelper.ElapsedMiliseconds; for (int i = 0; i < threads.Length; i++) { string prefix = "/test-" + i; { ZooKeeper zk = createClient(); try { zk.create(prefix, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } finally { zk.close(); } } prefix += "/"; HammerThread thread = new SuperHammerThread("SuperHammerThread-" + i, this, prefix, childCount); thread.start(); threads[i] = thread; } verifyHammer(start, threads, childCount); } catch (Exception t) { LOG.error("test Assert.failed", t); throw; } }
public void testPing() { ZooKeeper zkIdle = null; ZooKeeper zkWatchCreator = null; try { CountdownWatcher watcher = new CountdownWatcher(); zkIdle = createClient(watcher, 10000); zkWatchCreator = createClient(); for (int i = 0; i < 10; i++) { zkWatchCreator.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } for (int i = 0; i < 10; i++) { zkIdle.exists("/" + i, true); } for (int i = 0; i < 10; i++) { Thread.Sleep(1000); zkWatchCreator.delete("/" + i, -1); } // The bug will manifest itself here because zkIdle will expire zkIdle.exists("/0", false); } finally { if (zkIdle != null) { zkIdle.close(); } if (zkWatchCreator != null) { zkWatchCreator.close(); } } }
public void testNullData() { const string path = "/SIZE"; ZooKeeper zk = null; zk = createClient(); try { ManualResetEventSlim cn = new ManualResetEventSlim(false); zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // try sync zk exists zk.exists(path, false); zk.existsAsync(path, false).ContinueWith(t => { cn.Set(); }); cn.Wait(10 * 1000); Assert.assertTrue(cn.IsSet); } finally { if (zk != null) { zk.close(); } } }
public void testRootAcl() { ZooKeeper zk = createClient(); try { // set auth using digest zk.addAuthInfo("digest", "pat:test".getBytes()); zk.setACL("/", ZooDefs.Ids.CREATOR_ALL_ACL, -1); zk.getData("/", false, null); zk.close(); // verify no access zk = createClient(); try { zk.getData("/", false, null); Assert.fail("validate auth"); } catch (KeeperException.NoAuthException) { // expected } try { zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); Assert.fail("validate auth"); } catch (KeeperException.InvalidACLException) { // expected } zk.addAuthInfo("digest", "world:anyone".getBytes()); try { zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); Assert.fail("validate auth"); } catch (KeeperException.NoAuthException) { // expected } zk.close(); // verify access using original auth zk = createClient(); zk.addAuthInfo("digest", "pat:test".getBytes()); zk.getData("/", false, null); zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); zk.delete("/apps", -1); // reset acl (back to open) and verify accessible again zk.setACL("/", ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); zk.close(); zk = createClient(); zk.getData("/", false, null); zk.create("/apps", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); try { zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); Assert.fail("validate auth"); } catch (KeeperException.InvalidACLException) { // expected } zk.delete("/apps", -1); zk.addAuthInfo("digest", "world:anyone".getBytes()); zk.create("/apps", null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); zk.close(); zk = createClient(); zk.delete("/apps", -1); } finally { zk.close(); } }
public override void tearDown() { base.tearDown(); zk.close(); }
public override void tearDown() { client.close(); lsnr.close(); base.tearDown(); }
public void testMutipleWatcherObjs() { ZooKeeper zk = createClient(new CountdownWatcher()); try { MyWatcher[] watchers = new MyWatcher[100]; MyWatcher[] watchers2 = new MyWatcher[watchers.Length]; for (int i = 0; i < watchers.Length; i++) { watchers[i] = new MyWatcher(); watchers2[i] = new MyWatcher(); zk.create("/foo-" + i, ("foodata" + i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } Stat stat = new Stat(); // // test get/exists with single set of watchers // get all, then exists all // for (int i = 0; i < watchers.Length; i++) { Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); } for (int i = 0; i < watchers.Length; i++) { Assert.assertNotNull(zk.exists("/foo-" + i, watchers[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.setData("/foo-" + i, ("foodata2-" + i).getBytes(), -1); zk.setData("/foo-" + i, ("foodata3-" + i).getBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event = watchers[i].events.poll(10 * 1000); Assert.assertEquals("/foo-" + i, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.assertEquals(0, watchers[i].events.size()); } // // test get/exists with single set of watchers // get/exists together // for (int i = 0; i < watchers.Length; i++) { Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); Assert.assertNotNull(zk.exists("/foo-" + i, watchers[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.setData("/foo-" + i, ("foodata4-" + i).getBytes(), -1); zk.setData("/foo-" + i, ("foodata5-" + i).getBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event = watchers[i].events.poll(10 * 1000); Assert.assertEquals("/foo-" + i, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.assertEquals(0, watchers[i].events.size()); } // // test get/exists with two sets of watchers // for (int i = 0; i < watchers.Length; i++) { Assert.assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); Assert.assertNotNull(zk.exists("/foo-" + i, watchers2[i])); } // trigger the watches for (int i = 0; i < watchers.Length; i++) { zk.setData("/foo-" + i, ("foodata6-" + i).getBytes(), -1); zk.setData("/foo-" + i, ("foodata7-" + i).getBytes(), -1); } for (int i = 0; i < watchers.Length; i++) { WatchedEvent @event = watchers[i].events.poll(10 * 1000); Assert.assertEquals("/foo-" + i, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.assertEquals(0, watchers[i].events.size()); // watchers2 WatchedEvent event2 = watchers2[i].events.poll(10 * 1000); Assert.assertEquals("/foo-" + i, event2.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, event2.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, event2.getState()); // small chance that an unexpected message was delivered // after this check, but we would catch that next time // we check events Assert.assertEquals(0, watchers2[i].events.size()); } } finally { if (zk != null) { zk.close(); } } }
private void verifyHammer(long start, HammerThread[] threads, int childCount) { // look for the clients to finish their create operations LOG.info("Starting check for completed hammers"); int workingCount = threads.Length; for (int i = 0; i < 120; i++) { Thread.Sleep(10000); foreach (HammerThread h in threads) { if (!h.isAlive() || h.current == h.count) { workingCount--; } } if (workingCount == 0) { break; } workingCount = threads.Length; } if (workingCount > 0) { foreach (HammerThread h in threads) { LOG.warn(h.getName() + " never finished creation, current:" + h.current); } } else { LOG.info("Hammer threads completed creation operations"); } foreach (HammerThread h in threads) { const int safetyFactor = 3; verifyThreadTerminated(h, threads.Length * childCount * HAMMERTHREAD_LATENCY * safetyFactor); } LOG.info(DateTime.Now + " Total time " + (TimeHelper.ElapsedMiliseconds - start)); ZooKeeper zk = createClient(); try { LOG.info("******************* Connected to ZooKeeper" + DateTime.Now); for (int i = 0; i < threads.Length; i++) { LOG.info("Doing thread: " + i + " " + DateTime.Now); IList <string> children = zk.getChildren("/test-" + i, false); Assert.assertEquals(childCount, children.Count); children = zk.getChildren("/test-" + i, false, null); Assert.assertEquals(childCount, children.Count); } for (int i = 0; i < threads.Length; i++) { IList <string> children = zk.getChildren("/test-" + i, false); Assert.assertEquals(childCount, children.Count); children = zk.getChildren("/test-" + i, false, null); Assert.assertEquals(childCount, children.Count); } } finally { zk.close(); } }
private void performClientTest(bool withWatcherObj) { ZooKeeper zk = null; try { MyWatcher watcher = new MyWatcher(); zk = createClient(watcher); LOG.info("Before create /benwashere"); zk.create("/benwashere", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); LOG.info("After create /benwashere"); try { zk.setData("/benwashere", "hi".getBytes(), 57); Assert.fail("Should have gotten BadVersion exception"); } catch (KeeperException.BadVersionException) { // expected that } catch (KeeperException) { Assert.fail("Should have gotten BadVersion exception"); } LOG.info("Before delete /benwashere"); zk.delete("/benwashere", 0); LOG.info("After delete /benwashere"); zk.close(); //LOG.info("Closed client: " + zk.describeCNXN()); Thread.Sleep(2000); zk = createClient(watcher); //LOG.info("Created a new client: " + zk.describeCNXN()); LOG.info("Before delete /"); try { zk.delete("/", -1); Assert.fail("deleted root!"); } catch (KeeperException.BadArgumentsException) { // good, expected that } Stat stat = new Stat(); // Test basic create, ls, and getData zk.create("/pat", "Pat was here".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); LOG.info("Before create /ben"); zk.create("/pat/ben", "Ben was here".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); LOG.info("Before getChildren /pat"); List <string> children = zk.getChildren("/pat", false); Assert.assertEquals(1, children.Count); Assert.assertEquals("ben", children[0]); IList <string> children2 = zk.getChildren("/pat", false, null); Assert.assertEquals(children, children2); string value = Encoding.UTF8.GetString(zk.getData("/pat/ben", false, stat)); Assert.assertEquals("Ben was here", value); // Test stat and watch of non existent node try { if (withWatcherObj) { Assert.assertEquals(null, zk.exists("/frog", watcher)); } else { Assert.assertEquals(null, zk.exists("/frog", true)); } LOG.info("Comment: asseting passed for frog setting /"); } catch (KeeperException.NoNodeException) { // OK, expected that } zk.create("/frog", "hi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // the first poll is just a session delivery LOG.info("Comment: checking for events length " + watcher.events.size()); WatchedEvent @event = watcher.events.poll(10 * 1000); Assert.assertEquals("/frog", @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeCreated, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); // Test child watch and create with sequence zk.getChildren("/pat/ben", true); for (int i = 0; i < 10; i++) { zk.create("/pat/ben/" + i + "-", Convert.ToString(i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); } children = zk.getChildren("/pat/ben", false); children.Sort(); Assert.assertEquals(10, children.Count); for (int i = 0; i < 10; i++) { string name = children[i]; Assert.assertTrue("starts with -", name.StartsWith(i + "-", StringComparison.Ordinal)); byte[] b; if (withWatcherObj) { b = zk.getData("/pat/ben/" + name, watcher, stat); } else { b = zk.getData("/pat/ben/" + name, true, stat); } Assert.assertEquals(i, int.Parse(Encoding.UTF8.GetString(b))); zk.setData("/pat/ben/" + name, "new".getBytes(), stat.getVersion()); if (withWatcherObj) { stat = zk.exists("/pat/ben/" + name, watcher); } else { stat = zk.exists("/pat/ben/" + name, true); } zk.delete("/pat/ben/" + name, stat.getVersion()); } @event = watcher.events.poll(10 * 1000); Assert.assertEquals("/pat/ben", @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeChildrenChanged, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); for (int i = 0; i < 10; i++) { @event = watcher.events.poll(10 * 1000); string name = children[i]; Assert.assertEquals("/pat/ben/" + name, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDataChanged, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); @event = watcher.events.poll(10 * 1000); Assert.assertEquals("/pat/ben/" + name, @event.getPath()); Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, @event.get_Type()); Assert.assertEquals(Watcher.Event.KeeperState.SyncConnected, @event.getState()); } zk.create("/good\u0040path", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/duplicate", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); try { zk.create("/duplicate", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.fail("duplicate create allowed"); } catch (KeeperException.NodeExistsException) { // OK, expected that } } finally { if (zk != null) { zk.close(); } } }
public void testChrootSynchronous() { ZooKeeper zk1 = createClient(); try { zk1.create("/ch1", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } finally { if (zk1 != null) { zk1.close(); } } ZooKeeper zk2 = createClient("/ch1"); try { Assert.assertEquals("/ch2", zk2.create("/ch2", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); } finally { if (zk2 != null) { zk2.close(); } } zk1 = createClient(); zk2 = createClient("/ch1"); try { // check get MyWatcher w1 = new MyWatcher("/ch1"); Assert.assertNotNull(zk1.exists("/ch1", w1)); MyWatcher w2 = new MyWatcher("/ch1/ch2"); Assert.assertNotNull(zk1.exists("/ch1/ch2", w2)); MyWatcher w3 = new MyWatcher("/ch2"); Assert.assertNotNull(zk2.exists("/ch2", w3)); // set watches on child MyWatcher w4 = new MyWatcher("/ch1"); zk1.getChildren("/ch1", w4); MyWatcher w5 = new MyWatcher("/"); zk2.getChildren("/", w5); // check set zk1.setData("/ch1", "1".getBytes(), -1); zk2.setData("/ch2", "2".getBytes(), -1); // check watches Assert.assertTrue(w1.matches()); Assert.assertTrue(w2.matches()); Assert.assertTrue(w3.matches()); // check exceptions try { zk2.setData("/ch3", "3".getBytes(), -1); } catch (KeeperException.NoNodeException e) { Assert.assertEquals("/ch3", e.getPath()); } Assert.assertEquals("1".getBytes(), zk1.getData("/ch1", false, null)); Assert.assertEquals("2".getBytes(), zk1.getData("/ch1/ch2", false, null)); Assert.assertEquals("2".getBytes(), zk2.getData("/ch2", false, null)); // check delete zk2.delete("/ch2", -1); Assert.assertTrue(w4.matches()); Assert.assertTrue(w5.matches()); zk1.delete("/ch1", -1); Assert.assertNull(zk1.exists("/ch1", false)); Assert.assertNull(zk1.exists("/ch1/ch2", false)); Assert.assertNull(zk2.exists("/ch2", false)); } finally { if (zk1 != null) { zk1.close(); } if (zk2 != null) { zk2.close(); } } }