use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFramework method testConnectionState.
@Test
public void testConnectionState() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
final BlockingQueue<ConnectionState> queue = new LinkedBlockingQueue<ConnectionState>();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
queue.add(newState);
}
};
client.getConnectionStateListenable().addListener(listener);
client.start();
Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS), ConnectionState.CONNECTED);
server.stop();
Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(queue.poll(timing.multiple(4).seconds(), TimeUnit.SECONDS), ConnectionState.LOST);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFramework method testCreateParentContainers.
@Test
public void testCreateParentContainers() throws Exception {
if (!checkForContainers()) {
return;
}
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
try {
client.start();
client.create().creatingParentContainersIfNeeded().forPath("/one/two/three", "foo".getBytes());
byte[] data = client.getData().forPath("/one/two/three");
Assert.assertEquals(data, "foo".getBytes());
client.delete().forPath("/one/two/three");
new Timing().sleepABit();
Assert.assertNull(client.checkExists().forPath("/one/two"));
new Timing().sleepABit();
Assert.assertNull(client.checkExists().forPath("/one"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFramework method testCreateACLWithReset.
@Test
public void testCreateACLWithReset() throws Exception {
Timing timing = new Timing();
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework client = builder.connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).authorization("digest", "me:pass".getBytes()).retryPolicy(new RetryOneTime(1)).build();
client.start();
try {
final CountDownLatch lostLatch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
lostLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
ACL acl = new ACL(ZooDefs.Perms.WRITE, ZooDefs.Ids.AUTH_IDS);
List<ACL> aclList = Lists.newArrayList(acl);
client.create().withACL(aclList).forPath("/test", "test".getBytes());
server.stop();
Assert.assertTrue(timing.awaitLatch(lostLatch));
try {
client.checkExists().forPath("/");
Assert.fail("Connection should be down");
} catch (KeeperException.ConnectionLossException e) {
// expected
}
server.restart();
try {
client.setData().forPath("/test", "test".getBytes());
} catch (KeeperException.NoAuthException e) {
Assert.fail("Auth failed");
}
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFramework method testExistsCreatingParentsInBackground.
@Test
public void testExistsCreatingParentsInBackground() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
try {
client.start();
Assert.assertNull(client.checkExists().forPath("/one/two"));
final CountDownLatch latch = new CountDownLatch(1);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
latch.countDown();
}
};
client.checkExists().creatingParentContainersIfNeeded().inBackground(callback).forPath("/one/two/three");
Assert.assertTrue(new Timing().awaitLatch(latch));
Assert.assertNotNull(client.checkExists().forPath("/one/two"));
Assert.assertNull(client.checkExists().forPath("/one/two/three"));
Assert.assertNull(client.checkExists().creatingParentContainersIfNeeded().forPath("/one/two/three"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestBlockUntilConnected method testBlockUntilConnectedCurrentlyAwaitingReconnect.
/**
* Test the case where we are not currently connected, but have been previously
*/
@Test
public void testBlockUntilConnectedCurrentlyAwaitingReconnect() {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
final CountDownLatch lostLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
lostLatch.countDown();
}
}
});
try {
client.start();
// Block until we're connected
Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to connect");
// Kill the server
CloseableUtils.closeQuietly(server);
// Wait until we hit the lost state
Assert.assertTrue(timing.awaitLatch(lostLatch), "Failed to reach LOST state");
server = new TestingServer(server.getPort(), server.getTempDirectory());
Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Not connected");
} catch (Exception e) {
Assert.fail("Unexpected exception " + e);
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations