Search in sources :

Example 21 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFramework method testConnectionState.

@Test
public void testConnectionState() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        final BlockingQueue<ConnectionState> queue = new LinkedBlockingQueue<ConnectionState>();
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                queue.add(newState);
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        client.start();
        Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS), ConnectionState.CONNECTED);
        server.stop();
        Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS), ConnectionState.LOST);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 22 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFramework method testCreateParentContainers.

@Test
public void testCreateParentContainers() throws Exception {
    if (!checkForContainers()) {
        return;
    }
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
    try {
        client.start();
        client.create().creatingParentContainersIfNeeded().forPath("/one/two/three", "foo".getBytes());
        byte[] data = client.getData().forPath("/one/two/three");
        Assert.assertEquals(data, "foo".getBytes());
        client.delete().forPath("/one/two/three");
        new Timing().sleepABit();
        Assert.assertNull(client.checkExists().forPath("/one/two"));
        new Timing().sleepABit();
        Assert.assertNull(client.checkExists().forPath("/one"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 23 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFramework method testCreateACLWithReset.

@Test
public void testCreateACLWithReset() throws Exception {
    Timing timing = new Timing();
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework client = builder.connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).authorization("digest", "me:pass".getBytes()).retryPolicy(new RetryOneTime(1)).build();
    client.start();
    try {
        final CountDownLatch lostLatch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    lostLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        ACL acl = new ACL(ZooDefs.Perms.WRITE, ZooDefs.Ids.AUTH_IDS);
        List<ACL> aclList = Lists.newArrayList(acl);
        client.create().withACL(aclList).forPath("/test", "test".getBytes());
        server.stop();
        Assert.assertTrue(timing.awaitLatch(lostLatch));
        try {
            client.checkExists().forPath("/");
            Assert.fail("Connection should be down");
        } catch (KeeperException.ConnectionLossException e) {
        // expected
        }
        server.restart();
        try {
            client.setData().forPath("/test", "test".getBytes());
        } catch (KeeperException.NoAuthException e) {
            Assert.fail("Auth failed");
        }
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ACL(org.apache.zookeeper.data.ACL) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 24 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestFramework method testExistsCreatingParentsInBackground.

@Test
public void testExistsCreatingParentsInBackground() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try {
        client.start();
        Assert.assertNull(client.checkExists().forPath("/one/two"));
        final CountDownLatch latch = new CountDownLatch(1);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                latch.countDown();
            }
        };
        client.checkExists().creatingParentContainersIfNeeded().inBackground(callback).forPath("/one/two/three");
        Assert.assertTrue(new Timing().awaitLatch(latch));
        Assert.assertNotNull(client.checkExists().forPath("/one/two"));
        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
        Assert.assertNull(client.checkExists().creatingParentContainersIfNeeded().forPath("/one/two/three"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 25 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestBlockUntilConnected method testBlockUntilConnectedCurrentlyAwaitingReconnect.

/**
 * Test the case where we are not currently connected, but have been previously
 */
@Test
public void testBlockUntilConnectedCurrentlyAwaitingReconnect() {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
    final CountDownLatch lostLatch = new CountDownLatch(1);
    client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            if (newState == ConnectionState.LOST) {
                lostLatch.countDown();
            }
        }
    });
    try {
        client.start();
        // Block until we're connected
        Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to connect");
        // Kill the server
        CloseableUtils.closeQuietly(server);
        // Wait until we hit the lost state
        Assert.assertTrue(timing.awaitLatch(lostLatch), "Failed to reach LOST state");
        server = new TestingServer(server.getPort(), server.getTempDirectory());
        Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Not connected");
    } catch (Exception e) {
        Assert.fail("Unexpected exception " + e);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Aggregations

Timing (org.apache.curator.test.Timing)166 CuratorFramework (org.apache.curator.framework.CuratorFramework)147 Test (org.testng.annotations.Test)138 RetryOneTime (org.apache.curator.retry.RetryOneTime)129 CountDownLatch (java.util.concurrent.CountDownLatch)56 ConnectionState (org.apache.curator.framework.state.ConnectionState)39 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)29 KeeperException (org.apache.zookeeper.KeeperException)28 ExecutorService (java.util.concurrent.ExecutorService)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Semaphore (java.util.concurrent.Semaphore)18 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)16 TestingServer (org.apache.curator.test.TestingServer)15 Stat (org.apache.zookeeper.data.Stat)12 TestingCluster (org.apache.curator.test.TestingCluster)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 InstanceSpec (org.apache.curator.test.InstanceSpec)9 Test (org.junit.Test)9