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 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 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 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(); } }
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 testExistsSync() { Assert.assertNull(lsnr.exists("/foo", true)); Assert.assertNull(lsnr.exists("/foo/bar", true)); client.create("/foo", "parent".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); expected.Add(Watcher.Event.EventType.NodeCreated); client.create("/foo/bar", "child".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); expected.Add(Watcher.Event.EventType.NodeCreated); verify(); Assert.assertNotNull(lsnr.exists("/foo", true)); Assert.assertNotNull(lsnr.exists("/foo/bar", true)); try { Assert.assertNull(lsnr.exists("/car", true)); client.setData("/car", "missing".getBytes(), -1); Assert.fail(); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NONODE, e.getCode()); Assert.assertEquals("/car", e.getPath()); } try { Assert.assertNull(lsnr.exists("/foo/car", true)); client.setData("/foo/car", "missing".getBytes(), -1); Assert.fail(); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NONODE, e.getCode()); Assert.assertEquals("/foo/car", e.getPath()); } client.setData("/foo", "parent".getBytes(), -1); expected.Add(Watcher.Event.EventType.NodeDataChanged); client.setData("/foo/bar", "child".getBytes(), -1); expected.Add(Watcher.Event.EventType.NodeDataChanged); verify(); Assert.assertNotNull(lsnr.exists("/foo", true)); Assert.assertNotNull(lsnr.exists("/foo/bar", true)); client.delete("/foo/bar", -1); expected.Add(Watcher.Event.EventType.NodeDeleted); client.delete("/foo", -1); expected.Add(Watcher.Event.EventType.NodeDeleted); verify(); }
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(); } } }