Search in sources :

Example 26 with Timing

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

the class TestFailedDeleteManager method testBasic.

@Test
public void testBasic() throws Exception {
    final String PATH = "/one/two/three";
    Timing timing = new Timing();
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).connectionTimeoutMs(timing.connection()).sessionTimeoutMs(timing.session());
    CuratorFrameworkImpl client = new CuratorFrameworkImpl(builder);
    client.start();
    try {
        client.create().creatingParentsIfNeeded().forPath(PATH);
        Assert.assertNotNull(client.checkExists().forPath(PATH));
        // cause the next delete to fail
        server.stop();
        try {
            client.delete().forPath(PATH);
            Assert.fail();
        } catch (KeeperException.ConnectionLossException e) {
        // expected
        }
        server.restart();
        Assert.assertNotNull(client.checkExists().forPath(PATH));
        // cause the next delete to fail
        server.stop();
        try {
            client.delete().guaranteed().forPath(PATH);
            Assert.fail();
        } catch (KeeperException.ConnectionLossException e) {
        // expected
        }
        server.restart();
        final int TRIES = 5;
        for (int i = 0; i < TRIES; ++i) {
            if (client.checkExists().forPath(PATH) != null) {
                timing.sleepABit();
            }
        }
        Assert.assertNull(client.checkExists().forPath(PATH));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) Timing(org.apache.curator.test.Timing) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 27 with Timing

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

the class TestLeaderSelectorCluster method testLostRestart.

@Test
public void testLostRestart() throws Exception {
    final Timing timing = new Timing();
    CuratorFramework client = null;
    TestingCluster cluster = new TestingCluster(3);
    cluster.start();
    try {
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        client.start();
        client.sync().forPath("/");
        final AtomicReference<Exception> error = new AtomicReference<Exception>(null);
        final AtomicReference<String> lockNode = new AtomicReference<String>(null);
        final Semaphore semaphore = new Semaphore(0);
        final CountDownLatch lostLatch = new CountDownLatch(1);
        final CountDownLatch internalLostLatch = new CountDownLatch(1);
        LeaderSelectorListener listener = new LeaderSelectorListener() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                try {
                    List<String> names = client.getChildren().forPath("/leader");
                    if (names.size() != 1) {
                        semaphore.release();
                        Exception exception = new Exception("Names size isn't 1: " + names.size());
                        error.set(exception);
                        return;
                    }
                    lockNode.set(names.get(0));
                    semaphore.release();
                    if (!timing.multiple(4).awaitLatch(internalLostLatch)) {
                        error.set(new Exception("internalLostLatch await failed"));
                    }
                } finally {
                    lostLatch.countDown();
                }
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    internalLostLatch.countDown();
                }
            }
        };
        LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
        selector.start();
        Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
        if (error.get() != null) {
            throw new AssertionError(error.get());
        }
        Collection<InstanceSpec> instances = cluster.getInstances();
        cluster.stop();
        Assert.assertTrue(timing.multiple(4).awaitLatch(lostLatch));
        timing.sleepABit();
        Assert.assertFalse(selector.hasLeadership());
        Assert.assertNotNull(lockNode.get());
        cluster = new TestingCluster(instances.toArray(new InstanceSpec[instances.size()]));
        cluster.start();
        try {
            // simulate the lock deleting due to session expiration
            client.delete().forPath(ZKPaths.makePath("/leader", lockNode.get()));
        } catch (Exception ignore) {
        // ignore
        }
        Assert.assertTrue(semaphore.availablePermits() == 0);
        Assert.assertFalse(selector.hasLeadership());
        selector.requeue();
        Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) InstanceSpec(org.apache.curator.test.InstanceSpec) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) Test(org.testng.annotations.Test)

Example 28 with Timing

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

the class TestSharedCount method testMultiClientVersioned.

