Java Code Examples for org.apache.curator.test.Timing#connection()

The following examples show how to use org.apache.curator.test.Timing#connection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestExhibitorEnsembleProvider.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void     testSimple() throws Exception
{
    Exhibitors                  exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
    ExhibitorRestClient         mockRestClient = new ExhibitorRestClient()
    {
        @Override
        public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception
        {
            return "count=1&port=" + server.getPort() + "&server0=localhost";
        }
    };
    ExhibitorEnsembleProvider   provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
    provider.pollForInitialEnsemble();

    Timing                      timing = new Timing();
    CuratorZookeeperClient      client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new ExponentialBackoffRetry(timing.milliseconds(), 3));
    client.start();
    try
    {
        client.blockUntilConnectedOrTimedOut();
        client.getZooKeeper().exists("/", false);
    }
    catch ( Exception e )
    {
        Assert.fail("provider.getConnectionString(): " + provider.getConnectionString() + " server.getPort(): " + server.getPort(), e);
    }
    finally
    {
        client.close();
    }
}
 
Example 2
Source File: TestExhibitorEnsembleProvider.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testChanging() throws Exception
{
    TestingServer               secondServer = new TestingServer();
    try
    {
        String                          mainConnectionString = "count=1&port=" + server.getPort() + "&server0=localhost";
        String                          secondConnectionString = "count=1&port=" + secondServer.getPort() + "&server0=localhost";

        final Semaphore                 semaphore = new Semaphore(0);
        final AtomicReference<String>   connectionString = new AtomicReference<String>(mainConnectionString);
        Exhibitors                      exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
        ExhibitorRestClient             mockRestClient = new ExhibitorRestClient()
        {
            @Override
            public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception
            {
                semaphore.release();
                return connectionString.get();
            }
        };
        ExhibitorEnsembleProvider   provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
        provider.pollForInitialEnsemble();

        Timing                          timing = new Timing().multiple(4);
        final CuratorZookeeperClient    client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new RetryOneTime(2));
        client.start();
        try
        {
            RetryLoop.callWithRetry
            (
                client,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        client.getZooKeeper().create("/test", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                        return null;
                    }
                }
            );

            connectionString.set(secondConnectionString);
            semaphore.drainPermits();
            semaphore.acquire();

            server.stop();  // create situation where the current zookeeper gets a sys-disconnected

            Stat        stat = RetryLoop.callWithRetry
            (
                client,
                new Callable<Stat>()
                {
                    @Override
                    public Stat call() throws Exception
                    {
                        return client.getZooKeeper().exists("/test", false);
                    }
                }
            );
            Assert.assertNull(stat);    // it's a different server so should be null
        }
        finally
        {
            client.close();
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(secondServer);
    }
}
 
