summaryrefslogtreecommitdiff
path: root/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootTest.java
blob: 0b156156d0d64ea6799618a3c1e531c85641d086 (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
/*
 * 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.junit.jupiter.api.Test;

public class ChrootTest extends ClientBase {

    private static class MyWatcher implements Watcher {

        private final String path;
        private String eventPath;
        private CountDownLatch latch = new CountDownLatch(1);

        public MyWatcher(String path) {
            this.path = path;
        }
        public void process(WatchedEvent event) {
            System.out.println("latch:" + path + " " + event.getPath());
            this.eventPath = event.getPath();
            latch.countDown();
        }
        public boolean matches() throws InterruptedException {
            if (!latch.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
                fail("No watch received within timeout period " + path);
            }
            return path.equals(eventPath);
        }

    }

    @Test
    public void testChrootSynchronous() throws IOException, InterruptedException, KeeperException {
        ZooKeeper zk1 = createClient();
        try {
            zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } finally {
            if (zk1 != null) {
                zk1.close();
            }
        }
        ZooKeeper zk2 = createClient(hostPort + "/ch1");
        try {
            assertEquals("/ch2", zk2.create("/ch2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
        } finally {
            if (zk2 != null) {
                zk2.close();
            }
        }

        zk1 = createClient();
        zk2 = createClient(hostPort + "/ch1");
        try {
            // check get
            MyWatcher w1 = new MyWatcher("/ch1");
            assertNotNull(zk1.exists("/ch1", w1));
            MyWatcher w2 = new MyWatcher("/ch1/ch2");
            assertNotNull(zk1.exists("/ch1/ch2", w2));

            MyWatcher w3 = new MyWatcher("/ch2");
            assertNotNull(zk2.exists("/ch2", w3));

            // set watches on child
            MyWatcher w4 = new MyWatcher("/ch1");
            zk1.getChildren("/ch1", w4);
            MyWatcher w5 = new MyWatcher("/");
            zk2.getChildren("/", w5);

            // check set
            zk1.setData("/ch1", "1".getBytes(), -1);
            zk2.setData("/ch2", "2".getBytes(), -1);

            // check watches
            assertTrue(w1.matches());
            assertTrue(w2.matches());
            assertTrue(w3.matches());

            // check exceptions
            try {
                zk2.setData("/ch3", "3".getBytes(), -1);
            } catch (KeeperException.NoNodeException e) {
                assertEquals("/ch3", e.getPath());
            }

            assertTrue(Arrays.equals("1".getBytes(), zk1.getData("/ch1", false, null)));
            assertTrue(Arrays.equals("2".getBytes(), zk1.getData("/ch1/ch2", false, null)));
            assertTrue(Arrays.equals("2".getBytes(), zk2.getData("/ch2", false, null)));

            // check delete
            zk2.delete("/ch2", -1);
            assertTrue(w4.matches());
            assertTrue(w5.matches());

            zk1.delete("/ch1", -1);
            assertNull(zk1.exists("/ch1", false));
            assertNull(zk1.exists("/ch1/ch2", false));
            assertNull(zk2.exists("/ch2", false));
        } finally {
            if (zk1 != null) {
                zk1.close();
            }
            if (zk2 != null) {
                zk2.close();
            }
        }
    }

}