This project has retired. For details please refer to its Attic page.
TestZooKeeperExt xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.zk;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.List;
25  
26  import org.apache.giraph.zk.ZooKeeperExt;
27  import org.apache.zookeeper.CreateMode;
28  import org.apache.zookeeper.KeeperException;
29  import org.apache.zookeeper.WatchedEvent;
30  import org.apache.zookeeper.Watcher;
31  import org.apache.zookeeper.ZooDefs.Ids;
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  /**
37   * Test the ZooKeeperExt class.
38   */
39  public class TestZooKeeperExt implements Watcher {
40    /** ZooKeeperExt instance */
41    private ZooKeeperExt zooKeeperExt = null;
42    /** ZooKeeper server list */
43    private String zkList = System.getProperty("prop.zookeeper.list");
44  
45    public static final String BASE_PATH = "/_zooKeeperExtTest";
46    public static final String FIRST_PATH = "/_first";
47  
48    public void process(WatchedEvent event) {
49      return;
50    }
51  
52    @Before
53    public void setUp() {
54      try {
55        if (zkList == null) {
56          return;
57        }
58        zooKeeperExt =
59            new ZooKeeperExt(zkList, 30 * 1000, 0, 0, this);
60        zooKeeperExt.deleteExt(BASE_PATH, -1, true);
61      } catch (KeeperException.NoNodeException e) {
62        System.out.println("Clean start: No node " + BASE_PATH);
63      } catch (Exception e) {
64        throw new RuntimeException(e);
65      }
66    }
67  
68    @After
69    public void tearDown() {
70      if (zooKeeperExt == null) {
71        return;
72      }
73      try {
74        zooKeeperExt.close();
75      } catch (InterruptedException e) {
76        throw new RuntimeException(e);
77      }
78    }
79  
80    @Test
81    public void testCreateExt() throws KeeperException, InterruptedException {
82      if (zooKeeperExt == null) {
83        System.out.println(
84            "testCreateExt: No prop.zookeeper.list set, skipping test");
85        return;
86      }
87      System.out.println("Created: " +
88                             zooKeeperExt.createExt(
89                                 BASE_PATH + FIRST_PATH,
90                                 null,
91                                 Ids.OPEN_ACL_UNSAFE,
92                                 CreateMode.PERSISTENT,
93                                 true));
94      zooKeeperExt.deleteExt(BASE_PATH + FIRST_PATH, -1, false);
95      zooKeeperExt.deleteExt(BASE_PATH, -1, false);
96    }
97  
98    @Test
99    public void testDeleteExt() throws KeeperException, InterruptedException {
100     if (zooKeeperExt == null) {
101       System.out.println(
102           "testDeleteExt: No prop.zookeeper.list set, skipping test");
103       return;
104     }
105     zooKeeperExt.createExt(BASE_PATH,
106                            null,
107                            Ids.OPEN_ACL_UNSAFE,
108                            CreateMode.PERSISTENT,
109                            false);
110     zooKeeperExt.createExt(BASE_PATH + FIRST_PATH,
111                            null,
112                            Ids.OPEN_ACL_UNSAFE,
113                            CreateMode.PERSISTENT,
114                            false);
115     try {
116       zooKeeperExt.deleteExt(BASE_PATH, -1, false);
117     } catch (KeeperException.NotEmptyException e) {
118       System.out.println(
119           "Correctly failed to delete since not recursive");
120     }
121     zooKeeperExt.deleteExt(BASE_PATH, -1, true);
122   }
123 
124   @Test
125   public void testGetChildrenExt()
126       throws KeeperException, InterruptedException {
127     if (zooKeeperExt == null) {
128       System.out.println(
129           "testGetChildrenExt: No prop.zookeeper.list set, skipping test");
130       return;
131     }
132     zooKeeperExt.createExt(BASE_PATH,
133                            null,
134                            Ids.OPEN_ACL_UNSAFE,
135                            CreateMode.PERSISTENT,
136                            false);
137     zooKeeperExt.createExt(BASE_PATH + "/b",
138                            null,
139                            Ids.OPEN_ACL_UNSAFE,
140                            CreateMode.PERSISTENT_SEQUENTIAL,
141                            false);
142     zooKeeperExt.createExt(BASE_PATH + "/a",
143                            null,
144                            Ids.OPEN_ACL_UNSAFE,
145                            CreateMode.PERSISTENT_SEQUENTIAL,
146                            false);
147     zooKeeperExt.createExt(BASE_PATH + "/d",
148                            null,
149                            Ids.OPEN_ACL_UNSAFE,
150                            CreateMode.PERSISTENT_SEQUENTIAL,
151                            false);
152     zooKeeperExt.createExt(BASE_PATH + "/c",
153                            null,
154                            Ids.OPEN_ACL_UNSAFE,
155                            CreateMode.PERSISTENT_SEQUENTIAL,
156                            false);
157     List<String> fullPathList =
158         zooKeeperExt.getChildrenExt(BASE_PATH, false, false, true);
159     for (String fullPath : fullPathList) {
160       assertTrue(fullPath.contains(BASE_PATH + "/"));
161     }
162     List<String> sequenceOrderedList =
163         zooKeeperExt.getChildrenExt(BASE_PATH, false, true, true);
164     for (String fullPath : sequenceOrderedList) {
165       assertTrue(fullPath.contains(BASE_PATH + "/"));
166     }
167     assertEquals(4, sequenceOrderedList.size());
168     assertTrue(sequenceOrderedList.get(0).contains("/b"));
169     assertTrue(sequenceOrderedList.get(1).contains("/a"));
170     assertTrue(sequenceOrderedList.get(2).contains("/d"));
171     assertTrue(sequenceOrderedList.get(3).contains("/c"));
172   }
173 }