use of com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx in project otter by alibaba.
the class ZooKeeperClientTest method testClient.
@Test
public void testClient() {
ZkClientx zk = ZooKeeperClient.getInstance();
// 强制获取zk中的地址信息
final ZooKeeper zkp = ((ZooKeeperx) zk.getConnection()).getZookeeper();
ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zkp);
HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
want.number(serverAddrs.size()).isEqualTo(3);
String s1 = serverAddrs.get(0).getAddress().getHostAddress() + ":" + serverAddrs.get(0).getPort();
want.string(s1).isEqualTo(cluster1);
Stat stat = new Stat();
try {
zkp.getChildren("/otter/channel/304/388", false, stat);
System.out.println(stat.getCversion());
} catch (KeeperException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// 测试下session timeout
final CountDownLatch latch = new CountDownLatch(1);
new Thread() {
public void run() {
try {
zkp.getChildren("/", false);
} catch (KeeperException e1) {
want.fail();
} catch (InterruptedException e1) {
want.fail();
}
int sessionTimeout = zkp.getSessionTimeout();
long sessionId = zkp.getSessionId();
byte[] passwd = zkp.getSessionPasswd();
try {
ZooKeeper newZk = new ZooKeeper(cluster1, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
// do nothing
}
}, sessionId, passwd);
// 用老的sessionId连接上去,进行一次close操作后,让原先正在使用的出现SESSION_EXPIRED
newZk.close();
} catch (IOException e) {
want.fail();
} catch (InterruptedException e) {
want.fail();
}
latch.countDown();
}
}.start();
try {
latch.await();
} catch (InterruptedException e) {
want.fail();
}
zk.getChildren("/");
}
Aggregations