Beispiel #1
0
        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();
                }
            }
        }
Beispiel #2
0
        public void testChRootTransaction()
        {
            // creating the subtree for chRoot clients.
            string chRoot = createNameSpace();

            // checking the child version using chRoot client.
            zk_chroot = createClient(chRoot);
            const string childPath   = "/myid";
            Transaction  transaction = zk_chroot.transaction();

            transaction.create(childPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            transaction.check(childPath, 0);
            transaction.setData(childPath, childPath.getBytes(), 0);
            transaction.commit();

            Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk.exists(chRoot + childPath, false));
            Assert.assertNotNull("zNode is not created under chroot:" + chRoot, zk_chroot.exists(childPath, false));
            Assert.assertNull("zNode is created directly under '/', ignored configured chroot", zk.exists(childPath, false));
            Assert.assertEquals("zNode data not matching", childPath.getBytes(), zk_chroot.getData(childPath, false, null));

            transaction = zk_chroot.transaction();
            // Deleting child using chRoot client.
            transaction.delete(childPath, 1);
            transaction.commit();

            Assert.assertNull("chroot:" + chRoot + " exists after delete", zk.exists(chRoot + "/myid", false));
            Assert.assertNull("chroot:" + chRoot + " exists after delete", zk_chroot.exists("/myid", false));
        }
Beispiel #3
0
        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();
                }
            }
        }
Beispiel #4
0
        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();
                }
            }
        }
