forked from eclipse-vertx/vert.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClusteredEventBusTestBase.java
328 lines (297 loc) · 11 KB
/
ClusteredEventBusTestBase.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
/*
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.eventbus;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.shareddata.AsyncMapTest;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.core.spi.cluster.NodeSelector;
import io.vertx.core.spi.cluster.RegistrationUpdateEvent;
import io.vertx.core.spi.cluster.WrappedClusterManager;
import io.vertx.core.spi.cluster.WrappedNodeSelector;
import io.vertx.test.core.TestUtils;
import io.vertx.test.fakecluster.FakeClusterManager;
import org.junit.Test;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static org.hamcrest.CoreMatchers.*;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class ClusteredEventBusTestBase extends EventBusTestBase {
protected static final String ADDRESS1 = "some-address1";
protected ClusterManager getClusterManager() {
return new FakeClusterManager();
}
@Override
protected void clusteredVertx(VertxOptions options, Handler<AsyncResult<Vertx>> ar) {
Promise<Vertx> promise = Promise.promise();
super.clusteredVertx(options, promise);
promise.future().onSuccess(vertx -> {
ImmutableObjectCodec immutableObjectCodec = new ImmutableObjectCodec();
vertx.eventBus().registerCodec(immutableObjectCodec);
vertx.eventBus().codecSelector(obj -> obj instanceof ImmutableObject ? immutableObjectCodec.name() : null);
vertx.eventBus().clusterSerializableChecker(className -> className.startsWith(AsyncMapTest.class.getName()));
vertx.eventBus().serializableChecker(className -> {
return EventBus.DEFAULT_SERIALIZABLE_CHECKER.apply(className) || className.startsWith(AsyncMapTest.class.getName());
});
}).onComplete(ar);
}
@Override
protected boolean shouldImmutableObjectBeCopied() {
return true;
}
@Override
protected <T, R> void testSend(T val, R received, Consumer<T> consumer, DeliveryOptions options) {
if (vertices == null) {
startNodes(2);
}
MessageConsumer<T> reg = vertices[1].eventBus().<T>consumer(ADDRESS1).handler((Message<T> msg) -> {
if (consumer == null) {
assertTrue(msg.isSend());
assertEquals(received, msg.body());
if (options != null) {
assertNotNull(msg.headers());
int numHeaders = options.getHeaders() != null ? options.getHeaders().size() : 0;
assertEquals(numHeaders, msg.headers().size());
if (numHeaders != 0) {
for (Map.Entry<String, String> entry : options.getHeaders().entries()) {
assertEquals(msg.headers().get(entry.getKey()), entry.getValue());
}
}
}
} else {
consumer.accept(msg.body());
}
testComplete();
});
reg.completionHandler(ar -> {
assertTrue(ar.succeeded());
if (options == null) {
vertices[0].eventBus().send(ADDRESS1, val);
} else {
vertices[0].eventBus().send(ADDRESS1, val, options);
}
});
await();
}
@Override
protected <T> void testSend(T val, Consumer <T> consumer) {
testSend(val, val, consumer, null);
}
@Override
protected <T> void testReply(T val, Consumer<T> consumer) {
testReply(val, val, consumer, null);
}
@Override
protected <T, R> void testReply(T val, R received, Consumer<R> consumer, DeliveryOptions options) {
if (vertices == null) {
startNodes(2);
}
String str = TestUtils.randomUnicodeString(1000);
MessageConsumer<?> reg = vertices[1].eventBus().consumer(ADDRESS1).handler(msg -> {
assertEquals(str, msg.body());
if (options == null) {
msg.reply(val);
} else {
msg.reply(val, options);
}
});
reg.completionHandler(ar -> {
assertTrue(ar.succeeded());
vertices[0].eventBus().request(ADDRESS1, str, onSuccess((Message<R> reply) -> {
if (consumer == null) {
assertTrue(reply.isSend());
assertEquals(received, reply.body());
if (options != null && options.getHeaders() != null) {
assertNotNull(reply.headers());
assertEquals(options.getHeaders().size(), reply.headers().size());
for (Map.Entry<String, String> entry: options.getHeaders().entries()) {
assertEquals(reply.headers().get(entry.getKey()), entry.getValue());
}
}
} else {
consumer.accept(reply.body());
}
testComplete();
}));
});
await();
}
@Test
public void testRegisterRemote1() {
startNodes(2);
String str = TestUtils.randomUnicodeString(100);
vertices[0].eventBus().<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
assertEquals(str, msg.body());
testComplete();
}).completionHandler(ar -> {
assertTrue(ar.succeeded());
vertices[1].eventBus().send(ADDRESS1, str);
});
await();
}
@Test
public void testRegisterRemote2() {
startNodes(2);
String str = TestUtils.randomUnicodeString(100);
vertices[0].eventBus().consumer(ADDRESS1, (Message<String> msg) -> {
assertEquals(str, msg.body());
testComplete();
}).completionHandler(ar -> {
assertTrue(ar.succeeded());
vertices[1].eventBus().send(ADDRESS1, str);
});
await();
}
@Override
protected <T> void testPublish(T val, Consumer<T> consumer) {
int numNodes = 3;
startNodes(numNodes);
AtomicInteger count = new AtomicInteger();
class MyHandler implements Handler<Message<T>> {
@Override
public void handle(Message<T> msg) {
if (consumer == null) {
assertFalse(msg.isSend());
assertEquals(val, msg.body());
} else {
consumer.accept(msg.body());
}
if (count.incrementAndGet() == numNodes - 1) {
testComplete();
}
}
}
AtomicInteger registerCount = new AtomicInteger(0);
class MyRegisterHandler implements Handler<AsyncResult<Void>> {
@Override
public void handle(AsyncResult<Void> ar) {
assertTrue(ar.succeeded());
if (registerCount.incrementAndGet() == 2) {
vertices[0].eventBus().publish(ADDRESS1, val);
}
}
}
MessageConsumer reg = vertices[2].eventBus().<T>consumer(ADDRESS1).handler(new MyHandler());
reg.completionHandler(new MyRegisterHandler());
reg = vertices[1].eventBus().<T>consumer(ADDRESS1).handler(new MyHandler());
reg.completionHandler(new MyRegisterHandler());
await();
}
@Test
public void testMessageBodyInterceptor() throws Exception {
String content = TestUtils.randomUnicodeString(13);
startNodes(2);
waitFor(2);
CountDownLatch latch = new CountDownLatch(1);
vertices[0].eventBus().registerCodec(new StringLengthCodec()).<Integer>consumer("whatever", msg -> {
assertEquals(content.length(), (int) msg.body());
complete();
}).completionHandler(ar -> latch.countDown());
awaitLatch(latch);
StringLengthCodec codec = new StringLengthCodec();
vertices[1].eventBus().registerCodec(codec).addOutboundInterceptor(sc -> {
if ("whatever".equals(sc.message().address())) {
assertEquals(content, sc.body());
complete();
}
sc.next();
}).send("whatever", content, new DeliveryOptions().setCodecName(codec.name()));
await();
}
@Test
public void testClusteredUnregistration() throws Exception {
CountDownLatch updateLatch = new CountDownLatch(3);
Supplier<VertxOptions> options = () -> getOptions().setClusterManager(new WrappedClusterManager(getClusterManager()) {
@Override
public void init(Vertx vertx, NodeSelector nodeSelector) {
super.init(vertx, new WrappedNodeSelector(nodeSelector) {
@Override
public void registrationsUpdated(RegistrationUpdateEvent event) {
super.registrationsUpdated(event);
if (event.address().equals("foo") && event.registrations().isEmpty()) {
updateLatch.countDown();
}
}
});
}
});
startNodes(options.get(), options.get());
MessageConsumer<Object> consumer = vertices[0].eventBus().consumer("foo", msg -> msg.reply(msg.body()));
consumer.completionHandler(onSuccess(reg -> {
vertices[0].eventBus().request("foo", "echo", onSuccess(reply1 -> {
assertEquals("echo", reply1.body());
vertices[1].eventBus().request("foo", "echo", onSuccess(reply2 -> {
assertEquals("echo", reply1.body());
consumer.unregister(onSuccess(unreg -> {
updateLatch.countDown();
}));
}));
}));
}));
awaitLatch(updateLatch);
vertices[1].eventBus().request("foo", "echo", onFailure(fail1 -> {
assertThat(fail1, is(instanceOf(ReplyException.class)));
assertEquals(ReplyFailure.NO_HANDLERS, ((ReplyException) fail1).failureType());
vertices[0].eventBus().request("foo", "echo", onFailure(fail2 -> {
assertThat(fail2, is(instanceOf(ReplyException.class)));
assertEquals(ReplyFailure.NO_HANDLERS, ((ReplyException) fail2).failureType());
testComplete();
}));
}));
await();
}
@Test
public void testMessagingInStopMethod() throws Exception {
startNodes(2);
AtomicInteger count = new AtomicInteger();
class MyVerticle extends AbstractVerticle {
final String pingServerAddress;
final String pingClientAddress;
MyVerticle(String pingServerAddress, String pingClientAddress) {
this.pingServerAddress = pingServerAddress;
this.pingClientAddress = pingClientAddress;
}
@Override
public void start(Promise<Void> startPromise) throws Exception {
vertx.eventBus().consumer(pingServerAddress, msg -> msg.reply("pong")).completionHandler(startPromise);
}
@Override
public void stop(Promise<Void> stopPromise) throws Exception {
vertx.eventBus().<String>request(pingClientAddress, "ping", onSuccess(msg -> {
assertEquals("pong", msg.body());
count.incrementAndGet();
vertx.setPeriodic(10, l -> {
if (count.get() == 2) {
stopPromise.complete();
}
});
}));
}
}
waitFor(2);
vertices[0].deployVerticle(new MyVerticle("foo", "bar"), onSuccess(id1 -> {
vertices[1].deployVerticle(new MyVerticle("bar", "foo"), onSuccess(id2 -> {
vertices[0].close(onSuccess(v -> complete()));
vertices[1].close(onSuccess(v -> complete()));
}));
}));
await();
}
}