Search in sources :

Example 11 with TestableZooKeeper

use of org.apache.zookeeper.TestableZooKeeper in project zookeeper by apache.

the class ClientTest method testTestability.

/** Exercise the testable functions, verify tostring, etc... */
@Test
public void testTestability() throws Exception {
    TestableZooKeeper zk = createClient();
    try {
        LOG.info("{}", zk.testableLocalSocketAddress());
        LOG.info("{}", zk.testableRemoteSocketAddress());
        LOG.info("{}", zk.toString());
    } finally {
        zk.close();
        zk.testableWaitForShutdown(CONNECTION_TIMEOUT);
        LOG.info("{}", zk.testableLocalSocketAddress());
        LOG.info("{}", zk.testableRemoteSocketAddress());
        LOG.info("{}", zk.toString());
    }
}
Also used : TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Test(org.junit.Test)

Example 12 with TestableZooKeeper

use of org.apache.zookeeper.TestableZooKeeper in project zookeeper by apache.

the class SaslAuthFailDesignatedClientTest method testAuth.

@Test
public void testAuth() throws Exception {
    // Cannot use createClient here because server may close session before 
    // JMXEnv.ensureAll is called which will fail the test case
    CountdownWatcher watcher = new CountdownWatcher();
    TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher);
    if (!watcher.clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
        Assert.fail("Unable to connect to server");
    }
    try {
        zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
        Assert.fail("Should have gotten exception.");
    } catch (KeeperException e) {
        // ok, exception as expected.
        LOG.info("Got exception as expected: " + e);
    } finally {
        zk.close();
    }
}
Also used : TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 13 with TestableZooKeeper

use of org.apache.zookeeper.TestableZooKeeper in project zookeeper by apache.

the class WatcherTest method testWatchAutoResetWithPending.

/**
     * This test checks that watches for pending requests do not get triggered,
     * but watches set by previous requests do.
     *
     * @throws Exception
     */
@Test
public void testWatchAutoResetWithPending() throws Exception {
    MyWatcher[] watches = new MyWatcher[COUNT];
    MyStatCallback[] cbs = new MyStatCallback[COUNT];
    MyWatcher watcher = new MyWatcher();
    int[] count = new int[1];
    TestableZooKeeper zk = createClient(watcher, hostPort, 6000);
    ZooKeeper zk2 = createClient(watcher, hostPort, 5000);
    zk2.create("/test", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    for (int i = 0; i < COUNT / 2; i++) {
        watches[i] = new MyWatcher();
        cbs[i] = new MyStatCallback();
        zk.exists("/test", watches[i], cbs[i], count);
    }
    zk.exists("/test", false);
    Assert.assertTrue("Failed to pause the connection!", zk.pauseCnxn(3000));
    zk2.close();
    stopServer();
    watches[0].waitForDisconnected(60000);
    for (int i = COUNT / 2; i < COUNT; i++) {
        watches[i] = new MyWatcher();
        cbs[i] = new MyStatCallback();
        zk.exists("/test", watches[i], cbs[i], count);
    }
    startServer();
    watches[COUNT / 2 - 1].waitForConnected(60000);
    Assert.assertEquals(null, zk.exists("/test", false));
    waitForAllWatchers();
    for (int i = 0; i < COUNT / 2; i++) {
        Assert.assertEquals("For " + i, 1, watches[i].events.size());
    }
    for (int i = COUNT / 2; i < COUNT; i++) {
        if (cbs[i].rc == 0) {
            Assert.assertEquals("For " + i, 1, watches[i].events.size());
        } else {
            Assert.assertEquals("For " + i, 0, watches[i].events.size());
        }
    }
    Assert.assertEquals(COUNT, count[0]);
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Test(org.junit.Test)

Aggregations

TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)13 Test (org.junit.Test)9 KeeperException (org.apache.zookeeper.KeeperException)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 ExistsRequest (org.apache.zookeeper.proto.ExistsRequest)1 ExistsResponse (org.apache.zookeeper.proto.ExistsResponse)1 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)1 RequestHeader (org.apache.zookeeper.proto.RequestHeader)1