summaryrefslogtreecommitdiff
path: root/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperTestClient.java
blob: c4ef9e23dad4fb86f0251824186e2daf178b38c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * 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.
 */

package org.apache.zookeeper.test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.common.Time;
import org.apache.zookeeper.data.Stat;

public class ZooKeeperTestClient extends ZKTestCase implements Watcher {

    protected String hostPort = "127.0.0.1:22801";

    protected static final String dirOnZK = "/test_dir";

    protected String testDirOnZK = dirOnZK + "/" + Time.currentElapsedTime();

    LinkedBlockingQueue<WatchedEvent> events = new LinkedBlockingQueue<WatchedEvent>();

    private WatchedEvent getEvent(int numTries) throws InterruptedException {
        WatchedEvent event = null;
        for (int i = 0; i < numTries; i++) {
            System.out.println("i = " + i);
            event = events.poll(10, TimeUnit.SECONDS);
            if (event != null) {
                break;
            }
            Thread.sleep(5000);
        }
        return event;

    }

    private void deleteZKDir(ZooKeeper zk, String nodeName) throws IOException, InterruptedException, KeeperException {

        Stat stat = zk.exists(nodeName, false);
        if (stat == null) {
            return;
        }

        List<String> children1 = zk.getChildren(nodeName, false);
        List<String> c2 = zk.getChildren(nodeName, false, stat);

        if (!children1.equals(c2)) {
            fail("children lists from getChildren()/getChildren2() do not match");
        }

        if (!stat.equals(stat)) {
            fail("stats from exists()/getChildren2() do not match");
        }

        if (children1.size() == 0) {
            zk.delete(nodeName, -1);
            return;
        }
        for (String n : children1) {
            deleteZKDir(zk, n);
        }
    }

    private void checkRoot() throws IOException, InterruptedException {
        ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);

        try {
            zk.create(dirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ke) {
            // expected, sort of
        } catch (KeeperException ke) {
            fail("Unexpected exception code for create " + dirOnZK + ": " + ke.getMessage());
        }

        try {
            zk.create(testDirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ke) {
            // expected, sort of
        } catch (KeeperException ke) {
            fail("Unexpected exception code for create " + testDirOnZK + ": " + ke.getMessage());
        }

        zk.close();
    }

    private void enode_test_1() throws IOException, InterruptedException, KeeperException {
        checkRoot();
        String parentName = testDirOnZK;
        String nodeName = parentName + "/enode_abc";
        ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);

        Stat stat = zk.exists(parentName, false);
        if (stat == null) {
            try {
                zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException ke) {
                fail("Creating node " + parentName + ke.getMessage());
            }
        }

        try {
            zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NODEEXISTS;
            if (!valid) {
                fail("Unexpected exception code for createin: " + ke.getMessage());
            }
        }

        stat = zk.exists(nodeName, false);
        if (stat == null) {
            fail("node " + nodeName + " should exist");
        }
        System.out.println("Closing client with sessionid: 0x" + Long.toHexString(zk.getSessionId()));
        zk.close();
        zk = new ZooKeeper(hostPort, 10000, this);

        for (int i = 0; i < 10; i++) {
            System.out.println("i = " + i);
            stat = zk.exists(nodeName, false);
            if (stat != null) {
                System.out.println("node " + nodeName + " should not exist after reconnection close");
            } else {
                System.out.println("node " + nodeName + " is gone after reconnection close!");
                break;
            }
            Thread.sleep(5000);
        }
        deleteZKDir(zk, nodeName);
        zk.close();

    }

