use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class DataTreeTest method testRootWatchTriggered.
@Test(timeout = 60000)
public void testRootWatchTriggered() throws Exception {
class MyWatcher implements Watcher {
boolean fired = false;
public void process(WatchedEvent event) {
if (event.getPath().equals("/"))
fired = true;
}
}
MyWatcher watcher = new MyWatcher();
// set a watch on the root node
dt.getChildren("/", new Stat(), watcher);
// add a new node, should trigger a watch
dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion() + 1, 1, 1);
Assert.assertFalse("Root node watch not triggered", !watcher.fired);
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class DisconnectedWatcherTest method testDefaultWatcherAutoResetWithChroot.
@Test
public void testDefaultWatcherAutoResetWithChroot() throws Exception {
ZooKeeper zk1 = createClient();
zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
MyWatcher watcher = new MyWatcher();
ZooKeeper zk2 = createClient(watcher, hostPort + "/ch1");
zk2.getChildren("/", true);
// this call shouldn't trigger any error or watch
zk1.create("/youdontmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// this should trigger the watch
zk1.create("/ch1/youshouldmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
WatchedEvent e = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNotNull(e);
Assert.assertEquals(EventType.NodeChildrenChanged, e.getType());
Assert.assertEquals("/", e.getPath());
zk2.getChildren("/", true);
stopServer();
watcher.waitForDisconnected(3000);
startServer();
watcher.waitForConnected(3000);
// this should trigger the watch
zk1.create("/ch1/youshouldmatter2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
e = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNotNull(e);
Assert.assertEquals(EventType.NodeChildrenChanged, e.getType());
Assert.assertEquals("/", e.getPath());
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ClientTest method testMutipleWatcherObjs.
/**
* Register multiple watchers and verify that they all get notified and
* in the right order.
*/
@Test
public void testMutipleWatcherObjs() throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = createClient(new CountdownWatcher(), hostPort);
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(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
Stat stat = new Stat();
//
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, TimeUnit.SECONDS);
Assert.assertEquals("/foo-" + i, event.getPath());
Assert.assertEquals(EventType.NodeDataChanged, event.getType());
Assert.assertEquals(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());
}
//
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, TimeUnit.SECONDS);
Assert.assertEquals("/foo-" + i, event.getPath());
Assert.assertEquals(EventType.NodeDataChanged, event.getType());
Assert.assertEquals(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());
}
//
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, TimeUnit.SECONDS);
Assert.assertEquals("/foo-" + i, event.getPath());
Assert.assertEquals(EventType.NodeDataChanged, event.getType());
Assert.assertEquals(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, TimeUnit.SECONDS);
Assert.assertEquals("/foo-" + i, event2.getPath());
Assert.assertEquals(EventType.NodeDataChanged, event2.getType());
Assert.assertEquals(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();
}
}
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ClientTest method performClientTest.
private void performClientTest(boolean withWatcherObj) throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = null;
try {
MyWatcher watcher = new MyWatcher();
zk = createClient(watcher, hostPort);
LOG.info("Before create /benwashere");
zk.create("/benwashere", "".getBytes(), 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 e) {
// expected that
} catch (KeeperException e) {
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, hostPort);
//LOG.info("Created a new client: " + zk.describeCNXN());
LOG.info("Before delete /");
try {
zk.delete("/", -1);
Assert.fail("deleted root!");
} catch (KeeperException.BadArgumentsException e) {
// good, expected that
}
Stat stat = new Stat();
// Test basic create, ls, and getData
zk.create("/pat", "Pat was here".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
LOG.info("Before create /ben");
zk.create("/pat/ben", "Ben was here".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
LOG.info("Before getChildren /pat");
List<String> children = zk.getChildren("/pat", false);
Assert.assertEquals(1, children.size());
Assert.assertEquals("ben", children.get(0));
List<String> children2 = zk.getChildren("/pat", false, null);
Assert.assertEquals(children, children2);
String value = new String(zk.getData("/pat/ben", false, stat));
Assert.assertEquals("Ben was here", value);
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 e) {
// OK, expected that
}
zk.create("/frog", "hi".getBytes(), 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, TimeUnit.SECONDS);
Assert.assertEquals("/frog", event.getPath());
Assert.assertEquals(EventType.NodeCreated, event.getType());
Assert.assertEquals(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 + "-", Integer.toString(i).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
}
children = zk.getChildren("/pat/ben", false);
Collections.sort(children);
Assert.assertEquals(10, children.size());
for (int i = 0; i < 10; i++) {
final String name = children.get(i);
Assert.assertTrue("starts with -", name.startsWith(i + "-"));
byte[] b;
if (withWatcherObj) {
b = zk.getData("/pat/ben/" + name, watcher, stat);
} else {
b = zk.getData("/pat/ben/" + name, true, stat);
}
Assert.assertEquals(Integer.toString(i), new String(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, TimeUnit.SECONDS);
Assert.assertEquals("/pat/ben", event.getPath());
Assert.assertEquals(EventType.NodeChildrenChanged, event.getType());
Assert.assertEquals(KeeperState.SyncConnected, event.getState());
for (int i = 0; i < 10; i++) {
event = watcher.events.poll(10, TimeUnit.SECONDS);
final String name = children.get(i);
Assert.assertEquals("/pat/ben/" + name, event.getPath());
Assert.assertEquals(EventType.NodeDataChanged, event.getType());
Assert.assertEquals(KeeperState.SyncConnected, event.getState());
event = watcher.events.poll(10, TimeUnit.SECONDS);
Assert.assertEquals("/pat/ben/" + name, event.getPath());
Assert.assertEquals(EventType.NodeDeleted, event.getType());
Assert.assertEquals(KeeperState.SyncConnected, event.getState());
}
zk.create("/good@path", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/duplicate", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
try {
zk.create("/duplicate", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Assert.fail("duplicate create allowed");
} catch (KeeperException.NodeExistsException e) {
// OK, expected that
}
} finally {
if (zk != null) {
zk.close();
}
}
}
use of org.apache.zookeeper.WatchedEvent 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();
}
}
}
Aggregations