-
Notifications
You must be signed in to change notification settings - Fork 912
/
ClusteredResetMockTest.java
415 lines (306 loc) · 13 KB
/
ClusteredResetMockTest.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* 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.activemq.artemis.core.server.group.impl;
import javax.management.ObjectName;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.api.core.BroadcastGroupConfiguration;
import org.apache.activemq.artemis.api.core.ICoreMessage;
import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.api.core.management.AddressControl;
import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder;
import org.apache.activemq.artemis.core.config.ClusterConnectionConfiguration;
import org.apache.activemq.artemis.core.config.Configuration;
import org.apache.activemq.artemis.core.management.impl.ActiveMQServerControlImpl;
import org.apache.activemq.artemis.core.messagecounter.MessageCounterManager;
import org.apache.activemq.artemis.core.paging.PagingManager;
import org.apache.activemq.artemis.core.persistence.StorageManager;
import org.apache.activemq.artemis.core.postoffice.PostOffice;
import org.apache.activemq.artemis.core.remoting.server.RemotingService;
import org.apache.activemq.artemis.core.security.Role;
import org.apache.activemq.artemis.core.security.SecurityStore;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.Divert;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.core.server.QueueFactory;
import org.apache.activemq.artemis.core.server.routing.ConnectionRouter;
import org.apache.activemq.artemis.core.server.cluster.Bridge;
import org.apache.activemq.artemis.core.server.cluster.BroadcastGroup;
import org.apache.activemq.artemis.core.server.cluster.ClusterConnection;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;
import org.apache.activemq.artemis.core.server.management.ArtemisMBeanServerGuard;
import org.apache.activemq.artemis.core.server.management.ManagementService;
import org.apache.activemq.artemis.core.server.management.Notification;
import org.apache.activemq.artemis.core.server.management.NotificationListener;
import org.apache.activemq.artemis.core.settings.HierarchicalRepository;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.apache.activemq.artemis.core.transaction.ResourceManager;
import org.apache.activemq.artemis.spi.core.remoting.Acceptor;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.apache.activemq.artemis.utils.ReusableLatch;
import org.apache.activemq.artemis.utils.collections.ConcurrentHashSet;
import org.junit.Assert;
import org.junit.Test;
/**
* this is testing the case for resending notifications from RemotingGroupHandler
* There is a small window where you could receive notifications wrongly
* this test will make sure the component would play well with that notification
*/
public class ClusteredResetMockTest extends ActiveMQTestBase {
public static final SimpleString ANYCLUSTER = SimpleString.toSimpleString("anycluster");
@Test
public void testMultipleSenders() throws Throwable {
int NUMBER_OF_SENDERS = 100;
ReusableLatch latchSends = new ReusableLatch(NUMBER_OF_SENDERS);
FakeManagement fake = new FakeManagement(latchSends);
RemoteGroupingHandler handler = new RemoteGroupingHandler(fake, SimpleString.toSimpleString("tst1"), SimpleString.toSimpleString("tst2"), 50000, 499);
handler.start();
Sender[] sn = new Sender[NUMBER_OF_SENDERS];
for (int i = 0; i < sn.length; i++) {
sn[i] = new Sender("grp" + i, handler);
sn[i].start();
}
try {
// Waiting two requests to arrive
Assert.assertTrue(latchSends.await(1, TimeUnit.MINUTES));
// we will ask a resend.. need to add 2 back
for (int i = 0; i < NUMBER_OF_SENDERS; i++) {
// There is no countUp(NUMBER_OF_SENDERS); adding two back on the reusable latch
latchSends.countUp();
}
fake.pendingNotifications.clear();
handler.resendPending();
assertTrue(latchSends.await(10, TimeUnit.SECONDS));
HashSet<SimpleString> codesAsked = new HashSet<>();
for (Notification notification : fake.pendingNotifications) {
codesAsked.add(notification.getProperties().getSimpleStringProperty(ManagementHelper.HDR_PROPOSAL_GROUP_ID));
}
for (Sender snItem : sn) {
assertTrue(codesAsked.contains(snItem.code));
}
for (int i = NUMBER_OF_SENDERS - 1; i >= 0; i--) {
// Sending back the response as Notifications would be doing
Response response = new Response(sn[i].code, ANYCLUSTER);
handler.proposed(response);
}
for (Sender sni : sn) {
sni.join();
if (sni.ex != null) {
throw sni.ex;
}
}
} finally {
for (Sender sni : sn) {
sni.interrupt();
}
}
}
class Sender extends Thread {
SimpleString code;
public RemoteGroupingHandler handler;
Throwable ex;
Sender(String code, RemoteGroupingHandler handler) {
super("Sender::" + code);
this.code = SimpleString.toSimpleString(code);
this.handler = handler;
}
@Override
public void run() {
Proposal proposal = new Proposal(code, ANYCLUSTER);
try {
Response response = handler.propose(proposal);
if (response == null) {
ex = new NullPointerException("expected value on " + getName());
} else if (!response.getGroupId().equals(code)) {
ex = new IllegalStateException("expected code=" + code + " but it was " + response.getGroupId());
}
} catch (Throwable ex) {
ex.printStackTrace();
this.ex = ex;
}
}
}
class FakeManagement implements ManagementService {
public ConcurrentHashSet<Notification> pendingNotifications = new ConcurrentHashSet<>();
final ReusableLatch latch;
FakeManagement(ReusableLatch latch) {
this.latch = latch;
}
@Override
public MessageCounterManager getMessageCounterManager() {
return null;
}
@Override
public SimpleString getManagementAddress() {
return null;
}
@Override
public SimpleString getManagementNotificationAddress() {
return null;
}
@Override
public ObjectNameBuilder getObjectNameBuilder() {
return null;
}
@Override
public void setStorageManager(StorageManager storageManager) {
}
@Override
public ActiveMQServerControlImpl registerServer(PostOffice postOffice,
SecurityStore securityStore,
StorageManager storageManager,
Configuration configuration,
HierarchicalRepository<AddressSettings> addressSettingsRepository,
HierarchicalRepository<Set<Role>> securityRepository,
ResourceManager resourceManager,
RemotingService remotingService,
ActiveMQServer messagingServer,
QueueFactory queueFactory,
ScheduledExecutorService scheduledThreadPool,
PagingManager pagingManager,
boolean backup) throws Exception {
return null;
}
@Override
public void unregisterServer() throws Exception {
}
@Override
public void registerInJMX(ObjectName objectName, Object managedResource) throws Exception {
}
@Override
public void unregisterFromJMX(ObjectName objectName) throws Exception {
}
@Override
public void registerInRegistry(String resourceName, Object managedResource) {
}
@Override
public void unregisterFromRegistry(String resourceName) {
}
@Override
public void registerAddress(AddressInfo addressInfo) throws Exception {
}
@Override
public void registerAddressMeters(AddressInfo addressInfo, AddressControl addressControl) throws Exception {
}
@Override
public void unregisterAddress(SimpleString address) throws Exception {
}
@Override
public void registerQueue(Queue queue, SimpleString address, StorageManager storageManager) throws Exception {
}
@Override
public void registerQueue(Queue queue, SimpleString address, StorageManager storageManager, boolean forceInternal) throws Exception {
}
@Override
public void unregisterQueue(SimpleString name, SimpleString address, RoutingType routingType) throws Exception {
}
@Override
public void registerAcceptor(Acceptor acceptor, TransportConfiguration configuration) throws Exception {
}
@Override
public void unregisterAcceptors() {
}
@Override
public void registerDivert(Divert divert) throws Exception {
}
@Override
public void unregisterDivert(SimpleString name, SimpleString address) throws Exception {
}
@Override
public void registerBroadcastGroup(BroadcastGroup broadcastGroup,
BroadcastGroupConfiguration configuration) throws Exception {
}
@Override
public void unregisterBroadcastGroup(String name) throws Exception {
}
@Override
public void registerBridge(Bridge bridge) throws Exception {
}
@Override
public void unregisterBridge(String name) throws Exception {
}
@Override
public void registerCluster(ClusterConnection cluster,
ClusterConnectionConfiguration configuration) throws Exception {
}
@Override
public void unregisterCluster(String name) throws Exception {
}
@Override
public void registerConnectionRouter(ConnectionRouter router) throws Exception {
}
@Override
public void unregisterConnectionRouter(String name) throws Exception {
}
@Override
public Object getResource(String resourceName) {
return null;
}
@Override
public Object[] getResources(Class<?> resourceType) {
return new Object[0];
}
@Override
public ICoreMessage handleMessage(Message message) throws Exception {
return null;
}
@Override
public void registerHawtioSecurity(ArtemisMBeanServerGuard securityMBean) throws Exception {
}
@Override
public void unregisterHawtioSecurity() throws Exception {
}
@Override
public Object getAttribute(String resourceName, String attribute) {
return null;
}
@Override
public Object invokeOperation(String resourceName, String operation, Object[] params) throws Exception {
return null;
}
@Override
public void start() throws Exception {
}
@Override
public void stop() throws Exception {
}
@Override
public boolean isStarted() {
return false;
}
@Override
public void sendNotification(Notification notification) throws Exception {
pendingNotifications.add(notification);
latch.countDown();
}
@Override
public void enableNotifications(boolean enable) {
}
@Override
public void addNotificationListener(NotificationListener listener) {
}
@Override
public void removeNotificationListener(NotificationListener listener) {
}
}
}