    private void enode_test_2() throws IOException, InterruptedException, KeeperException {
        checkRoot();
        String parentName = testDirOnZK;
        String nodeName = parentName + "/enode_abc";
        ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
        ZooKeeper zk_1 = new ZooKeeper(hostPort, 10000, this);

        Stat stat_parent = zk_1.exists(parentName, false);
        if (stat_parent == null) {
            try {
                zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException ke) {
                fail("Creating node " + parentName + ke.getMessage());
            }
        }

        Stat stat_node = zk_1.exists(nodeName, false);
        if (stat_node != null) {

            try {
                zk.delete(nodeName, -1);
            } catch (KeeperException ke) {
                Code code = ke.code();
                boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY;
                if (!valid) {
                    fail("Unexpected exception code for delete: " + ke.getMessage());
                }
            }
        }

        List<String> firstGen1 = zk_1.getChildren(parentName, true);
        Stat stat = new Stat();
        List<String> firstGen2 = zk_1.getChildren(parentName, true, stat);

        if (!firstGen1.equals(firstGen2)) {
            fail("children lists from getChildren()/getChildren2() do not match");
        }

        if (!stat_parent.equals(stat)) {
            fail("stat from exists()/getChildren() do not match");
        }

        try {
            zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NODEEXISTS;
            if (!valid) {
                fail("Unexpected exception code for createin: " + ke.getMessage());
            }
        }

        Thread.sleep(5000);
        WatchedEvent event = events.poll(10, TimeUnit.SECONDS);
        if (event == null) {
            throw new IOException("No event was delivered promptly");
        }
        if (event.getType() != EventType.NodeChildrenChanged || !event.getPath().equalsIgnoreCase(parentName)) {
            fail("Unexpected event was delivered: " + event.toString());
        }

        stat_node = zk_1.exists(nodeName, false);
        if (stat_node == null) {
            fail("node " + nodeName + " should exist");
        }

        try {
            zk.delete(parentName, -1);
            fail("Should be impossible to delete a non-empty node " + parentName);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NOTEMPTY;
            if (!valid) {
                fail("Unexpected exception code for delete: " + code);
            }
        }

        try {
            zk.create(nodeName + "/def", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            fail("Should be impossible to create child off Ephemeral node " + nodeName);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NOCHILDRENFOREPHEMERALS;
            if (!valid) {
                fail("Unexpected exception code for createin: " + code);
            }
        }

        try {
            List<String> children1 = zk.getChildren(nodeName, false);
            List<String> children2 = zk.getChildren(nodeName, false, null);

            if (!children1.equals(children2)) {
                fail("children lists from getChildren()/getChildren2() does not match");
            }

            if (children1.size() > 0) {
                fail("ephemeral node " + nodeName + " should not have children");
            }
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NONODE;
            if (!valid) {
                fail("Unexpected exception code for createin: " + code);
            }
        }
        firstGen1 = zk_1.getChildren(parentName, true);
        firstGen2 = zk_1.getChildren(parentName, true, null);

        if (!firstGen1.equals(firstGen2)) {
            fail("children list from getChildren()/getChildren2() does not match");
        }

        stat_node = zk_1.exists(nodeName, true);
        if (stat_node == null) {
            fail("node " + nodeName + " should exist");
        }
        System.out.println("session id of zk: " + zk.getSessionId());
        System.out.println("session id of zk_1: " + zk_1.getSessionId());
        zk.close();

        zk_1.exists("nosuchnode", false);

        event = this.getEvent(10);
        if (event == null) {
            throw new Error("First event was not delivered promptly");
        }
        if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (
                event.getType() == EventType.NodeDeleted
                        && event.getPath().equalsIgnoreCase(nodeName)))) {
            System.out.print(parentName
                                     + " "
                                     + EventType.NodeChildrenChanged
                                     + " "
                                     + nodeName
                                     + " "
                                     + EventType.NodeDeleted);
            fail("Unexpected first event was delivered: " + event.toString());
        }

        event = this.getEvent(10);

