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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations