Java Code Examples for org.apache.curator.utils.EnsurePath#ensure()

The following examples show how to use org.apache.curator.utils.EnsurePath#ensure() . 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: TestEnsurePath.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void    testBasic() throws Exception
{
    ZooKeeper               client = mock(ZooKeeper.class, Mockito.RETURNS_MOCKS);
    CuratorZookeeperClient  curator = mock(CuratorZookeeperClient.class);
    RetryPolicy             retryPolicy = new RetryOneTime(1);
    RetryLoop               retryLoop = new RetryLoop(retryPolicy, null);
    when(curator.getZooKeeper()).thenReturn(client);
    when(curator.getRetryPolicy()).thenReturn(retryPolicy);
    when(curator.newRetryLoop()).thenReturn(retryLoop);

    Stat                    fakeStat = mock(Stat.class);
    when(client.exists(Mockito.<String>any(), anyBoolean())).thenReturn(fakeStat);
    
    EnsurePath      ensurePath = new EnsurePath("/one/two/three");
    ensurePath.ensure(curator);

    verify(client, times(3)).exists(Mockito.<String>any(), anyBoolean());

    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
}
 
Example 2
Source File: TestFramework.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsurePathWithNamespace() throws Exception
{
    final String namespace = "jz";

    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).namespace(namespace).build();
    client.start();
    try
    {
        EnsurePath ensurePath = new EnsurePath("/pity/the/fool");
        ensurePath.ensure(client.getZookeeperClient());
        Assert.assertNull(client.getZookeeperClient().getZooKeeper().exists("/jz/pity/the/fool", false));

        ensurePath = client.newNamespaceAwareEnsurePath("/pity/the/fool");
        ensurePath.ensure(client.getZookeeperClient());
        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/jz/pity/the/fool", false));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 3
Source File: TestFramework.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsurePathWithNamespace() throws Exception
{
    final String namespace = "jz";

    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).namespace(namespace).build();
    client.start();
    try
    {
        EnsurePath ensurePath = new EnsurePath("/pity/the/fool");
        ensurePath.ensure(client.getZookeeperClient());
        Assert.assertNull(client.getZookeeperClient().getZooKeeper().exists("/jz/pity/the/fool", false));

        ensurePath = client.newNamespaceAwareEnsurePath("/pity/the/fool");
        ensurePath.ensure(client.getZookeeperClient());
        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/jz/pity/the/fool", false));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 4
Source File: TestEnsurePath.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void    testBasic() throws Exception
{
    ZooKeeper               client = mock(ZooKeeper.class, Mockito.RETURNS_MOCKS);
    CuratorZookeeperClient  curator = mock(CuratorZookeeperClient.class);
    RetryPolicy             retryPolicy = new RetryOneTime(1);
    RetryLoop               retryLoop = new RetryLoopImpl(retryPolicy, null);
    when(curator.getZooKeeper()).thenReturn(client);
    when(curator.getRetryPolicy()).thenReturn(retryPolicy);
    when(curator.newRetryLoop()).thenReturn(retryLoop);

    Stat                    fakeStat = mock(Stat.class);
    when(client.exists(Mockito.<String>any(), anyBoolean())).thenReturn(fakeStat);
    
    EnsurePath      ensurePath = new EnsurePath("/one/two/three");
    ensurePath.ensure(curator);

    verify(client, times(3)).exists(Mockito.<String>any(), anyBoolean());

    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
}
 
Example 5
Source File: LeaseManager.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
private void validateZKPath(String zkPath) throws Exception {
    EnsurePath path = zkClient.newNamespaceAwareEnsurePath(zkPath);
    path.ensure(zkClient.getZookeeperClient());
    Stat stat = zkClient.checkExists().forPath(zkPath);
    Preconditions.checkNotNull(stat);
    LOG.info("Path {} ensured", path.getPath());
}
 
Example 6
Source File: TestAbstractZooKeeperConfigurationProvider.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    // start the instance without the admin server!
    InstanceSpec serverSpec = new InstanceSpec(null, -1, -1, -1, true, -1, -1, -1, Collections.singletonMap("zookeeper.admin.enableServer", "false"));
    zkServer = new TestingServer(serverSpec, true);
    client = CuratorFrameworkFactory
            .newClient("localhost:" + zkServer.getPort(),
                    new ExponentialBackoffRetry(1000, 3));
    client.start();

    EnsurePath ensurePath = new EnsurePath(AGENT_PATH);
    ensurePath.ensure(client.getZookeeperClient());
    doSetUp();
}
 
Example 7
Source File: ZkSubscriptionHandler.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public void addSubscription(String location, TreeCacheListener listener) throws Exception {
    CuratorFramework client = curator.getCurator();
    EnsurePath ensureMvTestPath = new EnsurePath(location);
    ensureMvTestPath.ensure(client.getZookeeperClient());
    TreeCache cache = new TreeCache(client, location);
    cache.getListenable().addListener(listener);
    cache.start();
    caches.put(location, cache);
    logger.info("Added ZooKeeper subscriber for " + location + " children.");
}
 
Example 8
Source File: TestEnsurePath.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void    testSimultaneous() throws Exception
{
    ZooKeeper               client = mock(ZooKeeper.class, Mockito.RETURNS_MOCKS);
    RetryPolicy             retryPolicy = new RetryOneTime(1);
    RetryLoop               retryLoop = new RetryLoop(retryPolicy, null);
    final CuratorZookeeperClient  curator = mock(CuratorZookeeperClient.class);
    when(curator.getZooKeeper()).thenReturn(client);
    when(curator.getRetryPolicy()).thenReturn(retryPolicy);
    when(curator.newRetryLoop()).thenReturn(retryLoop);

    final Stat              fakeStat = mock(Stat.class);
    final CountDownLatch    startedLatch = new CountDownLatch(2);
    final CountDownLatch    finishedLatch = new CountDownLatch(2);
    final Semaphore         semaphore = new Semaphore(0);
    when(client.exists(Mockito.<String>any(), anyBoolean())).thenAnswer
    (
        new Answer<Stat>()
        {
            @Override
            public Stat answer(InvocationOnMock invocation) throws Throwable
            {
                semaphore.acquire();
                return fakeStat;
            }
        }
    );

    final EnsurePath    ensurePath = new EnsurePath("/one/two/three");
    ExecutorService     service = Executors.newCachedThreadPool();
    for ( int i = 0; i < 2; ++i )
    {
        service.submit
        (
            new Callable<Void>()
            {
                @Override
                public Void call() throws Exception
                {
                    startedLatch.countDown();
                    ensurePath.ensure(curator);
                    finishedLatch.countDown();
                    return null;
                }
            }
        );
    }

    Assert.assertTrue(startedLatch.await(10, TimeUnit.SECONDS));
    semaphore.release(3);
    Assert.assertTrue(finishedLatch.await(10, TimeUnit.SECONDS));
    verify(client, times(3)).exists(Mockito.<String>any(), anyBoolean());

    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
}
 
Example 9
Source File: TestEnsurePath.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void    testSimultaneous() throws Exception
{
    ZooKeeper               client = mock(ZooKeeper.class, Mockito.RETURNS_MOCKS);
    RetryPolicy             retryPolicy = new RetryOneTime(1);
    RetryLoop               retryLoop = new RetryLoopImpl(retryPolicy, null);
    final CuratorZookeeperClient  curator = mock(CuratorZookeeperClient.class);
    when(curator.getZooKeeper()).thenReturn(client);
    when(curator.getRetryPolicy()).thenReturn(retryPolicy);
    when(curator.newRetryLoop()).thenReturn(retryLoop);

    final Stat              fakeStat = mock(Stat.class);
    final CountDownLatch    startedLatch = new CountDownLatch(2);
    final CountDownLatch    finishedLatch = new CountDownLatch(2);
    final Semaphore         semaphore = new Semaphore(0);
    when(client.exists(Mockito.<String>any(), anyBoolean())).thenAnswer
    (
        new Answer<Stat>()
        {
            @Override
            public Stat answer(InvocationOnMock invocation) throws Throwable
            {
                semaphore.acquire();
                return fakeStat;
            }
        }
    );

    final EnsurePath    ensurePath = new EnsurePath("/one/two/three");
    ExecutorService     service = Executors.newCachedThreadPool();
    for ( int i = 0; i < 2; ++i )
    {
        service.submit
        (
            new Callable<Void>()
            {
                @Override
                public Void call() throws Exception
                {
                    startedLatch.countDown();
                    ensurePath.ensure(curator);
                    finishedLatch.countDown();
                    return null;
                }
            }
        );
    }

    Assert.assertTrue(startedLatch.await(10, TimeUnit.SECONDS));
    semaphore.release(3);
    Assert.assertTrue(finishedLatch.await(10, TimeUnit.SECONDS));
    verify(client, times(3)).exists(Mockito.<String>any(), anyBoolean());

    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
    ensurePath.ensure(curator);
    verifyNoMoreInteractions(client);
}