        if (event == null) {
            throw new Error("Second event was not delivered promptly");
        }
        if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || (
                event.getType() == EventType.NodeDeleted
                        && event.getPath().equalsIgnoreCase(nodeName)))) {
            System.out.print(parentName
                                     + " "
                                     + EventType.NodeChildrenChanged
                                     + " "
                                     + nodeName
                                     + " "
                                     + EventType.NodeDeleted);
            fail("Unexpected second event was delivered: " + event.toString());
        }

        firstGen1 = zk_1.getChildren(parentName, false);
        stat_node = zk_1.exists(nodeName, false);
        if (stat_node != null) {
            fail("node " + nodeName + " should have been deleted");
        }
        if (firstGen1.contains(nodeName)) {
            fail("node " + nodeName + " should not be a children");
        }
        deleteZKDir(zk_1, nodeName);
        zk_1.close();
    }

    private void delete_create_get_set_test_1() throws IOException, InterruptedException, KeeperException {
        checkRoot();
        ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
        String parentName = testDirOnZK;
        String nodeName = parentName + "/benwashere";
        try {
            zk.delete(nodeName, -1);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY;
            if (!valid) {
                fail("Unexpected exception code for delete: " + ke.getMessage());
            }
        }
        try {
            zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NODEEXISTS;
            if (!valid) {
                fail("Unexpected exception code for create: " + ke.getMessage());
            }
        }
        try {
            zk.setData(nodeName, "hi".getBytes(), 5700);
            fail("Should have gotten BadVersion exception");
        } catch (KeeperException ke) {
            if (ke.code() != Code.BADVERSION) {
                fail("Should have gotten BadVersion exception");
            }
        }
        zk.setData(nodeName, "hi".getBytes(), -1);
        Stat st = new Stat();
        byte[] bytes = zk.getData(nodeName, false, st);
        String retrieved = new String(bytes);
        if (!"hi".equals(retrieved)) {
            fail("The retrieved data [" + retrieved + "] is differented than the expected [hi]");
        }
        try {
            zk.delete(nodeName, 6800);
            fail("Should have gotten BadVersion exception");
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NOTEMPTY || code == KeeperException.Code.BADVERSION;
            if (!valid) {
                fail("Unexpected exception code for delete: " + ke.getMessage());
            }
        }
        try {
            zk.delete(nodeName, -1);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NOTEMPTY;
            if (!valid) {
                fail("Unexpected exception code for delete: " + code);
            }
        }
        deleteZKDir(zk, nodeName);
        zk.close();
    }

    private void deleteNodeIfExists(ZooKeeper zk, String nodeName) throws InterruptedException {
        try {
            zk.delete(nodeName, -1);
        } catch (KeeperException ke) {
            Code code = ke.code();
            boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY;
            if (!valid) {
                fail("Unexpected exception code for delete: " + ke.getMessage());
            }
        }
    }

    private void create_get_stat_test() throws IOException, InterruptedException, KeeperException {
        checkRoot();
        ZooKeeper zk = new ZooKeeper(hostPort, 10000, this);
        String parentName = testDirOnZK;
        String nodeName = parentName + "/create_with_stat_tmp";
        deleteNodeIfExists(zk, nodeName);
        deleteNodeIfExists(zk, nodeName + "_2");
        Stat stat = new Stat();
        zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat);
        assertNotNull(stat);
        assertTrue(stat.getCzxid() > 0);
        assertTrue(stat.getCtime() > 0);

        Stat stat2 = new Stat();
        zk.create(nodeName + "_2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat2);
        assertNotNull(stat2);
        assertTrue(stat2.getCzxid() > stat.getCzxid());
        assertTrue(stat2.getCtime() > stat.getCtime());

        deleteNodeIfExists(zk, nodeName);
        deleteNodeIfExists(zk, nodeName + "_2");
        zk.close();
    }

    public void my_test_1() throws IOException, InterruptedException, KeeperException {
        enode_test_1();
        enode_test_2();
        delete_create_get_set_test_1();
        create_get_stat_test();
    }

    public synchronized void process(WatchedEvent event) {
        try {
            System.out.println("Got an event " + event.toString());
            events.put(event);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ZooKeeperTestClient zktc = new ZooKeeperTestClient();
        try {
            zktc.my_test_1();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}