use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ClientRetryTest method testClientRetry.
/*
* This is a simple test - try to connect two clients to a server
* accepting a maximum of one connection from each address. Check that
* only one is accepted. Close that connection, and check that the other
* eventually connects.
*
* There is a possibility of a false positive here, as when zk2 is tested
* for having connected it might not have been given enough time, and finish
* connecting after the test is done. Since the
* server doesn't tell the client why it hasn't connected, there's no
* obvious way to detect the difference.
*/
@Test
public void testClientRetry() throws IOException, InterruptedException, TimeoutException {
CountdownWatcher cdw1 = new CountdownWatcher();
CountdownWatcher cdw2 = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
try {
cdw1.waitForConnected(CONNECTION_TIMEOUT);
ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
try {
States s1 = zk.getState();
States s2 = zk2.getState();
Assert.assertSame(s1, States.CONNECTED);
Assert.assertSame(s2, States.CONNECTING);
cdw1.reset();
zk.close();
cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
cdw2.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertSame(zk2.getState(), States.CONNECTED);
} finally {
zk2.close();
}
} finally {
zk.close();
}
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ClientTest method testNullAuthId.
@Test
public void testNullAuthId() throws Exception {
ZooKeeper zk = null;
try {
zk = createClient();
zk.addAuthInfo("digest", "ben:passwd".getBytes());
ArrayList<ACL> testACL = new ArrayList<ACL>();
testACL.add(new ACL(Perms.ALL, new Id("auth", null)));
zk.create("/acltest", new byte[0], testACL, CreateMode.PERSISTENT);
zk.close();
zk = createClient();
zk.addAuthInfo("digest", "ben:passwd2".getBytes());
if (skipACL) {
try {
zk.getData("/acltest", false, null);
} catch (KeeperException e) {
Assert.fail("Badauth reads should succeed with skipACL.");
}
} else {
try {
zk.getData("/acltest", false, null);
Assert.fail("Should have received a permission error");
} catch (KeeperException e) {
Assert.assertEquals(Code.NOAUTH, e.code());
}
}
zk.addAuthInfo("digest", "ben:passwd".getBytes());
zk.getData("/acltest", false, null);
zk.setACL("/acltest", Ids.OPEN_ACL_UNSAFE, -1);
zk.close();
zk = createClient();
zk.getData("/acltest", false, null);
List<ACL> acls = zk.getACL("/acltest", new Stat());
Assert.assertEquals(1, acls.size());
Assert.assertEquals(Ids.OPEN_ACL_UNSAFE, acls);
} finally {
if (zk != null) {
zk.close();
}
}
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ClientTest method testSequentialNodeNames.
// Test that sequential filenames are being created correctly,
// with 0-padding in the filename
@Test
public void testSequentialNodeNames() throws IOException, InterruptedException, KeeperException {
String path = "/SEQUENCE";
String file = "TEST";
String filepath = path + "/" + file;
ZooKeeper zk = null;
try {
zk = createClient();
zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
List<String> children = zk.getChildren(path, false);
Assert.assertEquals(1, children.size());
Assert.assertEquals(file + "0000000000", children.get(0));
zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
children = zk.getChildren(path, false);
Assert.assertEquals(2, children.size());
Assert.assertTrue("contains child 1", children.contains(file + "0000000001"));
zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
children = zk.getChildren(path, false);
Assert.assertEquals(3, children.size());
Assert.assertTrue("contains child 2", children.contains(file + "0000000002"));
// to be sure it continues to spit out the correct answer
for (int i = children.size(); i < 105; i++) zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
children = zk.getChildren(path, false);
Assert.assertTrue("contains child 104", children.contains(file + "0000000104"));
} finally {
if (zk != null)
zk.close();
}
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class OOMTest method utestExists.
private void utestExists(int port) throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, this);
for (int i = 0; i < 10000; i++) {
zk.exists("/this/path/doesnt_exist!", true);
}
zk.close();
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class OOMTest method utestGet.
private void utestGet(int port) throws IOException, InterruptedException, KeeperException {
ZooKeeper zk = new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, this);
for (int i = 0; i < 10000; i++) {
Stat stat = new Stat();
zk.getData("/" + i, true, stat);
}
zk.close();
}
Aggregations