Example 3
Source File: TestSessionFailRetryLoop.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testRetry() 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.RETRY);
    retryLoop.start();
    try
    {
        client.start();
        final AtomicBoolean     secondWasDone = new AtomicBoolean(false);
        final AtomicBoolean     firstTime = new AtomicBoolean(true);
        while ( retryLoop.shouldContinue() )
        {
            try
            {
                RetryLoop.callWithRetry
                (
                    client,
                    new Callable<Void>()
                    {
                        @Override
                        public Void call() throws Exception
                        {
                            if ( firstTime.compareAndSet(true, false) )
                            {
                                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;
                        }
                    }
                );

                RetryLoop.callWithRetry
                (
                    client,
                    new Callable<Void>()
                    {
                        @Override
                        public Void call() throws Exception
                        {
                            Assert.assertFalse(firstTime.get());
                            Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                            secondWasDone.set(true);
                            return null;
                        }
                    }
                );
            }
            catch ( Exception e )
            {
                retryLoop.takeException(e);
            }
        }

        Assert.assertTrue(secondWasDone.get());
    }
    finally
    {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 4
Source File: TestSessionFailRetryLoop.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testRetryStatic() 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.RETRY);
    retryLoop.start();
    try
    {
        client.start();
        final AtomicBoolean     secondWasDone = new AtomicBoolean(false);
        final AtomicBoolean     firstTime = new AtomicBoolean(true);
        SessionFailRetryLoop.callWithRetry
        (
            client,
            SessionFailRetryLoop.Mode.RETRY,
            new Callable<Object>()
            {
                @Override
                public Object call() throws Exception
                {
                    RetryLoop.callWithRetry
                    (
                        client,
                        new Callable<Void>()
                        {
                            @Override
                            public Void call() throws Exception
                            {
                                if ( firstTime.compareAndSet(true, false) )
                                {
                                    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;
                            }
                        }
                    );

                    RetryLoop.callWithRetry
                    (
                        client,
                        new Callable<Void>()
                        {
                            @Override
                            public Void call() throws Exception
                            {
                                Assert.assertFalse(firstTime.get());
                                Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                                secondWasDone.set(true);
                                return null;
                            }
                        }
                    );
                    return null;
                }
            }
        );

        Assert.assertTrue(secondWasDone.get());
    }
    finally
    {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 5
Source File: TestSessionFailRetryLoop.java    From xian with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: TestSessionFailRetryLoop.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testBasicStatic() 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
        {
            SessionFailRetryLoop.callWithRetry
            (
                client,
                SessionFailRetryLoop.Mode.FAIL,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        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;
                                }
                            }
                        );
                        return null;
                    }
                }
            );
        }
        catch ( SessionFailRetryLoop.SessionFailedException dummy )
        {
            // correct
        }
    }
    finally
    {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 7
Source File: BasicTests.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testExpiredSession() throws Exception
{
    // see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4

    final Timing                  timing = new Timing();

    final CountDownLatch    latch = new CountDownLatch(1);
    Watcher                 watcher = new Watcher()
    {
        @Override
        public void process(WatchedEvent event)
        {
            if ( event.getState() == Event.KeeperState.Expired )
            {
                latch.countDown();
            }
        }
    };

    final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), watcher, new RetryOneTime(2));
    client.start();
    try
    {
        final AtomicBoolean     firstTime = new AtomicBoolean(true);
        RetryLoop.callWithRetry
        (
            client,
            new Callable<Object>()
            {
                @Override
                public Object call() throws Exception
                {
                    if ( firstTime.compareAndSet(true, false) )
                    {
                        try
                        {
                            client.getZooKeeper().create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                        }
                        catch ( KeeperException.NodeExistsException ignore )
                        {
                            // ignore
                        }

                        KillSession.kill(client.getZooKeeper(), server.getConnectString());

                        Assert.assertTrue(timing.awaitLatch(latch));
                    }
                    ZooKeeper zooKeeper = client.getZooKeeper();
                    client.blockUntilConnectedOrTimedOut();
                    Assert.assertNotNull(zooKeeper.exists("/foo", false));
                    return null;
                }
            }
        );
    }
    finally
    {
        client.close();
    }
}
 
