/**
* Copyright (C) 2012 LinkedIn Inc <opensource@linkedin.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.linkedin.helix;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkConnection;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.linkedin.helix.manager.zk.ZNRecordSerializer;
import com.linkedin.helix.manager.zk.ZkClient;
public class TestZkClientWrapper extends ZkUnitTestBase
{
ZkClient _zkClient;
@BeforeClass
public void beforeClass()
{
_zkClient = new ZkClient(ZK_ADDR);
_zkClient.setZkSerializer(new ZNRecordSerializer());
}
@AfterClass
public void afterClass()
{
_zkClient.close();
}
@Test ()
void testGetStat()
{
String path = "/tmp/getStatTest";
_zkClient.deleteRecursive(path);
Stat stat, newStat;
stat = _zkClient.getStat(path);
AssertJUnit.assertNull(stat);
_zkClient.createPersistent(path, true);
stat = _zkClient.getStat(path);
AssertJUnit.assertNotNull(stat);
newStat = _zkClient.getStat(path);
AssertJUnit.assertEquals(stat, newStat);
_zkClient.writeData(path, new ZNRecord("Test"));
newStat = _zkClient.getStat(path);
AssertJUnit.assertNotSame(stat, newStat);
}
@Test ()
void testSessioExpire()
{
IZkStateListener listener = new IZkStateListener()
{
@Override
public void handleStateChanged(KeeperState state) throws Exception
{
System.out.println("In Old connection New state " + state);
}
@Override
public void handleNewSession() throws Exception
{
System.out.println("In Old connection New session");
}
};
_zkClient.subscribeStateChanges(listener);
ZkConnection connection = ((ZkConnection) _zkClient.getConnection());
ZooKeeper zookeeper = connection.getZookeeper();
System.out.println("old sessionId= " + zookeeper.getSessionId());
try
{
Watcher watcher = new Watcher(){
@Override
public void process(WatchedEvent event)
{
System.out.println("In New connection In process event:"+ event);
}
};
ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(),
zookeeper.getSessionTimeout(), watcher , zookeeper.getSessionId(),
zookeeper.getSessionPasswd());
Thread.sleep(3000);
System.out.println("New sessionId= " + newZookeeper.getSessionId());
Thread.sleep(3000);
newZookeeper.close();
Thread.sleep(10000);
connection = ((ZkConnection) _zkClient.getConnection());
zookeeper = connection.getZookeeper();
System.out.println("After session expiry sessionId= " + zookeeper.getSessionId());
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}