Search in sources :

Example 1 with VoidCallback

use of org.apache.zookeeper.AsyncCallback.VoidCallback in project zookeeper by apache.

the class QuorumRequestPipelineTest method testSync.

@Test
public void testSync() throws Exception {
    complete = false;
    create2EmptyNode(zkClient, PARENT_PATH);
    VoidCallback onSync = new VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            complete = true;
            callComplete.countDown();
        }
    };
    zkClient.sync(PARENT_PATH, onSync, null);
    callComplete.await(30, TimeUnit.SECONDS);
    Assert.assertTrue(String.format("%s Sync completed", serverState), complete);
}
Also used : VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) Test(org.junit.Test)

Example 2 with VoidCallback

use of org.apache.zookeeper.AsyncCallback.VoidCallback in project zookeeper by apache.

the class WatcherTest method testWatcherCorrectness.

/**
     * Verify that we get all of the events we expect to get. This particular
     * case verifies that we see all of the data events on a particular node.
     * There was a bug (ZOOKEEPER-137) that resulted in events being dropped
     * in some cases (timing).
     *
     * @throws IOException
     * @throws InterruptedException
     * @throws KeeperException
     */
@Test
public void testWatcherCorrectness() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = null;
    try {
        MyWatcher watcher = new MyWatcher();
        zk = createClient(watcher, hostPort);
        StatCallback scb = new StatCallback() {

            public void processResult(int rc, String path, Object ctx, Stat stat) {
            // don't do anything
            }
        };
        VoidCallback vcb = new VoidCallback() {

            public void processResult(int rc, String path, Object ctx) {
            // don't do anything
            }
        };
        String[] names = new String[10];
        for (int i = 0; i < names.length; i++) {
            String name = zk.create("/tc-", "initialvalue".getBytes(), 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(), scb, null);
            stat = zk.exists(name, watcher);
            zk.delete(name, stat.getVersion(), vcb, null);
        }
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            WatchedEvent event = watcher.events.poll(10, TimeUnit.SECONDS);
            Assert.assertEquals(name, event.getPath());
            Assert.assertEquals(Event.EventType.NodeDataChanged, event.getType());
            Assert.assertEquals(Event.KeeperState.SyncConnected, event.getState());
            event = watcher.events.poll(10, TimeUnit.SECONDS);
            Assert.assertEquals(name, event.getPath());
            Assert.assertEquals(Event.EventType.NodeDeleted, event.getType());
            Assert.assertEquals(Event.KeeperState.SyncConnected, event.getState());
        }
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Stat(org.apache.zookeeper.data.Stat) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Test(org.junit.Test)

Example 3 with VoidCallback

use of org.apache.zookeeper.AsyncCallback.VoidCallback in project zookeeper by apache.

the class ZooKeeperTest method testDeleteRecursiveAsync.

@Test
public void testDeleteRecursiveAsync() throws IOException, InterruptedException, KeeperException {
    final ZooKeeper zk = createClient();
    // making sure setdata works on /
    zk.setData("/", "some".getBytes(), -1);
    zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/b/v/1", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/c", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/c/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    for (int i = 0; i < 50; ++i) {
        zk.create("/a/c/" + i, "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    List<String> children = zk.getChildren("/a", false);
    Assert.assertEquals("2 children - b & c should be present ", children.size(), 2);
    Assert.assertTrue(children.contains("b"));
    Assert.assertTrue(children.contains("c"));
    VoidCallback cb = new VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            synchronized (ctx) {
                ((AtomicInteger) ctx).set(4);
                ctx.notify();
            }
        }
    };
    final AtomicInteger ctx = new AtomicInteger(3);
    ZKUtil.deleteRecursive(zk, "/a", cb, ctx);
    synchronized (ctx) {
        ctx.wait();
    }
    Assert.assertEquals(4, ((AtomicInteger) ctx).get());
}
Also used : VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)3 Test (org.junit.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)1 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 Stat (org.apache.zookeeper.data.Stat)1