forked from yaojingguo/geekbang-zk-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCuratorTests.java
191 lines (163 loc) · 5.9 KB
/
CuratorTests.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
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
package org.yao;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import static com.google.common.truth.Truth.assertThat;
/**
* Example code to demonstrate the usage of Curator client and framework.
*/
public class CuratorTests {
private CuratorFramework client;
private String connectString = "localhost:2181";
private RetryPolicy retryPolicy;
@Before
public void setUp() {
retryPolicy = new ExponentialBackoffRetry(1000, 3);
client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
/*
// Fluent style
client =
CuratorFrameworkFactory.builder()
.connectString(connectString)
.retryPolicy(retryPolicy)
.build();
*/
// Start client
client.start();
}
@After
public void tearDown() {
client.close();
}
// create -> getData -> delete in synchronous mode
@Test
public void testSyncOp() throws Exception {
String path = "/one";
byte[] data = {'1'};
client.create().withMode(CreateMode.PERSISTENT).forPath(path, data);
byte[] actualData = client.getData().forPath(path);
assertThat(data).isEqualTo(actualData);
client.delete().forPath(path);
client.close();
}
// create -> getData -> delete in asynchronous mode
@Test
public void testAsyncOp() throws Exception {
String path = "/two";
final byte[] data = {'2'};
final CountDownLatch latch = new CountDownLatch(1);
// Use listener only for callbacks
client
.getCuratorListenable()
.addListener(
(CuratorFramework c, CuratorEvent event) -> {
switch (event.getType()) {
case CREATE:
System.out.printf("znode '%s' created\n", event.getPath());
// 2. getData
c.getData().inBackground().forPath(event.getPath());
break;
case GET_DATA:
System.out.printf("got the data of znode '%s'\n", event.getPath());
assertThat(event.getData()).isEqualTo(data);
// 3. Delete
c.delete().inBackground().forPath(path);
break;
case DELETE:
System.out.printf("znode '%s' deleted\n", event.getPath());
latch.countDown();
break;
}
});
// 1. create
client.create().withMode(CreateMode.PERSISTENT).inBackground().forPath(path, data);
latch.await();
client.close();
}
@Test
public void testWatch() throws Exception {
String path = "/three";
byte[] data = {'3'};
byte[] newData = {'4'};
CountDownLatch latch = new CountDownLatch(1);
// Use listener only for watches
client
.getCuratorListenable()
.addListener(
(CuratorFramework c, CuratorEvent event) -> {
switch (event.getType()) {
case WATCHED:
WatchedEvent we = event.getWatchedEvent();
System.out.println("watched event: " + we);
if (we.getType() == Watcher.Event.EventType.NodeDataChanged
&& we.getPath().equals(path)) {
// 4. watch triggered
System.out.printf("got the event for the triggered watch\n");
byte[] actualData = c.getData().forPath(path);
assertThat(actualData).isEqualTo(newData);
}
latch.countDown();
break;
}
});
// 1. create
client.create().withMode(CreateMode.PERSISTENT).forPath(path, data);
// 2. getData and register a watch
byte[] actualData = client.getData().watched().forPath(path);
assertThat(actualData).isEqualTo(data);
// 3. setData
client.setData().forPath(path, newData);
latch.await();
// 5. delete
client.delete().forPath(path);
}
@Test
public void testCallbackAndWatch() throws Exception {
String path = "/four";
byte[] data = {'4'};
byte[] newData = {'5'};
CountDownLatch latch = new CountDownLatch(2);
// Use listener for both callbacks and watches
client
.getCuratorListenable()
.addListener(
(CuratorFramework c, CuratorEvent event) -> {
switch (event.getType()) {
case CREATE:
// 2. callback for create
System.out.printf("znode '%s' created\n", event.getPath());
// 3. getData and register a watch
assertThat(client.getData().watched().forPath(path)).isEqualTo(data);
// 4. setData
client.setData().forPath(path, newData);
latch.countDown();
break;
case WATCHED:
WatchedEvent we = event.getWatchedEvent();
System.out.println("watched event: " + we);
if (we.getType() == Watcher.Event.EventType.NodeDataChanged
&& we.getPath().equals(path)) {
// 5. watch triggered
System.out.printf("got the event for the triggered watch\n");
assertThat(c.getData().forPath(path)).isEqualTo(newData);
}
latch.countDown();
break;
}
});
// 1. create
client.create().withMode(CreateMode.PERSISTENT).inBackground().forPath(path, data);
latch.await();
// 6. delete
client.delete().forPath(path);
}
}