Search in sources :

Example 1 with ZkClientx

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("/");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) ZkClientx(com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx) ClientCnxn(org.apache.zookeeper.ClientCnxn) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) ZooKeeperx(com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.arbitrate.BaseOtterTest)

Aggregations

BaseOtterTest (com.alibaba.otter.shared.arbitrate.BaseOtterTest)1 ZkClientx (com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx)1 ZooKeeperx (com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ClientCnxn (org.apache.zookeeper.ClientCnxn)1 KeeperException (org.apache.zookeeper.KeeperException)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Watcher (org.apache.zookeeper.Watcher)1 ZooKeeper (org.apache.zookeeper.ZooKeeper)1 HostProvider (org.apache.zookeeper.client.HostProvider)1 StaticHostProvider (org.apache.zookeeper.client.StaticHostProvider)1 Stat (org.apache.zookeeper.data.Stat)1 Test (org.testng.annotations.Test)1