Search in sources :

Example 36 with ZooKeeper

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();
    }
}
Also used : States(org.apache.zookeeper.ZooKeeper.States) ZooKeeper(org.apache.zookeeper.ZooKeeper) Test(org.junit.Test)

Example 37 with ZooKeeper

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();
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Stat(org.apache.zookeeper.data.Stat) ArrayList(java.util.ArrayList) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 38 with ZooKeeper

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();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Test(org.junit.Test)

Example 39 with ZooKeeper

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper)

Example 40 with ZooKeeper

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();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)330 Test (org.junit.Test)189 KeeperException (org.apache.zookeeper.KeeperException)90 Stat (org.apache.zookeeper.data.Stat)44 ArrayList (java.util.ArrayList)35 WatchedEvent (org.apache.zookeeper.WatchedEvent)35 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)34 IOException (java.io.IOException)32 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)32 CountDownLatch (java.util.concurrent.CountDownLatch)31 Watcher (org.apache.zookeeper.Watcher)24 File (java.io.File)22 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)21 HashMap (java.util.HashMap)17 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)17 ReconfigTest (org.apache.zookeeper.test.ReconfigTest)16 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)15 ACL (org.apache.zookeeper.data.ACL)10 ClientTest (org.apache.zookeeper.test.ClientTest)10 TimeoutException (java.util.concurrent.TimeoutException)9