Search in sources :

Example 91 with Timing

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

the class TestBlockUntilConnected method testBlockUntilConnectedSessionExpired.

/**
 * Test that we got disconnected before calling blockUntilConnected and we reconnect we receive a session expired event.
 */
@Test
public void testBlockUntilConnectedSessionExpired() throws Exception {
    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();
            }
        }
    });
    final CountDownLatch expiredLatch = new CountDownLatch(1);
    client.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            if (event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getState() == Watcher.Event.KeeperState.Expired) {
                expiredLatch.countDown();
            }
        }
    });
    ConnectionStateAccessor.setDebugWaitOnExpiredForClient(client);
    try {
        client.start();
        // Block until we're connected
        Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to connect");
        final long sessionTimeoutMs = client.getZookeeperClient().getConnectionTimeoutMs();
        // Kill the server
        CloseableUtils.closeQuietly(server);
        // Wait until we hit the lost state
        Assert.assertTrue(timing.awaitLatch(lostLatch), "Failed to reach LOST state");
        Thread.sleep(sessionTimeoutMs);
        server = new TestingServer(server.getPort(), server.getTempDirectory());
        // Wait until we get expired event
        Assert.assertTrue(timing.awaitLatch(expiredLatch), "Failed to get Expired event");
        final boolean blockUntilConnected5Seconds = client.blockUntilConnected(5, TimeUnit.SECONDS);
        Assert.assertTrue(client.getZookeeperClient().isConnected(), "ConnectionState.isConnected returned false");
        Assert.assertTrue(blockUntilConnected5Seconds, "BlockUntilConnected returned false");
    } catch (Exception e) {
        Assert.fail("Unexpected exception " + e);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 92 with Timing

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

the class TestFailedDeleteManager method testWithNamespaceAndLostSessionAlt.

@Test
public void testWithNamespaceAndLostSessionAlt() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
    try {
        client.start();
        CuratorFramework namespaceClient = client.usingNamespace("foo");
        namespaceClient.create().forPath("/test-me");
        final CountDownLatch latch = new CountDownLatch(1);
        final Semaphore semaphore = new Semaphore(0);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
                    semaphore.release();
                } else if (newState == ConnectionState.RECONNECTED) {
                    latch.countDown();
                }
            }
        };
        namespaceClient.getConnectionStateListenable().addListener(listener);
        server.stop();
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        try {
            namespaceClient.delete().guaranteed().forPath("/test-me");
            Assert.fail();
        } catch (KeeperException.ConnectionLossException e) {
        // expected
        }
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        timing.sleepABit();
        server.restart();
        Assert.assertTrue(timing.awaitLatch(latch));
        timing.sleepABit();
        Assert.assertNull(namespaceClient.checkExists().forPath("/test-me"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Timing(org.apache.curator.test.Timing) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 93 with Timing

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

the class TestFailedDeleteManager method testWithNamespaceAndLostSession.

@Test
public void testWithNamespaceAndLostSession() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).namespace("aisa").build();
    try {
        client.start();
        client.create().forPath("/test-me");
        final CountDownLatch latch = new CountDownLatch(1);
        final Semaphore semaphore = new Semaphore(0);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
                    semaphore.release();
                } else if (newState == ConnectionState.RECONNECTED) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        server.stop();
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        try {
            client.delete().guaranteed().forPath("/test-me");
            Assert.fail();
        } catch (KeeperException.ConnectionLossException e) {
        // expected
        }
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        timing.sleepABit();
        server.restart();
        Assert.assertTrue(timing.awaitLatch(latch));
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/test-me"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Timing(org.apache.curator.test.Timing) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 94 with Timing

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

the class TestServiceDiscovery method testCrashedServer.

@Test
public void testCrashedServer() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        Timing timing = new Timing();
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        final Semaphore semaphore = new Semaphore(0);
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance, false) {

            @Override
            protected void internalRegisterService(ServiceInstance<String> service) throws Exception {
                super.internalRegisterService(service);
                semaphore.release();
            }
        };
        closeables.add(discovery);
        discovery.start();
        timing.acquireSemaphore(semaphore);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        server.stop();
        server.restart();
        closeables.add(server);
        timing.acquireSemaphore(semaphore);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
    } finally {
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) Semaphore(java.util.concurrent.Semaphore) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 95 with Timing

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

the class TestServiceDiscovery method testCrashedInstance.

@Test
public void testCrashedInstance() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        Timing timing = new Timing();
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance, false);
        closeables.add(discovery);
        discovery.start();
        Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Thread.sleep(timing.multiple(1.5).session());
        Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) Timing(org.apache.curator.test.Timing) 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