View Javadoc

1   package org.apache.helix.util;
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 java.util.Date;
23  
24  import org.I0Itec.zkclient.ZkServer;
25  import org.I0Itec.zkclient.exception.ZkNoNodeException;
26  import org.apache.helix.TestHelper;
27  import org.apache.helix.ZNRecord;
28  import org.apache.helix.manager.zk.ZkClient;
29  import org.apache.helix.util.ZKClientPool;
30  import org.testng.Assert;
31  import org.testng.annotations.Test;
32  
33  
34  public class TestZKClientPool
35  {
36  
37    @Test
38    public void test() throws Exception
39    {
40      String testName = "TestZKClientPool";
41      System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis()));
42  
43      String zkAddr = "localhost:2189";
44      ZkServer zkServer = TestHelper.startZkSever(zkAddr);
45      ZkClient zkClient = ZKClientPool.getZkClient(zkAddr);
46      
47      zkClient.createPersistent("/" + testName, new ZNRecord(testName));
48      ZNRecord record = zkClient.readData("/" + testName);
49      Assert.assertEquals(record.getId(), testName);
50      
51      TestHelper.stopZkServer(zkServer);
52      
53      // restart zk 
54      zkServer = TestHelper.startZkSever(zkAddr);
55      try
56      {
57        zkClient = ZKClientPool.getZkClient(zkAddr);
58        record = zkClient.readData("/" + testName);
59        Assert.fail("should fail on zk no node exception");
60      } catch (ZkNoNodeException e)
61      {
62        // OK
63      } catch (Exception e)
64      {
65        Assert.fail("should not fail on exception other than ZkNoNodeException");
66      }
67      
68      zkClient.createPersistent("/" + testName, new ZNRecord(testName));
69      record = zkClient.readData("/" + testName);
70      Assert.assertEquals(record.getId(), testName);
71      
72      zkClient.close();
73      TestHelper.stopZkServer(zkServer);
74      System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
75    }
76  }