Search in sources :

Example 96 with Timing

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

the class TestServiceDiscovery method testCrashedServerMultiInstances.

@Test
public void testCrashedServerMultiInstances() 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> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceInstance<String> instance2 = ServiceInstance.<String>builder().payload("thing").name("test").port(10065).build();
        ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance1, false) {

            @Override
            protected void internalRegisterService(ServiceInstance<String> service) throws Exception {
                super.internalRegisterService(service);
                semaphore.release();
            }
        };
        closeables.add(discovery);
        discovery.start();
        discovery.registerService(instance2);
        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        server.stop();
        server.restart();
        closeables.add(server);
        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
    } 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 97 with Timing

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

the class TestServiceDiscovery method testUnregisterService.

// CURATOR-164
@Test
public void testUnregisterService() throws Exception {
    final String name = "name";
    final CountDownLatch restartLatch = new CountDownLatch(1);
    List<Closeable> closeables = Lists.newArrayList();
    InstanceSerializer<String> slowSerializer = new JsonInstanceSerializer<String>(String.class) {

        private boolean first = true;

        @Override
        public byte[] serialize(ServiceInstance<String> instance) throws Exception {
            if (first) {
                System.out.println("Serializer first registration.");
                first = false;
            } else {
                System.out.println("Waiting for reconnect to finish.");
                // Simulate the serialize method being slow.
                // This could just be a timed wait, but that's kind of non-deterministic.
                restartLatch.await();
            }
            return super.serialize(instance);
        }
    };
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name(name).port(10064).build();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).serializer(slowSerializer).watchInstances(true).build();
        closeables.add(discovery);
        discovery.start();
        Assert.assertFalse(discovery.queryForInstances(name).isEmpty(), "Service should start registered.");
        server.stop();
        server.restart();
        discovery.unregisterService(instance);
        restartLatch.countDown();
        // Wait for the rest of registration to finish.
        new Timing().sleepABit();
        Assert.assertTrue(discovery.queryForInstances(name).isEmpty(), "Service should have unregistered.");
    } 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) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 98 with Timing

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

the class TestWatchedInstances method testWatchedInstances.

@Test
public void testWatchedInstances() throws Exception {
    Timing timing = new Timing();
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).watchInstances(true).build();
        closeables.add(discovery);
        discovery.start();
        Assert.assertEquals(discovery.queryForNames(), Arrays.asList("test"));
        List<ServiceInstance<String>> list = Lists.newArrayList();
        list.add(instance);
        Assert.assertEquals(discovery.queryForInstances("test"), list);
        ServiceDiscoveryImpl<String> discoveryImpl = (ServiceDiscoveryImpl<String>) discovery;
        ServiceInstance<String> changedInstance = ServiceInstance.<String>builder().id(instance.getId()).address(instance.getAddress()).payload("different").name(instance.getName()).port(instance.getPort()).build();
        String path = discoveryImpl.pathForInstance("test", instance.getId());
        byte[] bytes = discoveryImpl.getSerializer().serialize(changedInstance);
        client.setData().forPath(path, bytes);
        timing.sleepABit();
        ServiceInstance<String> registeredService = discoveryImpl.getRegisteredService(instance.getId());
        Assert.assertNotNull(registeredService);
        Assert.assertEquals(registeredService.getPayload(), "different");
    } finally {
        Collections.reverse(closeables);
        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) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 99 with Timing

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

the class TestSessionFailRetryLoop method testBasic.

@Test
public void testBasic() throws Exception {
    Timing timing = new Timing();
    final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
    SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
    retryLoop.start();
    try {
        client.start();
        try {
            while (retryLoop.shouldContinue()) {
                try {
                    RetryLoop.callWithRetry(client, new Callable<Void>() {

                        @Override
                        public Void call() throws Exception {
                            Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                            KillSession.kill(client.getZooKeeper(), server.getConnectString());
                            client.getZooKeeper();
                            client.blockUntilConnectedOrTimedOut();
                            Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                            return null;
                        }
                    });
                } catch (Exception e) {
                    retryLoop.takeException(e);
                }
            }
            Assert.fail();
        } catch (SessionFailRetryLoop.SessionFailedException dummy) {
        // correct
        }
    } finally {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 100 with Timing

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

the class TestChildReaper method testNamespace.

@Test
public void testNamespace() throws Exception {
    Timing timing = new Timing();
    ChildReaper reaper = null;
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).namespace("foo").build();
    try {
        client.start();
        for (int i = 0; i < 10; ++i) {
            client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
        }
        reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
        reaper.start();
        timing.forWaiting().sleepABit();
        Stat stat = client.checkExists().forPath("/test");
        Assert.assertEquals(stat.getNumChildren(), 0);
        stat = client.usingNamespace(null).checkExists().forPath("/foo/test");
        Assert.assertNotNull(stat);
        Assert.assertEquals(stat.getNumChildren(), 0);
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Stat(org.apache.zookeeper.data.Stat) 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