public
Last active

Testing if calling ZooKeeper's getData() on the path for updated node returns the updated _data_.

  • Download Gist
TestZooKeeperGetData.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
package zookeeper.test.getdata;
 
 
 
import static org.junit.Assert.assertTrue;
 
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.netflix.curator.test.TestingServer;
 
/**
* Tests if calling getData() on a node that has been changed always returns the updated value.
* {@link ZooKeeperer} is used to keep track of the value set on the node after watch is fired.
*
* @author Amirhossein Kiani (amirhkiani@gmail.com) Created on Mar 2, 2012
*/
public class TestZooKeeperGetData {
static final Logger logger = LoggerFactory.getLogger(TestZooKeeperGetData.class);
private static final int NUMBER_OF_TESTS = 10000;
@Test
public void testZoo() throws Exception {
TestingServer testingServer = new TestingServer(12322);
ZooKeeperer zooKeeperer = new ZooKeeperer();
ZooKeeper zoo = new ZooKeeper(testingServer.getConnectString(), 1000, zooKeeperer);
zooKeeperer.setZk(zoo, NUMBER_OF_TESTS);
Stat stat = new Stat();
zoo.create("/test", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
 
for (int a = 0; a < NUMBER_OF_TESTS; a++) {
zoo.getData("/test", zooKeeperer, stat);
String string = String.valueOf(a);
zoo.setData("/test", string.getBytes(), stat.getVersion());
}
 
while(!zooKeeperer.doneWithWatches){
Thread.sleep(100);
}
for (int a = 0; a < NUMBER_OF_TESTS; a++) {
assertTrue("Data " + a + " was never receieved.", zooKeeperer.values[a]);
}
}
}
ZooKeeperer.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 
package zookeeper.test.getdata;
 
 
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
 
/**
* Keeps track of the values returned after {@link TestZooKeeperGetData} sets data on the "/test"
* node, by marking each value it receives in a boolean array.
*
* @author Amirhossein Kiani (amirhkiani@gmail.com) Created on Mar 2, 2012
*/
public class ZooKeeperer implements Watcher {
final static Logger logger = LoggerFactory.getLogger(ZooKeeperer.class);
private ZooKeeper zk;
boolean[] values;
boolean doneWithWatches = false;
int numberOfEvents = 0;
public ZooKeeperer() {}
 
public void setZk(ZooKeeper zk, int numberOfTests) {
this.zk = zk;
this.values = new boolean[numberOfTests];
}
 
public void process(WatchedEvent event) {
try {
if (event.getType().equals(EventType.NodeDataChanged)) {
numberOfEvents ++;
byte[] data = zk.getData(event.getPath(), false, new Stat());
String dataString = new String(data);
 
//convert string to integer
int number = Integer.valueOf(dataString);
//mark this value as received
values[number] = true;
if(numberOfEvents == values.length - 1){
doneWithWatches = true;
}
}
} catch (Exception e) {
logger.error("Exception in processing the watch.", e);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.