use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPersistentEphemeralNodeListener method testListenersReconnectedIsOK.
@Test
public void testListenersReconnectedIsOK() throws Exception {
server.stop();
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
PersistentEphemeralNode node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes());
node.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
final AtomicReference<ConnectionState> lastState = new AtomicReference<ConnectionState>();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
lastState.set(newState);
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(connectedLatch));
timing.sleepABit();
Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
server.restart();
timing.sleepABit();
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
timing.sleepABit();
Assert.assertEquals(lastState.get(), ConnectionState.RECONNECTED);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPersistentNode method testQuickCloseNodeExists.
@Test
public void testQuickCloseNodeExists() throws Exception {
Timing timing = new Timing();
PersistentNode pen = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
client.create().creatingParentsIfNeeded().forPath("/test/one/two");
pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test/one/two", new byte[0]);
pen.start();
pen.close();
timing.sleepABit();
Assert.assertNull(client.checkExists().forPath("/test/one/two"));
} finally {
CloseableUtils.closeQuietly(pen);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPersistentNode method testBasic.
@Test
public void testBasic() throws Exception {
final byte[] TEST_DATA = "hey".getBytes();
Timing timing = new Timing();
PersistentNode pen = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test", TEST_DATA);
pen.start();
Assert.assertTrue(pen.waitForInitialCreate(timing.milliseconds(), TimeUnit.MILLISECONDS));
// cause session to end - force checks that node is persistent
client.close();
client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
byte[] bytes = client.getData().forPath("/test");
Assert.assertEquals(bytes, TEST_DATA);
} finally {
CloseableUtils.closeQuietly(pen);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class BasicTests method testExpiredSession.
@Test
public void testExpiredSession() throws Exception {
// see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4
final Timing timing = new Timing();
final CountDownLatch latch = new CountDownLatch(1);
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.Expired) {
latch.countDown();
}
}
};
final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), watcher, new RetryOneTime(2));
client.start();
try {
final AtomicBoolean firstTime = new AtomicBoolean(true);
RetryLoop.callWithRetry(client, new Callable<Object>() {
@Override
public Object call() throws Exception {
if (firstTime.compareAndSet(true, false)) {
try {
client.getZooKeeper().create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException.NodeExistsException ignore) {
// ignore
}
KillSession.kill(client.getZooKeeper(), server.getConnectString());
Assert.assertTrue(timing.awaitLatch(latch));
}
ZooKeeper zooKeeper = client.getZooKeeper();
client.blockUntilConnectedOrTimedOut();
Assert.assertNotNull(zooKeeper.exists("/foo", false));
return null;
}
});
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestSessionFailRetryLoop method testRetryStatic.
@Test
public void testRetryStatic() throws Exception {
Timing timing = new Timing();
final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
retryLoop.start();
try {
client.start();
final AtomicBoolean secondWasDone = new AtomicBoolean(false);
final AtomicBoolean firstTime = new AtomicBoolean(true);
SessionFailRetryLoop.callWithRetry(client, SessionFailRetryLoop.Mode.RETRY, new Callable<Object>() {
@Override
public Object call() throws Exception {
RetryLoop.callWithRetry(client, new Callable<Void>() {
@Override
public Void call() throws Exception {
if (firstTime.compareAndSet(true, false)) {
Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
KillSession.kill(client.getZooKeeper(), server.getConnectString());
client.getZooKeeper();
client.blockUntilConnectedOrTimedOut();
}
Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
return null;
}
});
RetryLoop.callWithRetry(client, new Callable<Void>() {
@Override
public Void call() throws Exception {
Assert.assertFalse(firstTime.get());
Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
secondWasDone.set(true);
return null;
}
});
return null;
}
});
Assert.assertTrue(secondWasDone.get());
} finally {
retryLoop.close();
CloseableUtils.closeQuietly(client);
}
}
Aggregations