use of org.apache.zookeeper.AsyncCallback.StatCallback 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);
assertEquals(name, event.getPath());
assertEquals(Event.EventType.NodeDataChanged, event.getType());
assertEquals(Event.KeeperState.SyncConnected, event.getState());
event = watcher.events.poll(10, TimeUnit.SECONDS);
assertEquals(name, event.getPath());
assertEquals(Event.EventType.NodeDeleted, event.getType());
assertEquals(Event.KeeperState.SyncConnected, event.getState());
}
} finally {
if (zk != null) {
zk.close();
}
}
}
use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.
the class TestZooKeeperClient method testACLSetAndGet.
@Test
public void testACLSetAndGet() throws Exception {
final int timeout = 2000;
ZooKeeperClient client = ZooKeeperClient.createConnectedZooKeeperClient(zkUtil.getZooKeeperConnectString(), timeout, new HashSet<Watcher>(), new BoundExponentialBackoffRetryPolicy(timeout, timeout, Integer.MAX_VALUE));
Assert.assertTrue("Client failed to connect an alive ZooKeeper.", client.getState().isConnected());
String path = "/testACLSetAndGet";
byte[] data = "test".getBytes();
// create a node and call getACL to verify the received ACL
logger.info("Create znode " + path);
List<ACL> setACLList = new ArrayList<ACL>();
setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
client.create(path, data, setACLList, CreateMode.PERSISTENT);
Stat status = new Stat();
List<ACL> receivedACLList = client.getACL(path, status);
Assert.assertEquals("Test1 - ACLs are expected to match", setACLList, receivedACLList);
// update node's ACL and call getACL to verify the received ACL
setACLList.clear();
setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
setACLList.addAll(Ids.READ_ACL_UNSAFE);
status = client.setACL(path, setACLList, status.getAversion());
receivedACLList = client.getACL(path, status);
Assert.assertEquals("Test2 - ACLs are expected to match", setACLList, receivedACLList);
// update node's ACL by calling async setACL and call async getACL to verify the received ACL
setACLList.clear();
setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
CountDownLatch latch = new CountDownLatch(1);
final Stat[] statArray = { null };
final int[] rcArray = { -1 };
client.setACL(path, setACLList, status.getAversion(), new StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
CountDownLatch cdLatch = (CountDownLatch) ctx;
rcArray[0] = rc;
statArray[0] = stat;
cdLatch.countDown();
}
}, latch);
latch.await(3000, TimeUnit.MILLISECONDS);
if (rcArray[0] != KeeperException.Code.OK.intValue()) {
Assert.fail("Test3 - SetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
}
status = statArray[0];
latch = new CountDownLatch(1);
rcArray[0] = 0;
statArray[0] = null;
final List<List<ACL>> aclListArray = new ArrayList<List<ACL>>(1);
client.getACL(path, status, new ACLCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<ACL> acl, Stat stat) {
CountDownLatch cdLatch = (CountDownLatch) ctx;
rcArray[0] = rc;
statArray[0] = stat;
aclListArray.add(acl);
cdLatch.countDown();
}
}, latch);
latch.await(3000, TimeUnit.MILLISECONDS);
if (rcArray[0] != KeeperException.Code.OK.intValue()) {
Assert.fail("Test4 - GetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
}
status = statArray[0];
receivedACLList = aclListArray.get(0);
Assert.assertEquals("Test5 - ACLs are expected to match", setACLList, receivedACLList);
// delete the node
client.delete(path, status.getVersion());
}
use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.
the class TestZooKeeperClient method testZnodeExists.
@Test
public void testZnodeExists() throws Exception {
final int timeout = 2000;
ZooKeeperClient client = ZooKeeperClient.createConnectedZooKeeperClient(zkUtil.getZooKeeperConnectString(), timeout, new HashSet<Watcher>(), new BoundExponentialBackoffRetryPolicy(timeout, timeout, Integer.MAX_VALUE));
Assert.assertTrue("Client failed to connect an alive ZooKeeper.", client.getState().isConnected());
String path = "/testZnodeExists";
byte[] data = "test".getBytes();
// create a node
logger.info("Create znode " + path);
client.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// expire the ZKClient session and then call exists method for the path
expireZooKeeperSession(client, timeout);
final AtomicBoolean isDeleted = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(1);
Stat stat = client.exists(path, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
isDeleted.set(true);
latch.countDown();
}
}
});
Assert.assertNotNull("node with path " + path + " should exists", stat);
// now delete the znode and verify if the Watcher is called
client.delete(path, stat.getVersion());
latch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertTrue("The watcher on the node should have been called", isDeleted.get());
// calling async exists method and verifying the return values
CountDownLatch latch2 = new CountDownLatch(1);
final int[] rcArray = { -1 };
final boolean[] statIsnull = { false };
client.exists(path, null, new StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
CountDownLatch cdlatch = (CountDownLatch) ctx;
rcArray[0] = rc;
statIsnull[0] = (stat == null);
cdlatch.countDown();
}
}, latch2);
latch2.await(3000, TimeUnit.MILLISECONDS);
if (rcArray[0] != KeeperException.Code.NONODE.intValue()) {
Assert.fail("exists call is supposed to return NONODE rcvalue, but it returned - " + KeeperException.Code.get(rcArray[0]));
}
Assert.assertTrue("exists is supposed to return null for Stat," + " since the node is already deleted", statIsnull[0]);
}
use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.
the class TestZooKeeperClient method testACLSetAndGetAfterSessionExpiry.
@Test
public void testACLSetAndGetAfterSessionExpiry() throws Exception {
final int timeout = 2000;
ZooKeeperClient client = ZooKeeperClient.createConnectedZooKeeperClient(zkUtil.getZooKeeperConnectString(), timeout, new HashSet<Watcher>(), new BoundExponentialBackoffRetryPolicy(timeout, timeout, Integer.MAX_VALUE));
Assert.assertTrue("Client failed to connect an alive ZooKeeper.", client.getState().isConnected());
String path = "/testACLSetAndGetAfterSessionExpiry";
byte[] data = "test".getBytes();
// create a node
logger.info("Create znode " + path);
List<ACL> setACLList = new ArrayList<ACL>();
setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
client.create(path, data, setACLList, CreateMode.PERSISTENT);
// expire the ZKClient session
expireZooKeeperSession(client, timeout);
// call getACL and verify if it returns the previously set ACL
Stat status = new Stat();
List<ACL> receivedACLList = client.getACL(path, status);
Assert.assertEquals("Test1 - ACLs are expected to match", setACLList, receivedACLList);
// update ACL of that node
setACLList.clear();
setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
setACLList.addAll(Ids.READ_ACL_UNSAFE);
status = client.setACL(path, setACLList, status.getAversion());
// expire the ZKClient session
expireZooKeeperSession(client, timeout);
// call getACL and verify if it returns the previously set ACL
receivedACLList = client.getACL(path, status);
Assert.assertEquals("Test2 - ACLs are expected to match", setACLList, receivedACLList);
// update the ACL of node by calling async setACL
setACLList.clear();
setACLList.addAll(Ids.OPEN_ACL_UNSAFE);
CountDownLatch latch = new CountDownLatch(1);
final Stat[] statArray = { null };
final int[] rcArray = { -1 };
client.setACL(path, setACLList, status.getAversion(), new StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
CountDownLatch cdLatch = (CountDownLatch) ctx;
rcArray[0] = rc;
statArray[0] = stat;
cdLatch.countDown();
}
}, latch);
latch.await(3000, TimeUnit.MILLISECONDS);
if (rcArray[0] != KeeperException.Code.OK.intValue()) {
Assert.fail("Test3 - SetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
}
status = statArray[0];
// expire the ZKClient session
expireZooKeeperSession(client, timeout);
// call async getACL and verify if it returns the previously set ACL
latch = new CountDownLatch(1);
rcArray[0] = 0;
statArray[0] = null;
final List<List<ACL>> aclListArray = new ArrayList<List<ACL>>(1);
client.getACL(path, status, new ACLCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<ACL> acl, Stat stat) {
CountDownLatch cdLatch = (CountDownLatch) ctx;
rcArray[0] = rc;
statArray[0] = stat;
aclListArray.add(acl);
cdLatch.countDown();
}
}, latch);
Assert.assertTrue("getACL operation should have completed successfully", latch.await(6000, TimeUnit.MILLISECONDS));
if (rcArray[0] != KeeperException.Code.OK.intValue()) {
Assert.fail("Test4 - GetACL call failed because of exception - " + KeeperException.Code.get(rcArray[0]));
}
status = statArray[0];
receivedACLList = aclListArray.get(0);
Assert.assertEquals("Test5 - ACLs are expected to match", setACLList, receivedACLList);
client.delete(path, status.getVersion());
}
use of org.apache.zookeeper.AsyncCallback.StatCallback in project bookkeeper by apache.
the class MockZooKeeperTestCase method mockZkSetData.
protected void mockZkSetData(String expectedLedgerPath, byte[] expectedBytes, int expectedVersion, int retCode, Stat retStat) throws Exception {
doAnswer(invocationOnMock -> {
String path = invocationOnMock.getArgument(0);
StatCallback callback = invocationOnMock.getArgument(3);
Object ctx = invocationOnMock.getArgument(4);
callback.processResult(retCode, path, ctx, retStat);
return null;
}).when(mockZk).setData(eq(expectedLedgerPath), eq(expectedBytes), eq(expectedVersion), any(StatCallback.class), any());
}
Aggregations