View Javadoc

1   package org.apache.helix;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.I0Itec.zkclient.IZkStateListener;
23  import org.I0Itec.zkclient.ZkConnection;
24  import org.apache.helix.ZNRecord;
25  import org.apache.helix.manager.zk.ZNRecordSerializer;
26  import org.apache.helix.manager.zk.ZkClient;
27  import org.apache.zookeeper.WatchedEvent;
28  import org.apache.zookeeper.Watcher;
29  import org.apache.zookeeper.Watcher.Event.KeeperState;
30  import org.apache.zookeeper.ZooKeeper;
31  import org.apache.zookeeper.data.Stat;
32  import org.testng.AssertJUnit;
33  import org.testng.annotations.AfterClass;
34  import org.testng.annotations.BeforeClass;
35  import org.testng.annotations.Test;
36  
37  
38  public class TestZkClientWrapper extends ZkUnitTestBase
39  {
40  	ZkClient _zkClient;
41  
42  	@BeforeClass
43  	public void beforeClass()
44  	{
45  		_zkClient = new ZkClient(ZK_ADDR);
46  		_zkClient.setZkSerializer(new ZNRecordSerializer());
47  	}
48  
49  	@AfterClass
50  	public void afterClass()
51  	{
52  		_zkClient.close();
53  	}
54  
55  	@Test ()
56  	void testGetStat()
57  	{
58  		String path = "/tmp/getStatTest";
59  		_zkClient.deleteRecursive(path);
60  
61  		Stat stat, newStat;
62  		stat = _zkClient.getStat(path);
63  		AssertJUnit.assertNull(stat);
64  		_zkClient.createPersistent(path, true);
65  
66  		stat = _zkClient.getStat(path);
67  		AssertJUnit.assertNotNull(stat);
68  
69  		newStat = _zkClient.getStat(path);
70  		AssertJUnit.assertEquals(stat, newStat);
71  
72  		_zkClient.writeData(path, new ZNRecord("Test"));
73  		newStat = _zkClient.getStat(path);
74  		AssertJUnit.assertNotSame(stat, newStat);
75  	}
76  
77    @Test ()
78  	void testSessioExpire()
79  	{
80  		IZkStateListener listener = new IZkStateListener()
81  		{
82  
83  			@Override
84  			public void handleStateChanged(KeeperState state) throws Exception
85  			{
86  				System.out.println("In Old connection New state " + state);
87  			}
88  
89  			@Override
90  			public void handleNewSession() throws Exception
91  			{
92  				System.out.println("In Old connection New session");
93  			}
94  		};
95  		_zkClient.subscribeStateChanges(listener);
96  		ZkConnection connection = ((ZkConnection) _zkClient.getConnection());
97  		ZooKeeper zookeeper = connection.getZookeeper();
98  		System.out.println("old sessionId= " + zookeeper.getSessionId());
99  		try
100 		{
101 			Watcher watcher = new Watcher(){
102 				@Override
103         public void process(WatchedEvent event)
104         {
105 					System.out.println("In New connection In process event:"+ event);
106         }
107 			};
108 			ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(),
109 			    zookeeper.getSessionTimeout(), watcher , zookeeper.getSessionId(),
110 			    zookeeper.getSessionPasswd());
111 			Thread.sleep(3000);
112 			System.out.println("New sessionId= " + newZookeeper.getSessionId());
113 			Thread.sleep(3000);
114 			newZookeeper.close();
115 			Thread.sleep(10000);
116 			connection = ((ZkConnection) _zkClient.getConnection());
117 			zookeeper = connection.getZookeeper();
118 			System.out.println("After session expiry sessionId= " + zookeeper.getSessionId());
119 		} catch (Exception e)
120 		{
121 			// TODO Auto-generated catch block
122 			e.printStackTrace();
123 		}
124 	}
125 }