Search in sources :

Example 6 with CuratorTransactionResult

use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project druid by druid-io.

the class AnnouncerTest method testSanity.

@Test(timeout = 60_000L)
public void testSanity() throws Exception {
    curator.start();
    curator.blockUntilConnected();
    Announcer announcer = new Announcer(curator, exec);
    announcer.initializeAddedChildren();
    final byte[] billy = StringUtils.toUtf8("billy");
    final String testPath1 = "/test1";
    final String testPath2 = "/somewhere/test2";
    announcer.announce(testPath1, billy);
    Assert.assertNull("/test1 does not exists", curator.checkExists().forPath(testPath1));
    Assert.assertNull("/somewhere/test2 does not exists", curator.checkExists().forPath(testPath2));
    announcer.start();
    while (!announcer.getAddedChildren().contains("/test1")) {
        Thread.sleep(100);
    }
    try {
        Assert.assertArrayEquals("/test1 has data", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertNull("/somewhere/test2 still does not exist", curator.checkExists().forPath(testPath2));
        announcer.announce(testPath2, billy);
        Assert.assertArrayEquals("/test1 still has data", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals("/somewhere/test2 has data", billy, curator.getData().decompressed().forPath(testPath2));
        final CountDownLatch latch = new CountDownLatch(1);
        curator.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) {
                if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(testPath1)) {
                    latch.countDown();
                }
            }
        });
        final CuratorOp deleteOp = curator.transactionOp().delete().forPath(testPath1);
        final Collection<CuratorTransactionResult> results = curator.transaction().forOperations(deleteOp);
        Assert.assertEquals(1, results.size());
        final CuratorTransactionResult result = results.iterator().next();
        // assert delete
        Assert.assertEquals(Code.OK.intValue(), result.getError());
        Assert.assertTrue("Wait for /test1 to be created", timing.forWaiting().awaitLatch(latch));
        Assert.assertArrayEquals("expect /test1 data is restored", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals("expect /somewhere/test2 is still there", billy, curator.getData().decompressed().forPath(testPath2));
        announcer.unannounce(testPath1);
        Assert.assertNull("expect /test1 unannounced", curator.checkExists().forPath(testPath1));
        Assert.assertArrayEquals("expect /somewhere/test2 is still still there", billy, curator.getData().decompressed().forPath(testPath2));
    } finally {
        announcer.stop();
    }
    Assert.assertNull("expect /test1 remains unannounced", curator.checkExists().forPath(testPath1));
    Assert.assertNull("expect /somewhere/test2 unannounced", curator.checkExists().forPath(testPath2));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorOp(org.apache.curator.framework.api.transaction.CuratorOp) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult) Test(org.junit.Test)

Aggregations

CuratorTransactionResult (org.apache.curator.framework.api.transaction.CuratorTransactionResult)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 RetryOneTime (org.apache.curator.retry.RetryOneTime)3 Test (org.testng.annotations.Test)3 OpResult (org.apache.zookeeper.OpResult)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)1 CuratorListener (org.apache.curator.framework.api.CuratorListener)1 CuratorOp (org.apache.curator.framework.api.transaction.CuratorOp)1 KeeperException (org.apache.zookeeper.KeeperException)1 Stat (org.apache.zookeeper.data.Stat)1 Test (org.junit.Test)1