@Test
public void testMultiClientVersioned() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    CuratorFramework client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    SharedCount count1 = new SharedCount(client1, "/count", 0);
    SharedCount count2 = new SharedCount(client2, "/count", 0);
    try {
        client1.start();
        client2.start();
        count1.start();
        count2.start();
        VersionedValue<Integer> versionedValue = count1.getVersionedValue();
        Assert.assertTrue(count1.trySetCount(versionedValue, 10));
        timing.sleepABit();
        versionedValue = count2.getVersionedValue();
        Assert.assertTrue(count2.trySetCount(versionedValue, 20));
        timing.sleepABit();
        VersionedValue<Integer> versionedValue1 = count1.getVersionedValue();
        VersionedValue<Integer> versionedValue2 = count2.getVersionedValue();
        Assert.assertTrue(count2.trySetCount(versionedValue2, 30));
        Assert.assertFalse(count1.trySetCount(versionedValue1, 40));
        versionedValue1 = count1.getVersionedValue();
        Assert.assertTrue(count1.trySetCount(versionedValue1, 40));
    } finally {
        CloseableUtils.closeQuietly(count2);
        CloseableUtils.closeQuietly(count1);
        CloseableUtils.closeQuietly(client2);
        CloseableUtils.closeQuietly(client1);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 29 with Timing

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

the class TestInterProcessSemaphoreCluster method testKilledServerWithEnsembleProvider.

@Test
public void testKilledServerWithEnsembleProvider() throws Exception {
    final int CLIENT_QTY = 10;
    final Timing timing = new Timing();
    final String PATH = "/foo/bar/lock";
    ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    TestingCluster cluster = new TestingCluster(3);
    try {
        cluster.start();
        final AtomicReference<String> connectionString = new AtomicReference<String>(cluster.getConnectString());
        final EnsembleProvider provider = new EnsembleProvider() {

            @Override
            public void start() throws Exception {
            }

            @Override
            public String getConnectionString() {
                return connectionString.get();
            }

            @Override
            public void close() throws IOException {
            }
        };
        final Semaphore acquiredSemaphore = new Semaphore(0);
        final AtomicInteger acquireCount = new AtomicInteger(0);
        final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY);
        for (int i = 0; i < CLIENT_QTY; ++i) {
            completionService.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
                    try {
                        final Semaphore suspendedSemaphore = new Semaphore(0);
                        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

                            @Override
                            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                                if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
                                    suspendedLatch.countDown();
                                    suspendedSemaphore.release();
                                }
                            }
                        });
                        client.start();
                        InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1);
                        while (!Thread.currentThread().isInterrupted()) {
                            Lease lease = null;
                            try {
                                lease = semaphore.acquire();
                                acquiredSemaphore.release();
                                acquireCount.incrementAndGet();
                                suspendedSemaphore.acquire();
                            } catch (Exception e) {
                            // just retry
                            } finally {
                                if (lease != null) {
                                    acquireCount.decrementAndGet();
                                    CloseableUtils.closeQuietly(lease);
                                }
                            }
                        }
                    } finally {
                        CloseableUtils.closeQuietly(client);
                    }
                    return null;
                }
            });
        }
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        Assert.assertEquals(1, acquireCount.get());
        cluster.close();
        timing.awaitLatch(suspendedLatch);
        timing.forWaiting().sleepABit();
        Assert.assertEquals(0, acquireCount.get());
        cluster = new TestingCluster(3);
        cluster.start();
        connectionString.set(cluster.getConnectString());
        timing.forWaiting().sleepABit();
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(1, acquireCount.get());
    } finally {
        executorService.shutdown();
        executorService.awaitTermination(10, TimeUnit.SECONDS);
        executorService.shutdownNow();
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) 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 30 with Timing

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

the class TestLeaderLatch method testProperCloseWithoutConnectionEstablished.

@Test
public void testProperCloseWithoutConnectionEstablished() throws Exception {
    server.stop();
    Timing timing = new Timing();
    LeaderLatch latch = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        final AtomicBoolean resetCalled = new AtomicBoolean(false);
        final CountDownLatch cancelStartTaskLatch = new CountDownLatch(1);
        latch = new LeaderLatch(client, PATH_NAME) {

            @Override
            void reset() throws Exception {
                resetCalled.set(true);
                super.reset();
            }

            @Override
            protected boolean cancelStartTask() {
                if (super.cancelStartTask()) {
                    cancelStartTaskLatch.countDown();
                    return true;
                }
                return false;
            }
        };
        latch.start();
        latch.close();
        latch = null;
        Assert.assertTrue(timing.awaitLatch(cancelStartTaskLatch));
        Assert.assertFalse(resetCalled.get());
    } finally {
        CloseableUtils.closeQuietly(latch);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) 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