-
Notifications
You must be signed in to change notification settings - Fork 912
/
MQTTRetainMessageManagerTest.java
265 lines (225 loc) · 11.8 KB
/
MQTTRetainMessageManagerTest.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
/*
* 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.protocol.mqtt;
import static java.util.Objects.nonNull;
import static org.awaitility.Awaitility.await;
import static org.awaitility.Durations.FIVE_HUNDRED_MILLISECONDS;
import static org.awaitility.Durations.TEN_SECONDS;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.IntStream;
import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.core.config.WildcardConfiguration;
import org.apache.activemq.artemis.core.server.MessageReference;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.junit.EmbeddedJMSResource;
import org.apache.activemq.artemis.utils.collections.LinkedListIterator;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.jboss.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import com.github.javafaker.ChuckNorris;
import com.github.javafaker.Faker;
@SuppressWarnings("deprecation")
public class MQTTRetainMessageManagerTest {
private Logger log = Logger.getLogger(MQTTRetainMessageManagerTest.class);
public EmbeddedJMSResource jmsServer = new EmbeddedJMSResource("embedded-artemis-server-mqtt.xml");
private MqttClientService mqttPublisher;
private MqttClientService mqttConsumerCount;
private MqttClientService mqttConsumerBeforePublish;
private MqttClientService mqttConsumerAfterPublish;
private MqttClientService mqttConsumerAfterPublish2;
private final AtomicInteger publishCount = new AtomicInteger(0);
private final AtomicInteger arrivedCountBeforePublish = new AtomicInteger();
private final AtomicInteger arrivedCountAferPublish = new AtomicInteger();
private final AtomicInteger arrivedCountAferPublish2 = new AtomicInteger();
private final AtomicReference<MqttMessage> lastMessagePublished = new AtomicReference<>();
private final AtomicReference<MqttMessage> lastMessageArrivedOnConsumerBeforePublish = new AtomicReference<>();
private final AtomicReference<MqttMessage> lastMessageArrivedOnConsumerAfterPublish = new AtomicReference<>();
private final AtomicReference<MqttMessage> lastMessageArrivedOnConsumerAfterPublish2 = new AtomicReference<>();
private final String topic = "fact";
private final ChuckNorris chuckNorris = (new Faker()).chuckNorris();
private final int numberOfMessages = 1000;
private final int numberOfTests = 10;
@Rule
public RuleChain rulechain = RuleChain.outerRule(jmsServer);
@Before
public void beforeEach() throws MqttException {
publishCount.set(0);
mqttPublisher = new MqttClientService("publisher", null);
mqttPublisher.init();
final MqttMessage clearRetainedMessage = new MqttMessage(new byte[] {});
clearRetainedMessage.setRetained(true);
clearRetainedMessage.setQos(1);
mqttPublisher.publish(topic, clearRetainedMessage);
mqttConsumerCount = new MqttClientService("consumer-count", null);
mqttConsumerCount.init();
mqttConsumerCount.setMessageConsumer(message -> publishCount.incrementAndGet());
arrivedCountBeforePublish.set(0);
mqttConsumerBeforePublish = new MqttClientService("consumer-before",
message -> {
final String payload = new String(message.getPayload());
lastMessageArrivedOnConsumerBeforePublish.set(message);
arrivedCountBeforePublish.incrementAndGet();
log.debugf("[MQTT][before ][retained: %s][duplicate: %s][qos: %s] %s",
message.isRetained(), message.isDuplicate(), message.getQos(), payload);
});
mqttConsumerBeforePublish.init();
arrivedCountAferPublish.set(0);
arrivedCountAferPublish2.set(0);
mqttConsumerAfterPublish = new MqttClientService("consumer-after",
message -> {
final String payload = new String(message.getPayload());
lastMessageArrivedOnConsumerAfterPublish.set(message);
arrivedCountAferPublish.incrementAndGet();
log.infof("[MQTT][after ][retained: %s][duplicate: %s][qos: %s] %s",
message.isRetained(), message.isDuplicate(), message.getQos(), payload);
});
mqttConsumerAfterPublish2 = new MqttClientService("consumer-after2",
message -> {
final String payload = new String(message.getPayload());
lastMessageArrivedOnConsumerAfterPublish2.set(message);
arrivedCountAferPublish2.incrementAndGet();
log.infof("[MQTT][after2 ][retained: %s][duplicate: %s][qos: %s] %s",
message.isRetained(), message.isDuplicate(), message.getQos(), payload);
});
mqttConsumerAfterPublish.init();
mqttConsumerAfterPublish2.init();
}
@After
public void afterEach() throws MqttException {
mqttPublisher.destroy();
mqttConsumerCount.unsubsribe(topic);
mqttConsumerCount.destroy();
mqttConsumerBeforePublish.unsubsribe(topic);
mqttConsumerBeforePublish.destroy();
mqttConsumerAfterPublish.unsubsribe(topic);
mqttConsumerAfterPublish.destroy();
mqttConsumerAfterPublish2.unsubsribe(topic);
mqttConsumerAfterPublish2.destroy();
}
@Test
public void testAtMostOnce() throws MqttException {
IntStream.of(numberOfTests).forEach(i -> actAndAssert(i, 0));
}
@Test
public void testAtLeastOnce() throws MqttException {
IntStream.of(numberOfTests).forEach(i -> actAndAssert(i, 1));
}
@Test
public void testExactlyOnce() throws MqttException {
IntStream.of(numberOfTests).forEach(i -> actAndAssert(i, 2));
}
private void actAndAssert(int i, int qos) {
try {
// Act
mqttConsumerBeforePublish.subscribe(topic, qos);
publish(qos);
logAftePublish(i, qos);
logRetainedMessagesQueue();
mqttConsumerAfterPublish.subscribe(topic, qos);
mqttConsumerAfterPublish2.subscribe(topic, qos);
awaitUntilLastMessageArrivedOnConsumerAfterPublish();
awaitUntilLastMessageArrivedOnConsumerAfterPublish2();
// Assert
assertEquals(1, arrivedCountAferPublish.get());
assertLastMessageOnConsumerBeforePublishArrivedEqualsLastMessagePublished();
assertLastMessageOnConsumerAfterPublishArrivedEqualsLastMessagePublished();
} catch (MqttException e) {
fail(e.getMessage());
}
}
protected void publish(final int qos) throws MqttException {
mqttConsumerCount.subscribe(topic, qos);
IntStream.range(0, numberOfMessages).forEach(i -> {
final String fact = String.format("[%s] %s", i, chuckNorris.fact());
final MqttMessage message = message(fact, qos, true);
mqttPublisher.publish(topic, message);
lastMessagePublished.set(message);
});
awaitUntilPiblishCount();
}
protected MqttMessage message(final String payload, final int qos, final boolean retained) {
final MqttMessage message = new MqttMessage();
message.setQos(qos);
message.setRetained(retained);
message.setPayload(payload.getBytes());
return message;
}
private void awaitUntilPiblishCount() {
await()
.with()
.pollDelay(FIVE_HUNDRED_MILLISECONDS)
.atMost(TEN_SECONDS)
.until(() -> publishCount.get() >= numberOfMessages);
}
private void awaitUntilLastMessageArrivedOnConsumerAfterPublish() {
await()
.pollDelay(FIVE_HUNDRED_MILLISECONDS)
.atMost(TEN_SECONDS)
.until(() -> nonNull(lastMessageArrivedOnConsumerAfterPublish.get()));
}
private void awaitUntilLastMessageArrivedOnConsumerAfterPublish2() {
await()
.pollDelay(FIVE_HUNDRED_MILLISECONDS)
.atMost(TEN_SECONDS)
.until(() -> nonNull(lastMessageArrivedOnConsumerAfterPublish2.get()));
}
private void assertLastMessageOnConsumerBeforePublishArrivedEqualsLastMessagePublished() {
assertArrayEquals(String.format(
"\nMessage arrived on consumer subscribed before the publish is different from the last published message!\nPublished: %s\nArrived : %s\n",
new String(lastMessagePublished.get().getPayload()), new String(lastMessageArrivedOnConsumerAfterPublish.get().getPayload())),
lastMessagePublished.get().getPayload(), lastMessageArrivedOnConsumerBeforePublish.get().getPayload());
}
private void assertLastMessageOnConsumerAfterPublishArrivedEqualsLastMessagePublished() {
assertArrayEquals(String.format(
"\nMessage arrived on consumer subscribed after the publish is different from the last published message!\nPublished: %s\nArrived : %s\n",
new String(lastMessagePublished.get().getPayload()), new String(lastMessageArrivedOnConsumerAfterPublish.get().getPayload())),
lastMessagePublished.get().getPayload(), lastMessageArrivedOnConsumerAfterPublish.get().getPayload());
assertArrayEquals(String.format(
"\nMessage arrived on consumer subscribed after the publish (2) is different from the last published message!\nPublished: %s\nArrived : %s\n",
new String(lastMessagePublished.get().getPayload()), new String(lastMessageArrivedOnConsumerAfterPublish.get().getPayload())),
lastMessagePublished.get().getPayload(), lastMessageArrivedOnConsumerAfterPublish2.get().getPayload());
}
private void logAftePublish(int i, int qos) {
log.infof("--- QoS: %s --- %s/%s---", qos, i, numberOfTests);
log.infof("[MQTT][publish][retained: %s][duplicate: %s][qos: %s] %s",
lastMessagePublished.get().isRetained(), lastMessagePublished.get().isDuplicate(), lastMessagePublished.get().getQos(), lastMessagePublished.get());
log.infof("[MQTT][before ][retained: %s][duplicate: %s][qos: %s] %s",
lastMessageArrivedOnConsumerBeforePublish.get().isRetained(),
lastMessageArrivedOnConsumerBeforePublish.get().isDuplicate(),
lastMessageArrivedOnConsumerBeforePublish.get().getQos(),
new String(lastMessageArrivedOnConsumerBeforePublish.get().getPayload()));
}
private void logRetainedMessagesQueue() {
final WildcardConfiguration wildcardConfiguration = new WildcardConfiguration();
final String retainAddress = MQTTUtil.convertMqttTopicFilterToCoreAddress(MQTTUtil.MQTT_RETAIN_ADDRESS_PREFIX, topic, wildcardConfiguration);
final Queue queue = jmsServer.getDestinationQueue(retainAddress);
final LinkedListIterator<MessageReference> browserIterator = queue.browserIterator();
browserIterator.forEachRemaining(messageReference -> {
final Message message = messageReference.getMessage();
final String body = message.toCore().getBuffer().toString(StandardCharsets.UTF_8);
log.infof("[MQTT][%s][%s][%s]", retainAddress, message, body);
});
}
}