Created
March 2, 2012 21:55
-
-
Save akiani/1961660 to your computer and use it in GitHub Desktop.
Testing if calling ZooKeeper's getData() on the path for updated node returns the updated _data_.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment