Search in sources :

Example 66 with Timing

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

the class TestReaper method testReapUntilGone.

private void testReapUntilGone(String namespace) throws Exception {
    Timing timing = new Timing();
    Reaper reaper = null;
    CuratorFramework client = makeClient(timing, namespace);
    try {
        client.start();
        reaper = new Reaper(client, 100);
        reaper.start();
        reaper.addPath("/one/two/three", Reaper.Mode.REAP_UNTIL_GONE);
        timing.sleepABit();
        client.create().creatingParentsIfNeeded().forPath("/one/two/three");
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        reaper.addPath("/one/two/three", Reaper.Mode.REAP_UNTIL_GONE);
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing)

Example 67 with Timing

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

the class TestReaper method testSparseUseNoReap.

@Test
public void testSparseUseNoReap() throws Exception {
    final int THRESHOLD = 3000;
    Timing timing = new Timing();
    Reaper reaper = null;
    CuratorFramework client = makeClient(timing, null);
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/one/two/three");
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        final Queue<Reaper.PathHolder> holders = new ConcurrentLinkedQueue<Reaper.PathHolder>();
        final ExecutorService pool = Executors.newCachedThreadPool();
        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);
        reaper = new Reaper(client, service, THRESHOLD) {

            @Override
            protected Future<Void> schedule(final PathHolder pathHolder, int reapingThresholdMs) {
                holders.add(pathHolder);
                final Future<?> f = super.schedule(pathHolder, reapingThresholdMs);
                pool.submit(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        f.get();
                        holders.remove(pathHolder);
                        return null;
                    }
                });
                return null;
            }
        };
        reaper.start();
        reaper.addPath("/one/two/three");
        long start = System.currentTimeMillis();
        boolean emptyCountIsCorrect = false;
        while (// need to loop as the Holder can go in/out of the Reaper's DelayQueue
        ((System.currentTimeMillis() - start) < timing.forWaiting().milliseconds()) && !emptyCountIsCorrect) {
            for (Reaper.PathHolder holder : holders) {
                if (holder.path.endsWith("/one/two/three")) {
                    emptyCountIsCorrect = (holder.emptyCount > 0);
                    break;
                }
            }
            Thread.sleep(1);
        }
        Assert.assertTrue(emptyCountIsCorrect);
        client.create().forPath("/one/two/three/foo");
        Thread.sleep(2 * (THRESHOLD / Reaper.EMPTY_COUNT_THRESHOLD));
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        client.delete().forPath("/one/two/three/foo");
        Thread.sleep(THRESHOLD);
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Callable(java.util.concurrent.Callable) CuratorFramework(org.apache.curator.framework.CuratorFramework) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Timing(org.apache.curator.test.Timing) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.testng.annotations.Test)

Example 68 with Timing

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

the class TestReaper method testRemove.

private void testRemove(String namespace) throws Exception {
    Timing timing = new Timing();
    Reaper reaper = null;
    CuratorFramework client = makeClient(timing, namespace);
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/one/two/three");
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        reaper = new Reaper(client, 100);
        reaper.start();
        reaper.addPath("/one/two/three");
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
        Assert.assertTrue(reaper.removePath("/one/two/three"));
        client.create().forPath("/one/two/three");
        timing.sleepABit();
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing)

Example 69 with Timing

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

the class TestReaper method testUsingLeaderPath.

@Test
public void testUsingLeaderPath() throws Exception {
    final Timing timing = new Timing();
    CuratorFramework client = makeClient(timing, null);
    Reaper reaper1 = null;
    Reaper reaper2 = null;
    try {
        final AtomicInteger reaper1Count = new AtomicInteger();
        reaper1 = new Reaper(client, Reaper.newExecutorService(), 1, "/reaper/leader") {

            @Override
            protected void reap(PathHolder holder) {
                reaper1Count.incrementAndGet();
                super.reap(holder);
            }
        };
        final AtomicInteger reaper2Count = new AtomicInteger();
        reaper2 = new Reaper(client, Reaper.newExecutorService(), 1, "/reaper/leader") {

            @Override
            protected void reap(PathHolder holder) {
                reaper2Count.incrementAndGet();
                super.reap(holder);
            }
        };
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/one/two/three");
        reaper1.start();
        reaper2.start();
        reaper1.addPath("/one/two/three");
        reaper2.addPath("/one/two/three");
        timing.sleepABit();
        Assert.assertTrue((reaper1Count.get() == 0) || (reaper2Count.get() == 0));
        Assert.assertTrue((reaper1Count.get() > 0) || (reaper2Count.get() > 0));
        Reaper activeReaper;
        AtomicInteger inActiveReaperCount;
        if (reaper1Count.get() > 0) {
            activeReaper = reaper1;
            inActiveReaperCount = reaper2Count;
        } else {
            activeReaper = reaper2;
            inActiveReaperCount = reaper1Count;
        }
        Assert.assertEquals(inActiveReaperCount.get(), 0);
        activeReaper.close();
        timing.sleepABit();
        Assert.assertTrue(inActiveReaperCount.get() > 0);
    } finally {
        CloseableUtils.closeQuietly(reaper1);
        CloseableUtils.closeQuietly(reaper2);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 70 with Timing

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

the class TestReaper method testWithEphemerals.

private void testWithEphemerals(String namespace) throws Exception {
    Timing timing = new Timing();
    Reaper reaper = null;
    CuratorFramework client2 = null;
    CuratorFramework client = makeClient(timing, namespace);
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/one/two/three");
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        client2 = makeClient(timing, namespace);
        client2.start();
        for (int i = 0; i < 10; ++i) {
            client2.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/one/two/three/foo-");
        }
        reaper = new Reaper(client, 100);
        reaper.start();
        reaper.addPath("/one/two/three");
        timing.sleepABit();
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        // should clear ephemerals
        client2.close();
        client2 = null;
        Thread.sleep(timing.session());
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
    } finally {
        CloseableUtils.closeQuietly(reaper);
        CloseableUtils.closeQuietly(client2);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing)

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