Example #1
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));
        }
Example #2
0
        public async Task TestMethod1()
        {
            //原来zk客户端不应该作为静态对象,每次new

            foreach (var i in Com.Range(100))
            {
                var client = new ZooKeeper("localhost:32771",
                                           (int)TimeSpan.FromSeconds(5).TotalMilliseconds, this,
                                           canBeReadOnly: false);
                try
                {
                    //var count = 0;
                    //while (client.getState() != ZooKeeper.States.CONNECTED)
                    //{
                    //    if (++count > 100) { throw new Exception("loss patient to wait for connection"); }
                    //    await Task.Delay(10);
                    //}

                    if (await client.existsAsync("/home", false) == null)
                    {
                        var path = await client.createAsync("/home", "".GetBytes(),
                                                            Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    }

                    var bs = new { id = 2, name = "fas", age = 44, time = DateTime.Now }.ToJson().GetBytes();

                    await client.setDataAsync("/home", bs);

                    var data = await client.getDataAsync("/home", false);

                    var children = await client.getChildrenAsync("/home");

                    var t = client.transaction();

                    //t.delete("/home");
                    t.setData("/home", $"{DateTime.Now.Ticks}".GetBytes());

                    var res = await t.commitAsync();
                }
                catch (Exception e)
                {
                    //
                }
                finally
                {
                    await client.closeAsync();

                    this.connected = false;
                }
            }
        }