-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathZooKeeperTests.java
147 lines (122 loc) · 4.2 KB
/
ZooKeeperTests.java
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
package org.yao;
import com.google.common.collect.ImmutableList;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Op;
import org.apache.zookeeper.OpResult;
import org.apache.zookeeper.Transaction;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import static com.google.common.truth.Truth.assertThat;
public class ZooKeeperTests {
private String pathPrefix = "/multi";
private ZooKeeper zk;
private CountDownLatch startLatch;
private CountDownLatch closeLatch;
private AsyncCallback.MultiCallback callback;
private String path1 = pathPrefix + "1";
private String path2 = pathPrefix + "2";
private byte[] data1 = {0x1};
private byte[] data2 = {0x2};
@Before
public void setUp() throws Exception {
startLatch = new CountDownLatch(1);
callback =
(int rc, String path, Object ctx, List<OpResult> opResults) -> {
assertThat(rc).isEqualTo(KeeperException.Code.OK.intValue());
System.out.printf("delete multi executed");
closeLatch.countDown();
};
zk = new ZooKeeper("localhost", 2181, new DefaultWatcher());
startLatch.await();
}
@After
public void tearDown() throws Exception {
closeLatch.await();
zk.close();
}
@Test
public void testMulti() throws Exception {
closeLatch = new CountDownLatch(1);
// Create two znodes
Op createOp1 = Op.create(path1, data1, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Op createOp2 = Op.create(path2, data2, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// Synchronous API
zk.multi(ImmutableList.of(createOp1, createOp2));
System.out.println("create multi executed");
assertThat(zk.getData(path1, false, null)).isEqualTo(data1);
assertThat(zk.getData(path2, false, null)).isEqualTo(data2);
// Delete two znodes
Op deleteOp1 = Op.delete(path1, -1);
Op deleteOp2 = Op.delete(path2, -1);
// Asynchronous API
zk.multi(ImmutableList.of(deleteOp1, deleteOp2), callback, null);
}
@Test
public void testTransaction() throws Exception {
closeLatch = new CountDownLatch(1);
// Create two znodes
Transaction tx = zk.transaction();
tx.create(path1, data1, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
tx.create(path2, data2, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// Synchronous API
tx.commit();
System.out.println("transaction committed");
assertThat(zk.getData(path1, false, null)).isEqualTo(data1);
assertThat(zk.getData(path2, false, null)).isEqualTo(data2);
// Delete two znodes
tx = zk.transaction();
tx.delete(path1, -1);
tx.delete(path2, -1);
// Asynchronous API
tx.commit(callback, null);
}
@Test
public void testTransactionWithCheck() throws Exception {
closeLatch = new CountDownLatch(0);
{
Transaction tx = zk.transaction();
tx.create(path1, data1, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
tx.create(path2, data2, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
tx.check(path1, 0);
tx.check(path2, 0);
tx.commit();
}
{
Transaction tx = zk.transaction();
tx.check(path1, 0);
tx.check(path2, 0);
tx.delete(path1, 0);
tx.delete(path2, 0);
tx.commit();
}
}
/**
* getChildren does not list descendants recursively.
*/
@Test
public void testGetChilren() throws Exception {
closeLatch = new CountDownLatch(0);
List<String> paths = zk.getChildren("/a", false);
System.out.printf("child paths: %s\n", paths);
}
class DefaultWatcher implements Watcher {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.None
&& event.getState() == Event.KeeperState.SyncConnected) {
System.out.println("zookeeper client connected");
startLatch.countDown();
}
}
}
}