use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class WatchedEventTest method testCreatingWatchedEvent.
@Test
public void testCreatingWatchedEvent() {
// EventWatch is a simple, immutable type, so all we need to do
// is make sure we can create all possible combinations of values.
EnumSet<EventType> allTypes = EnumSet.allOf(EventType.class);
EnumSet<KeeperState> allStates = EnumSet.allOf(KeeperState.class);
WatchedEvent we;
for (EventType et : allTypes) {
for (KeeperState ks : allStates) {
we = new WatchedEvent(et, ks, "blah");
assertEquals(et, we.getType());
assertEquals(ks, we.getState());
assertEquals("blah", we.getPath());
}
}
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class WatcherTest method testWatcherDisconnectOnClose.
@Test
public void testWatcherDisconnectOnClose() throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = null;
try {
final BlockingQueue<WatchedEvent> queue = new LinkedBlockingQueue<>();
MyWatcher connWatcher = new MyWatcher();
Watcher watcher = event -> {
try {
queue.put(event);
} catch (InterruptedException e) {
// Oh well, never mind
}
};
zk = createClient(connWatcher, hostPort);
StatCallback scb = new StatCallback() {
public void processResult(int rc, String path, Object ctx, Stat stat) {
// don't do anything
}
};
// Register a watch on the node
zk.exists("/missing", watcher, scb, null);
// Close the client without changing the node
zk.close();
WatchedEvent event = queue.poll(10, TimeUnit.SECONDS);
assertNotNull(event, "No watch event was received after closing the Zookeeper client. A 'Closed' event should have occurred");
assertEquals(Event.EventType.None, event.getType(), "Closed events are not generated by the server, and so should have a type of 'None'");
assertEquals(Event.KeeperState.Closed, event.getState(), "A 'Closed' event was expected as the Zookeeper client was closed without altering the node it was watching");
} 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);
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.WatchedEvent in project zookeeper by apache.
the class ZooKeeperTestClient method getEvent.
private WatchedEvent getEvent(int numTries) throws InterruptedException {
WatchedEvent event = null;
for (int i = 0; i < numTries; i++) {
System.out.println("i = " + i);
event = events.poll(10, TimeUnit.SECONDS);
if (event != null) {
break;
}
Thread.sleep(5000);
}
return event;
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ZooKeeperTestClient method enode_test_2.
private void enode_test_2() throws IOException, InterruptedException, KeeperException {
checkRoot();
String parentName = testDirOnZK;
String nodeName = parentName + "/enode_abc";
ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
ZooKeeper zk_1 = new ZooKeeper(hostPort, 10000, this);
Stat stat_parent = zk_1.exists(parentName, false);
if (stat_parent == null) {
try {
zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException ke) {
fail("Creating node " + parentName + ke.getMessage());
}
}
Stat stat_node = zk_1.exists(nodeName, false);
if (stat_node != null) {
try {
zk.delete(nodeName, -1);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY;
if (!valid) {
fail("Unexpected exception code for delete: " + ke.getMessage());
}
}
}
List<String> firstGen1 = zk_1.getChildren(parentName, true);
Stat stat = new Stat();
List<String> firstGen2 = zk_1.getChildren(parentName, true, stat);
if (!firstGen1.equals(firstGen2)) {
fail("children lists from getChildren()/getChildren2() do not match");
}
if (!stat_parent.equals(stat)) {
fail("stat from exists()/getChildren() do not match");
}
try {
zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NODEEXISTS;
if (!valid) {
fail("Unexpected exception code for createin: " + ke.getMessage());
}
}
Thread.sleep(5000);
WatchedEvent event = events.poll(10, TimeUnit.SECONDS);
if (event == null) {
throw new IOException("No event was delivered promptly");
}
if (event.getType() != EventType.NodeChildrenChanged || !event.getPath().equalsIgnoreCase(parentName)) {
fail("Unexpected event was delivered: " + event.toString());
}
stat_node = zk_1.exists(nodeName, false);
if (stat_node == null) {
fail("node " + nodeName + " should exist");
}
try {
zk.delete(parentName, -1);
fail("Should be impossible to delete a non-empty node " + parentName);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NOTEMPTY;
if (!valid) {
fail("Unexpected exception code for delete: " + code);
}
}
try {
zk.create(nodeName + "/def", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
fail("Should be impossible to create child off Ephemeral node " + nodeName);
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NOCHILDRENFOREPHEMERALS;
if (!valid) {
fail("Unexpected exception code for createin: " + code);
}
}
try {
List<String> children1 = zk.getChildren(nodeName, false);
List<String> children2 = zk.getChildren(nodeName, false, null);
if (!children1.equals(children2)) {
fail("children lists from getChildren()/getChildren2() does not match");
}
if (children1.size() > 0) {
fail("ephemeral node " + nodeName + " should not have children");
}
} catch (KeeperException ke) {
Code code = ke.code();
boolean valid = code == KeeperException.Code.NONODE;
if (!valid) {
fail("Unexpected exception code for createin: " + code);
}
}
firstGen1 = zk_1.getChildren(parentName, true);
firstGen2 = zk_1.getChildren(parentName, true, null);
if (!firstGen1.equals(firstGen2)) {
fail("children list from getChildren()/getChildren2() does not match");
}
stat_node = zk_1.exists(nodeName, true);
if (stat_node == null) {
fail("node " + nodeName + " should exist");
}
System.out.println("session id of zk: " + zk.getSessionId());
System.out.println("session id of zk_1: " + zk_1.getSessionId());
zk.close();
zk_1.exists("nosuchnode", false);
event = this.getEvent(10);
if (event == null) {
throw new Error("First event was not delivered promptly");
}
if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (event.getType() == EventType.NodeDeleted && event.getPath().equalsIgnoreCase(nodeName)))) {
System.out.print(parentName + " " + EventType.NodeChildrenChanged + " " + nodeName + " " + EventType.NodeDeleted);
fail("Unexpected first event was delivered: " + event.toString());
}
event = this.getEvent(10);
if (event == null) {
throw new Error("Second event was not delivered promptly");
}
if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (event.getType() == EventType.NodeDeleted && event.getPath().equalsIgnoreCase(nodeName)))) {
System.out.print(parentName + " " + EventType.NodeChildrenChanged + " " + nodeName + " " + EventType.NodeDeleted);
fail("Unexpected second event was delivered: " + event.toString());
}
firstGen1 = zk_1.getChildren(parentName, false);
stat_node = zk_1.exists(nodeName, false);
if (stat_node != null) {
fail("node " + nodeName + " should have been deleted");
}
if (firstGen1.contains(nodeName)) {
fail("node " + nodeName + " should not be a children");
}
deleteZKDir(zk_1, nodeName);
zk_1.close();
}
Aggregations