Beispiel #5
0
        public void testChRootSetData()
        {
            // creating the subtree for chRoot clients.
            string chRoot = createNameSpace();

            // setData using chRoot client.
            zk_chroot = createClient(chRoot);
            string[]  names = new string[] { "/multi0", "/multi1", "/multi2" };
            List <Op> ops   = new List <Op>();

            for (int i = 0; i < names.Length; i++)
            {
                ops.Add(Op.create(names[i], new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
                ops.Add(Op.setData(names[i], names[i].getBytes(), 0));
            }

            zk_chroot.multi(ops);

            for (int i = 0; i < names.Length; i++)
            {
                Assert.assertEquals("zNode data not matching", names[i].getBytes(), zk_chroot.getData(names[i], false, null));
            }
        }
Beispiel #6
0
        public void testBasic()
        {
            const string name = "/foo";

            zk.create(name, name.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

            var stat = newStat();

            zk.getData(name, false, stat);

            Assert.assertEquals(stat.getCzxid(), stat.getMzxid());
            Assert.assertEquals(stat.getCzxid(), stat.getPzxid());
            Assert.assertEquals(stat.getCtime(), stat.getMtime());
            Assert.assertEquals(0, stat.getCversion());
            Assert.assertEquals(0, stat.getVersion());
            Assert.assertEquals(0, stat.getAversion());
            Assert.assertEquals(0, stat.getEphemeralOwner());
            Assert.assertEquals(name.Length, stat.getDataLength());
            Assert.assertEquals(0, stat.getNumChildren());
        }
Beispiel #7
0
        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();
            }
        }
Beispiel #8
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void getConf(org.apache.zookeeper.ZooKeeper zk, String groupName, String displayName, java.util.List<String> retList) throws org.apache.zookeeper.KeeperException, InterruptedException
		private void getConf(ZooKeeper zk, string groupName, string displayName, IList<string> retList)
		{
			try
			{

				StringBuilder sb = new StringBuilder();

				int pathLength = StringUtils.countMatches(groupName, "/");
				for (int i = 0; i < pathLength - 2; ++i)
				{
					sb.Append("\t");
				}

				IList<string> children = zk.getChildren(groupName, false);

				if (!"/".Equals(groupName))
				{

					sb.Append("|----" + displayName);
					Stat stat = new Stat();
					sbyte[] data = zk.getData(groupName, null, stat);

					if (data != null && children.Count == 0)
					{
						sb.Append("\t" + StringHelperClass.NewString(data, CHARSET));
					}
				}
				else
				{
					sb.Append(groupName);
				}
				retList.Add(sb.ToString());

				//
				//
				//
				children.Sort(Collator.getInstance(java.util.Locale.CHINA));
				foreach (string child in children)
				{

					string nextName = "";

					if (!"/".Equals(groupName))
					{

						nextName = groupName + "/" + child;

					}
					else
					{
						nextName = groupName + "/" + child;
					}

					string node = StringUtils.substringAfterLast(nextName, "/");

					getConf(zk, groupName + "/" + child, node, retList);
				}

			}
			catch (KeeperException.NoNodeException)
			{
				LOG.error("Group " + groupName + " does not exist\n");
			}

		}
Beispiel #9
0
		/// <summary>
		/// 获取指定 配置数据
		/// 
		/// @return
		/// </summary>
		/// <exception cref="InterruptedException"> </exception>
		/// <exception cref="KeeperException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private com.baidu.disconf.web.service.zookeeper.dto.ZkDisconfData getDisconfData(String path, String keyName, org.apache.zookeeper.ZooKeeper zooKeeper) throws org.apache.zookeeper.KeeperException, InterruptedException
		private ZkDisconfData getDisconfData(string path, string keyName, ZooKeeper zooKeeper)
		{

			string curPath = path + "/" + keyName;

			if (zooKeeper.exists(curPath, false) == null)
			{
				return null;
			}

			ZkDisconfData zkDisconfData = new ZkDisconfData();
			zkDisconfData.Key = keyName;

			IList<string> secChiList = zooKeeper.getChildren(curPath, false);
			IList<ZkDisconfData.ZkDisconfDataItem> zkDisconfDataItems = new List<ZkDisconfData.ZkDisconfDataItem>();

			// list
			foreach (string secKey in secChiList)
			{

				// machine
				ZkDisconfData.ZkDisconfDataItem zkDisconfDataItem = new ZkDisconfData.ZkDisconfDataItem();
				zkDisconfDataItem.Machine = secKey;

				string thirdPath = curPath + "/" + secKey;

				// value
				sbyte[] data = zooKeeper.getData(thirdPath, null, null);
				if (data != null)
				{
					zkDisconfDataItem.Value = StringHelperClass.NewString(data, CHARSET);
				}

				// add
				zkDisconfDataItems.Add(zkDisconfDataItem);
			}

			zkDisconfData.Data = zkDisconfDataItems;

			return zkDisconfData;
		}
Beispiel #10
0
        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();
                }
            }
        }
Beispiel #11
0
        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();
                }
            }
        }
Beispiel #12
0
        public void testGetDataSync()
        {
            try
            {
                lsnr.getData("/foo", true, null);
                Assert.fail();
            }
            catch (KeeperException e)
            {
                Assert.assertEquals(KeeperException.Code.NONODE, e.getCode());
                Assert.assertEquals("/foo", e.getPath());
            }
            try
            {
                lsnr.getData("/foo/bar", true, null);
                Assert.fail();
            }
            catch (KeeperException e)
            {
                Assert.assertEquals(KeeperException.Code.NONODE, e.getCode());
                Assert.assertEquals("/foo/bar", e.getPath());
            }

            client.create("/foo", "parent".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            Assert.assertNotNull(lsnr.getData("/foo", true, null));
            client.create("/foo/bar", "child".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            Assert.assertNotNull(lsnr.getData("/foo/bar", true, null));

            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.getData("/foo", true, null));
            Assert.assertNotNull(lsnr.getData("/foo/bar", true, null));

            client.delete("/foo/bar", -1);
            expected.Add(Watcher.Event.EventType.NodeDeleted);
            client.delete("/foo", -1);
            expected.Add(Watcher.Event.EventType.NodeDeleted);

            verify();
        }
Beispiel #13
0
        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();
                }
            }
        }