use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestWatcherIdentity method testCuratorWatcher.
@Test
public void testCuratorWatcher() throws Exception {
Timing timing = new Timing();
CountCuratorWatcher watcher = new CountCuratorWatcher();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
client.create().forPath(PATH);
// Add twice the same watcher on the same path
client.getData().usingWatcher(watcher).forPath(PATH);
client.getData().usingWatcher(watcher).forPath(PATH);
// Ok, let's test it
client.setData().forPath(PATH, new byte[] {});
timing.sleepABit();
Assert.assertEquals(1, watcher.count.get());
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestBackgroundStates method testConnectionStateListener.
@Test
public void testConnectionStateListener() throws Exception {
server.close();
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(timing.milliseconds()));
try {
client.start();
final BlockingQueue<ConnectionState> stateVector = Queues.newLinkedBlockingQueue(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
stateVector.offer(newState);
}
};
Timing waitingTiming = timing.forWaiting();
client.getConnectionStateListenable().addListener(listener);
server = new TestingServer(server.getPort());
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
server.stop();
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
server.restart();
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
server.close();
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(stateVector.poll(waitingTiming.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestBackgroundStates method testListenersReconnectedIsOK.
@Test
public void testListenersReconnectedIsOK() throws Exception {
server.close();
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
PersistentEphemeralNode node = null;
try {
client.start();
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 = new TestingServer(server.getPort());
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);
CloseableUtils.closeQuietly(node);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestResetConnectionWithBackgroundFailure method testConnectionStateListener.
@Test
public void testConnectionStateListener() throws Exception {
server.stop();
LeaderSelector selector = null;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
timing.sleepABit();
LeaderSelectorListener listenerLeader = new LeaderSelectorListenerAdapter() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
Thread.currentThread().join();
}
};
selector = new LeaderSelector(client, "/leader", listenerLeader);
selector.autoRequeue();
selector.start();
final BlockingQueue<ConnectionState> listenerSequence = Queues.newLinkedBlockingQueue();
ConnectionStateListener listener1 = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
listenerSequence.add(newState);
}
};
Timing forWaiting = timing.forWaiting();
client.getConnectionStateListenable().addListener(listener1);
log.debug("Starting ZK server");
server.restart();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
log.debug("Stopping ZK server");
server.stop();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
log.debug("Starting ZK server");
server.restart();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
log.debug("Stopping ZK server");
server.close();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestBlockUntilConnected method testBlockUntilConnectedCurrentlyConnected.
/**
* Test the case where we're already connected
*/
@Test
public void testBlockUntilConnectedCurrentlyConnected() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
try {
final CountDownLatch connectedLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState.isConnected()) {
connectedLatch.countDown();
}
}
});
client.start();
Assert.assertTrue(timing.awaitLatch(connectedLatch), "Timed out awaiting latch");
Assert.assertTrue(client.blockUntilConnected(1, TimeUnit.SECONDS), "Not connected");
} catch (InterruptedException e) {
Assert.fail("Unexpected interruption");
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations