use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestFrameworkBackground method testListenerConnectedAtStart.
@Test
public void testListenerConnectedAtStart() throws Exception {
server.stop();
Timing timing = new Timing(2);
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(0, 0));
try {
client.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final AtomicBoolean firstListenerAction = new AtomicBoolean(true);
final AtomicReference<ConnectionState> firstListenerState = new AtomicReference<ConnectionState>();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (firstListenerAction.compareAndSet(true, false)) {
firstListenerState.set(newState);
System.out.println("First listener state is " + newState);
}
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
// due to CURATOR-72, this was causing a LOST event to precede the CONNECTED event
client.create().inBackground().forPath("/foo");
server.restart();
Assert.assertTrue(timing.awaitLatch(connectedLatch));
Assert.assertFalse(firstListenerAction.get());
ConnectionState firstconnectionState = firstListenerState.get();
Assert.assertEquals(firstconnectionState, ConnectionState.CONNECTED, "First listener state MUST BE CONNECTED but is " + firstconnectionState);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestFrameworkEdges method testBackgroundFailure.
@Test
public void testBackgroundFailure() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
final CountDownLatch latch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
latch.countDown();
}
}
});
client.checkExists().forPath("/hey");
client.checkExists().inBackground().forPath("/hey");
server.stop();
client.checkExists().inBackground().forPath("/hey");
Assert.assertTrue(timing.awaitLatch(latch));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestReadOnly method testReadOnly.
@Test
public void testReadOnly() throws Exception {
Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = new TestingCluster(2);
try {
cluster.start();
client = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).canBeReadOnly(true).connectionTimeoutMs(timing.connection()).sessionTimeoutMs(timing.session()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
client.start();
client.create().forPath("/test");
final CountDownLatch readOnlyLatch = new CountDownLatch(1);
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.READ_ONLY) {
readOnlyLatch.countDown();
} else if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
InstanceSpec ourInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
InstanceSpec killInstance = iterator.next();
if (killInstance.equals(ourInstance)) {
// kill the instance we're not connected to
killInstance = iterator.next();
}
cluster.killServer(killInstance);
Assert.assertEquals(reconnectedLatch.getCount(), 1);
Assert.assertTrue(timing.awaitLatch(readOnlyLatch));
Assert.assertEquals(reconnectedLatch.getCount(), 1);
cluster.restartServer(killInstance);
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestReadOnly method testConnectionStateNewClient.
@Test
public void testConnectionStateNewClient() throws Exception {
Timing timing = new Timing();
TestingCluster cluster = new TestingCluster(3);
CuratorFramework client = null;
try {
cluster.start();
client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(100));
client.start();
client.checkExists().forPath("/");
client.close();
client = null;
Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
for (int i = 0; i < 2; ++i) {
cluster.killServer(iterator.next());
}
client = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryNTimes(3, timing.milliseconds())).canBeReadOnly(true).build();
final BlockingQueue<ConnectionState> states = Queues.newLinkedBlockingQueue();
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
states.add(newState);
}
});
client.start();
client.checkExists().forPath("/");
ConnectionState state = states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
Assert.assertEquals(state, ConnectionState.READ_ONLY);
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener 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