Example 8
Source File: TestSessionFailRetryLoop.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void     testRetry() throws Exception
{
    Timing                          timing = new Timing();
    final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
    SessionFailRetryLoop            retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
    retryLoop.start();
    try
    {
        client.start();
        final AtomicBoolean     secondWasDone = new AtomicBoolean(false);
        final AtomicBoolean     firstTime = new AtomicBoolean(true);
        while ( retryLoop.shouldContinue() )
        {
            try
            {
                RetryLoop.callWithRetry
                (
                    client,
                    new Callable<Void>()
                    {
                        @Override
                        public Void call() throws Exception
                        {
                            if ( firstTime.compareAndSet(true, false) )
                            {
                                Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                                client.getZooKeeper().getTestable().injectSessionExpiration();
                                client.getZooKeeper();
                                client.blockUntilConnectedOrTimedOut();
                            }

                            Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                            return null;
                        }
                    }
                );

                RetryLoop.callWithRetry
                (
                    client,
                    new Callable<Void>()
                    {
                        @Override
                        public Void call() throws Exception
                        {
                            Assert.assertFalse(firstTime.get());
                            Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                            secondWasDone.set(true);
                            return null;
                        }
                    }
                );
            }
            catch ( Exception e )
            {
                retryLoop.takeException(e);
            }
        }

        Assert.assertTrue(secondWasDone.get());
    }
    finally
    {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 9
Source File: TestSessionFailRetryLoop.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void     testRetryStatic() throws Exception
{
    Timing                          timing = new Timing();
    final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
    SessionFailRetryLoop            retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
    retryLoop.start();
    try
    {
        client.start();
        final AtomicBoolean     secondWasDone = new AtomicBoolean(false);
        final AtomicBoolean     firstTime = new AtomicBoolean(true);
        SessionFailRetryLoop.callWithRetry
        (
            client,
            SessionFailRetryLoop.Mode.RETRY,
            new Callable<Object>()
            {
                @Override
                public Object call() throws Exception
                {
                    RetryLoop.callWithRetry
                    (
                        client,
                        new Callable<Void>()
                        {
                            @Override
                            public Void call() throws Exception
                            {
                                if ( firstTime.compareAndSet(true, false) )
                                {
                                    Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                                    client.getZooKeeper().getTestable().injectSessionExpiration();
                                    client.getZooKeeper();
                                    client.blockUntilConnectedOrTimedOut();
                                }

                                Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                                return null;
                            }
                        }
                    );

                    RetryLoop.callWithRetry
                    (
                        client,
                        new Callable<Void>()
                        {
                            @Override
                            public Void call() throws Exception
                            {
                                Assert.assertFalse(firstTime.get());
                                Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                                secondWasDone.set(true);
                                return null;
                            }
                        }
                    );
                    return null;
                }
            }
        );

        Assert.assertTrue(secondWasDone.get());
    }
    finally
    {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 10
Source File: TestSessionFailRetryLoop.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void     testBasic() throws Exception
{
    final Timing                          timing = new Timing();
    final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
    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));
                                client.getZooKeeper().getTestable().injectSessionExpiration();

                                timing.sleepABit();

                                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);
    }
}
 
Example 11
Source File: TestSessionFailRetryLoop.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void     testBasicStatic() throws Exception
{
    Timing                          timing = new Timing();
    final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
    SessionFailRetryLoop            retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
    retryLoop.start();
    try
    {
        client.start();
        try
        {
            SessionFailRetryLoop.callWithRetry
            (
                client,
                SessionFailRetryLoop.Mode.FAIL,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        RetryLoop.callWithRetry
                        (
                            client,
                            new Callable<Void>()
                            {
                                @Override
                                public Void call() throws Exception
                                {
                                    Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                                    client.getZooKeeper().getTestable().injectSessionExpiration();

                                    client.getZooKeeper();
                                    client.blockUntilConnectedOrTimedOut();
                                    Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
                                    return null;
                                }
                            }
                        );
                        return null;
                    }
                }
            );
        }
        catch ( SessionFailRetryLoop.SessionFailedException dummy )
        {
            // correct
        }
    }
    finally
    {
        retryLoop.close();
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 12
Source File: BasicTests.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void     testExpiredSession() throws Exception
{
    // see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4

    final Timing                  timing = new Timing();

    final CountDownLatch    latch = new CountDownLatch(1);
    Watcher                 watcher = new Watcher()
    {
        @Override
        public void process(WatchedEvent event)
        {
            if ( event.getState() == Event.KeeperState.Expired )
            {
                latch.countDown();
            }
        }
    };

    final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), watcher, new RetryOneTime(2));
    client.start();
    try
    {
        final AtomicBoolean     firstTime = new AtomicBoolean(true);
        RetryLoop.callWithRetry
        (
            client,
            new Callable<Object>()
            {
                @Override
                public Object call() throws Exception
                {
                    if ( firstTime.compareAndSet(true, false) )
                    {
                        try
                        {
                            client.getZooKeeper().create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                        }
                        catch ( KeeperException.NodeExistsException ignore )
                        {
                            // ignore
                        }

                        client.getZooKeeper().getTestable().injectSessionExpiration();

                        Assert.assertTrue(timing.awaitLatch(latch));
                    }
                    ZooKeeper zooKeeper = client.getZooKeeper();
                    client.blockUntilConnectedOrTimedOut();
                    Assert.assertNotNull(zooKeeper.exists("/foo", false));
                    return null;
                }
            }
        );
    }
    finally
    {
        client.close();
    }
}