Search in sources :

Example 11 with Timing

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

the class TestWithCluster method testSessionSurvives.

@Test
public void testSessionSurvives() throws Exception {
    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 ExponentialBackoffRetry(100, 3));
        client.start();
        client.create().withMode(CreateMode.EPHEMERAL).forPath("/temp", "value".getBytes());
        Assert.assertNotNull(client.checkExists().forPath("/temp"));
        for (InstanceSpec spec : cluster.getInstances()) {
            cluster.killServer(spec);
            timing.forWaiting().sleepABit();
            cluster.restartServer(spec);
            timing.sleepABit();
        }
        timing.sleepABit();
        Assert.assertNotNull(client.checkExists().forPath("/temp"));
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) InstanceSpec(org.apache.curator.test.InstanceSpec) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 12 with Timing

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

the class TestWithCluster method testSplitBrain.

@Test
public void testSplitBrain() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = null;
    TestingCluster cluster = new TestingCluster(3);
    cluster.start();
    try {
        // make sure all instances are up
        for (InstanceSpec instanceSpec : cluster.getInstances()) {
            client = CuratorFrameworkFactory.newClient(instanceSpec.getConnectString(), new RetryOneTime(1));
            client.start();
            client.checkExists().forPath("/");
            client.close();
            client = null;
        }
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        client.start();
        final CountDownLatch latch = new CountDownLatch(2);
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
                    latch.countDown();
                }
            }
        });
        client.checkExists().forPath("/");
        for (InstanceSpec instanceSpec : cluster.getInstances()) {
            if (!instanceSpec.equals(cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper()))) {
                Assert.assertTrue(cluster.killServer(instanceSpec));
            }
        }
        Assert.assertTrue(timing.awaitLatch(latch));
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) InstanceSpec(org.apache.curator.test.InstanceSpec) 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)

Example 13 with Timing

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

the class TestFailedDeleteManager method testLostSession.

@Test
public void testLostSession() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    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 14 with Timing

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

the class TestFrameworkBackground method testShutdown.

/**
 * CURATOR-126
 * Shutdown the Curator client while there are still background operations running.
 */
@Test
public void testShutdown() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).maxCloseWaitMs(timing.forWaiting().milliseconds()).build();
    try {
        final AtomicBoolean hadIllegalStateException = new AtomicBoolean(false);
        ((CuratorFrameworkImpl) client).debugUnhandledErrorListener = new UnhandledErrorListener() {

            @Override
            public void unhandledError(String message, Throwable e) {
                if (e instanceof IllegalStateException) {
                    hadIllegalStateException.set(true);
                }
            }
        };
        client.start();
        final CountDownLatch operationReadyLatch = new CountDownLatch(1);
        ((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() {

            @Override
            public void listen(OperationAndData<?> data) {
                try {
                    operationReadyLatch.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        };
        // queue a background operation that will block due to the debugListener
        client.create().inBackground().forPath("/hey");
        timing.sleepABit();
        // close the client while the background is still blocked
        client.close();
        // unblock the background
        operationReadyLatch.countDown();
        timing.sleepABit();
        // should not generate an exception
        Assert.assertFalse(hadIllegalStateException.get());
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) Test(org.testng.annotations.Test)

Example 15 with Timing

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

the class TestFrameworkBackground method testListenerConnectedAtStart.

@Test
public void testListenerConnectedAtStart() throws Exception {
    server.stop();
    Timing timing = new Timing(2);
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(0, 0));
    try {
        client.start();
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        final AtomicBoolean firstListenerAction = new AtomicBoolean(true);
        final AtomicReference<ConnectionState> firstListenerState = new AtomicReference<ConnectionState>();
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (firstListenerAction.compareAndSet(true, false)) {
                    firstListenerState.set(newState);
                    System.out.println("First listener state is " + newState);
                }
                if (newState == ConnectionState.CONNECTED) {
                    connectedLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        // due to CURATOR-72, this was causing a LOST event to precede the CONNECTED event
        client.create().inBackground().forPath("/foo");
        server.restart();
        Assert.assertTrue(timing.awaitLatch(connectedLatch));
        Assert.assertFalse(firstListenerAction.get());
        ConnectionState firstconnectionState = firstListenerState.get();
        Assert.assertEquals(firstconnectionState, ConnectionState.CONNECTED, "First listener state MUST BE CONNECTED but is " + firstconnectionState);
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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