use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFrameworkBackground method testRetries.
@Test
public void testRetries() throws Exception {
final int SLEEP = 1000;
final int TIMES = 5;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(TIMES, SLEEP));
try {
client.start();
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
final CountDownLatch latch = new CountDownLatch(TIMES);
final List<Long> times = Lists.newArrayList();
final AtomicLong start = new AtomicLong(System.currentTimeMillis());
((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() {
@Override
public void listen(OperationAndData<?> data) {
if (data.getOperation().getClass().getName().contains("CreateBuilderImpl")) {
long now = System.currentTimeMillis();
times.add(now - start.get());
start.set(now);
latch.countDown();
}
}
};
server.stop();
client.create().inBackground().forPath("/one");
latch.await();
for (// first one isn't a retry
long elapsed : // first one isn't a retry
times.subList(1, times.size())) {
Assert.assertTrue(elapsed >= SLEEP, elapsed + ": " + times);
}
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFrameworkBackground method testBasic.
@Test
public void testBasic() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
final CountDownLatch latch = new CountDownLatch(3);
final List<String> paths = Lists.newArrayList();
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
paths.add(event.getPath());
latch.countDown();
}
};
client.create().inBackground(callback).forPath("/one");
client.create().inBackground(callback).forPath("/one/two");
client.create().inBackground(callback).forPath("/one/two/three");
latch.await();
Assert.assertEquals(paths, Arrays.asList("/one", "/one/two", "/one/two/three"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFrameworkBackground method testErrorListener.
@Test
public void testErrorListener() throws Exception {
ACLProvider badAclProvider = new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
throw new UnsupportedOperationException();
}
@Override
public List<ACL> getAclForPath(String path) {
throw new UnsupportedOperationException();
}
};
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).aclProvider(badAclProvider).build();
try {
client.start();
final CountDownLatch errorLatch = new CountDownLatch(1);
UnhandledErrorListener listener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof UnsupportedOperationException) {
errorLatch.countDown();
}
}
};
client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
Assert.assertTrue(new Timing().awaitLatch(errorLatch));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFrameworkBackground method testCuratorCallbackOnError.
/**
* Attempt a background operation while Zookeeper server is down.
* Return code must be {@link Code#CONNECTIONLOSS}
*/
@Test
public void testCuratorCallbackOnError() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1000)).build();
final CountDownLatch latch = new CountDownLatch(1);
try {
client.start();
BackgroundCallback curatorCallback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getResultCode() == Code.CONNECTIONLOSS.intValue()) {
latch.countDown();
}
}
};
// Stop the Zookeeper server
server.stop();
// Attempt to retrieve children list
client.getChildren().inBackground(curatorCallback).forPath("/");
// Check if the callback has been called with a correct return code
Assert.assertTrue(timing.awaitLatch(latch), "Callback has not been called by curator !");
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestWatcherIdentity method testRefExpiration.
@Test
public void testRefExpiration() throws Exception {
final int MAX_CHECKS = 10;
final CuratorFrameworkImpl client = (CuratorFrameworkImpl) CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
try {
Assert.assertNull(client.getNamespaceWatcherMap().get(new CountCuratorWatcher()));
final CountDownLatch latch = new CountDownLatch(1);
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
CountZKWatcher watcher = new CountZKWatcher();
client.getNamespaceWatcherMap().getNamespaceWatcher(watcher);
Assert.assertNotNull(client.getNamespaceWatcherMap().get(watcher));
latch.countDown();
return null;
}
});
latch.await();
service.shutdownNow();
Timing timing = new Timing();
for (int i = 0; i < MAX_CHECKS; ++i) {
Assert.assertTrue((i + 1) < MAX_CHECKS);
timing.sleepABit();
// just to cause drainReferenceQueues() to get called
client.getNamespaceWatcherMap().drain();
if (client.getNamespaceWatcherMap().isEmpty()) {
break;
}
}
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations