Package org.apache.helix

Source Code of org.apache.helix.TestZkClientWrapper

package org.apache.helix;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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.
*/

import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkConnection;
import org.apache.helix.ZNRecord;
import org.apache.helix.manager.zk.ZNRecordSerializer;
import org.apache.helix.manager.zk.ZkClient;
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;


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();
    }
  }
}
TOP

Related Classes of org.apache.helix.TestZkClientWrapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.