-
Notifications
You must be signed in to change notification settings - Fork 383
/
AtomixClientTest.java
362 lines (295 loc) · 9.02 KB
/
AtomixClientTest.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
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
/*
* Copyright 2015 the original author or authors.
*
* Licensed 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 io.atomix;
import io.atomix.copycat.client.Command;
import io.atomix.copycat.client.CopycatClient;
import io.atomix.copycat.client.Query;
import io.atomix.copycat.server.Commit;
import io.atomix.resource.Consistency;
import io.atomix.resource.Resource;
import io.atomix.resource.ResourceStateMachine;
import io.atomix.resource.ResourceTypeInfo;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* Client server test.
*
* @author <a href="http://github.com/kuujo>Jordan Halterman</a>
*/
@Test
public class AtomixClientTest extends AbstractAtomixTest {
@BeforeMethod
protected void beforeMethod() {
init();
}
@AfterMethod
protected void afterMethod() throws Throwable {
cleanup();
}
/**
* Tests submitting a command.
*/
public void testSubmitCommandWithProcessConsistency() throws Throwable {
testSubmitCommand(Consistency.PROCESS);
}
/**
* Tests submitting a command.
*/
public void testSubmitCommandWithSequentialConsistency() throws Throwable {
testSubmitCommand(Consistency.SEQUENTIAL);
}
/**
* Tests submitting a command.
*/
public void testSubmitCommandWithAtomicConsistency() throws Throwable {
testSubmitCommand(Consistency.ATOMIC);
}
/**
* Tests submitting a command with a configured consistency level.
*/
private void testSubmitCommand(Consistency consistency) throws Throwable {
createReplicas(5, 3, 1);
Atomix client = createClient();
TestResource resource = client.get("test", TestResource.class).get(5, TimeUnit.SECONDS);
resource.with(consistency).command("Hello world!").thenAccept(result -> {
threadAssertEquals(result, "Hello world!");
resume();
});
await(10000);
}
/**
* Tests submitting a query.
*/
public void testSubmitQueryWithProcessConsistency() throws Throwable {
testSubmitQuery(Consistency.PROCESS);
}
/**
* Tests submitting a query.
*/
public void testSubmitQueryWithSequentialConsistency() throws Throwable {
testSubmitQuery(Consistency.SEQUENTIAL);
}
/**
* Tests submitting a query.
*/
public void testSubmitQueryWithAtomicConsistency() throws Throwable {
testSubmitQuery(Consistency.ATOMIC);
}
/**
* Tests submitting a query with a configured consistency level.
*/
private void testSubmitQuery(Consistency consistency) throws Throwable {
createReplicas(5, 3, 1);
Atomix client = createClient();
TestResource resource = client.get("test", TestResource.class).get(5, TimeUnit.SECONDS);
resource.with(consistency).query("Hello world!").thenAccept(result -> {
threadAssertEquals(result, "Hello world!");
resume();
});
await(10000);
}
/**
* Tests getting a resource and submitting commands.
*/
public void testGetConcurrency() throws Throwable {
createReplicas(5, 3, 1);
Atomix client1 = createClient();
Atomix client2 = createClient();
ValueResource resource1 = client1.get("test", ValueResource.class).get(5, TimeUnit.SECONDS);
ValueResource resource2 = client2.get("test", ValueResource.class).get(5, TimeUnit.SECONDS);
resource1.set("Hello world!").join();
resource2.get().thenAccept(result -> {
threadAssertEquals("Hello world!", result);
resume();
});
await(10000);
}
/**
* Tests creating a resource and submitting commands.
*/
public void testCreateConcurrency() throws Throwable {
createReplicas(5, 3, 1);
Atomix client1 = createClient();
Atomix client2 = createClient();
ValueResource resource1 = client1.get("test", ValueResource.class).get(5, TimeUnit.SECONDS);
ValueResource resource2 = client2.get("test", ValueResource.class).get(5, TimeUnit.SECONDS);
resource1.set("Hello world!").join();
resource2.get().thenAccept(result -> {
threadAssertEquals("Hello world!", result);
resume();
});
await(10000);
}
/**
* Tests getting and creating a resource and submitting commands.
*/
public void testGetCreateConcurrency() throws Throwable {
createReplicas(5, 3, 1);
Atomix client1 = createClient();
Atomix client2 = createClient();
ValueResource resource1 = client1.get("test", ValueResource.class).get(5, TimeUnit.SECONDS);
ValueResource resource2 = client2.get("test", ValueResource.class).get(5, TimeUnit.SECONDS);
resource1.set("Hello world!").join();
resource2.get().thenAccept(result -> {
threadAssertEquals("Hello world!", result);
resume();
});
await(10000);
}
/**
* Tests getting resource keys.
*/
public void testGetResourceKeys() throws Throwable {
createReplicas(5, 3, 1);
Atomix client = createClient();
client.keys().thenAccept(result -> {
threadAssertTrue(result.isEmpty());
resume();
});
await(10000);
client.get("test", TestResource.class).get(5, TimeUnit.SECONDS);
client.keys().thenAccept(result -> {
threadAssertTrue(result.size() == 1 && result.contains("test"));
resume();
});
await(10000);
client.get("value", ValueResource.class).get(5, TimeUnit.SECONDS);
client.keys().thenAccept(result -> {
threadAssertTrue(result.size() == 2 && result.contains("test") && result.contains("value"));
resume();
});
await(10000);
client.keys(TestResource.class).thenAccept(result -> {
threadAssertTrue(result.size() == 1 && result.contains("test"));
resume();
});
await(10000);
client.keys(ValueResource.class).thenAccept(result -> {
threadAssertTrue(result.size() == 1 && result.contains("value"));
resume();
});
await(10000);
}
/**
* Test resource.
*/
@ResourceTypeInfo(id=1, stateMachine=TestStateMachine.class)
public static class TestResource extends Resource<TestResource, Resource.Options> {
public TestResource(CopycatClient client, Resource.Options options) {
super(client, options);
}
@Override
public TestResource with(Consistency consistency) {
super.with(consistency);
return this;
}
public CompletableFuture<String> command(String value) {
return submit(new TestCommand(value));
}
public CompletableFuture<String> query(String value) {
return submit(new TestQuery(value));
}
}
/**
* Test state machine.
*/
public static class TestStateMachine extends ResourceStateMachine {
public String command(Commit<TestCommand> commit) {
return commit.operation().value();
}
public String query(Commit<TestQuery> commit) {
return commit.operation().value();
}
}
/**
* Test command.
*/
public static class TestCommand implements Command<String> {
private String value;
public TestCommand(String value) {
this.value = value;
}
public String value() {
return value;
}
}
/**
* Test query.
*/
public static class TestQuery implements Query<String> {
private String value;
public TestQuery(String value) {
this.value = value;
}
public String value() {
return value;
}
}
/**
* Value resource.
*/
@ResourceTypeInfo(id=2, stateMachine=ValueStateMachine.class)
public static class ValueResource extends Resource<ValueResource, Resource.Options> {
public ValueResource(CopycatClient client, Resource.Options options) {
super(client, options);
}
public CompletableFuture<Void> set(String value) {
return submit(new SetCommand(value));
}
public CompletableFuture<String> get() {
return submit(new GetQuery());
}
}
/**
* Value state machine.
*/
public static class ValueStateMachine extends ResourceStateMachine {
private Commit<SetCommand> value;
public void set(Commit<SetCommand> commit) {
Commit<SetCommand> oldValue = value;
value = commit;
if (oldValue != null)
oldValue.close();
}
public String get(Commit<GetQuery> commit) {
try {
return value != null ? value.operation().value() : null;
} finally {
commit.close();
}
}
}
/**
* Set command.
*/
public static class SetCommand implements Command<Void> {
private String value;
public SetCommand(String value) {
this.value = value;
}
public String value() {
return value;
}
}
/**
* Get query.
*/
public static class GetQuery implements Query<String> {
}
}