use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessMutexBase method testContainerCleanup.
@Test
public void testContainerCleanup() throws Exception {
if (!ZKPaths.hasContainerSupport()) {
System.out.println("ZooKeeper version does not support Containers. Skipping test");
return;
}
server.close();
System.setProperty("container.checkIntervalMs", "10");
try {
server = new TestingServer();
final int THREAD_QTY = 10;
ExecutorService service = null;
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(100, 3));
try {
client.start();
List<Future<Object>> threads = Lists.newArrayList();
service = Executors.newCachedThreadPool();
for (int i = 0; i < THREAD_QTY; ++i) {
Future<Object> t = service.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
InterProcessLock lock = makeLock(client);
lock.acquire();
try {
Thread.sleep(10);
} finally {
lock.release();
}
return null;
}
});
threads.add(t);
}
for (Future<Object> t : threads) {
t.get();
}
new Timing().sleepABit();
Assert.assertNull(client.checkExists().forPath(LOCK_BASE_PATH));
} finally {
if (service != null) {
service.shutdownNow();
}
CloseableUtils.closeQuietly(client);
}
} finally {
System.clearProperty("container.checkIntervalMs");
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessMutexBase method testWaitingProcessKilledServer.
@Test
public void testWaitingProcessKilledServer() throws Exception {
final Timing timing = new Timing();
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
try {
client.start();
final CountDownLatch latch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
final AtomicBoolean isFirst = new AtomicBoolean(true);
ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(Executors.newFixedThreadPool(2));
for (int i = 0; i < 2; ++i) {
service.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
InterProcessLock lock = makeLock(client);
lock.acquire();
try {
if (isFirst.compareAndSet(true, false)) {
timing.sleepABit();
server.stop();
Assert.assertTrue(timing.awaitLatch(latch));
server.restart();
}
} finally {
try {
lock.release();
} catch (Exception e) {
// ignore
}
}
return null;
}
});
}
for (int i = 0; i < 2; ++i) {
service.take().get(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
}
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPathChildrenCache method testBasicsOnTwoCachesWithSameExecutor.
@Test
public void testBasicsOnTwoCachesWithSameExecutor() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
client.create().forPath("/test");
final BlockingQueue<PathChildrenCacheEvent.Type> events = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
final ExecutorService exec = Executors.newSingleThreadExecutor();
PathChildrenCache cache = new PathChildrenCache(client, "/test", true, false, exec);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (event.getData().getPath().equals("/test/one")) {
events.offer(event.getType());
}
}
});
cache.start();
final BlockingQueue<PathChildrenCacheEvent.Type> events2 = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
PathChildrenCache cache2 = new PathChildrenCache(client, "/test", true, false, exec);
cache2.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (event.getData().getPath().equals("/test/one")) {
events2.offer(event.getType());
}
}
});
cache2.start();
client.create().forPath("/test/one", "hey there".getBytes());
Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_ADDED);
Assert.assertEquals(events2.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_ADDED);
client.setData().forPath("/test/one", "sup!".getBytes());
Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_UPDATED);
Assert.assertEquals(events2.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_UPDATED);
Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "sup!");
Assert.assertEquals(new String(cache2.getCurrentData("/test/one").getData()), "sup!");
client.delete().forPath("/test/one");
Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_REMOVED);
Assert.assertEquals(events2.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_REMOVED);
cache.close();
cache2.close();
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPathChildrenCache method testBasics.
@Test
public void testBasics() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
client.create().forPath("/test");
final BlockingQueue<PathChildrenCacheEvent.Type> events = new LinkedBlockingQueue<PathChildrenCacheEvent.Type>();
PathChildrenCache cache = new PathChildrenCache(client, "/test", true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (event.getData().getPath().equals("/test/one")) {
events.offer(event.getType());
}
}
});
cache.start();
client.create().forPath("/test/one", "hey there".getBytes());
Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_ADDED);
client.setData().forPath("/test/one", "sup!".getBytes());
Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_UPDATED);
Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "sup!");
client.delete().forPath("/test/one");
Assert.assertEquals(events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS), PathChildrenCacheEvent.Type.CHILD_REMOVED);
cache.close();
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPathChildrenCache method testAsyncInitialPopulation.
@Test
public void testAsyncInitialPopulation() throws Exception {
Timing timing = new Timing();
PathChildrenCache cache = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
client.create().forPath("/test");
client.create().forPath("/test/one", "hey there".getBytes());
final BlockingQueue<PathChildrenCacheEvent> events = new LinkedBlockingQueue<PathChildrenCacheEvent>();
cache = new PathChildrenCache(client, "/test", true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
events.offer(event);
}
});
cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
PathChildrenCacheEvent event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.CHILD_ADDED);
event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.INITIALIZED);
Assert.assertEquals(event.getInitialData().size(), 1);
} finally {
CloseableUtils.closeQuietly(cache);
CloseableUtils.closeQuietly(client);
}
}
Aggregations