-
Notifications
You must be signed in to change notification settings - Fork 912
/
QoSTests.java
671 lines (592 loc) · 30.9 KB
/
QoSTests.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
* 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.tests.integration.mqtt5.spec;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttPubAckMessage;
import io.netty.handler.codec.mqtt.MqttPubReplyMessageVariableHeader;
import io.netty.handler.codec.mqtt.MqttPublishMessage;
import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTReasonCodes;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.tests.integration.mqtt5.MQTT5TestSupport;
import org.apache.activemq.artemis.tests.util.RandomUtil;
import org.apache.activemq.artemis.tests.util.Wait;
import org.eclipse.paho.mqttv5.client.MqttClient;
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
import org.junit.Test;
/**
* Fulfilled by client or Netty codec (i.e. not tested here):
*
* [MQTT-4.3.1-1] In the QoS 0 delivery protocol, the sender MUST send a PUBLISH packet with QoS 0 and DUP flag set to 0.
* [MQTT-4.3.2-1] In the QoS 1 delivery protocol, the sender MUST assign an unused Packet Identifier each time it has a new Application Message to publish.
* [MQTT-4.3.3-1] In the QoS 2 delivery protocol, the sender MUST assign an unused Packet Identifier when it has a new Application Message to publish.
* [MQTT-4.3.3-2] In the QoS 2 delivery protocol, the sender MUST send a PUBLISH packet containing this Packet Identifier with QoS 2 and DUP flag set to 0.
*
*
* Unsure how to test:
*
* [MQTT-4.3.2-5] In the QoS 1 delivery protocol, the receiver after it has sent a PUBACK packet the receiver MUST treat any incoming PUBLISH packet that contains the same Packet Identifier as being a new Application Message, irrespective of the setting of its DUP flag.
* [MQTT-4.3.3-6] In the QoS 2 delivery protocol, the sender MUST NOT re-send the PUBLISH once it has sent the corresponding PUBREL packet.
* [MQTT-4.3.3-9] In the QoS 2 delivery protocol, the receiver if it has sent a PUBREC with a Reason Code of 0x80 or greater, the receiver MUST treat any subsequent PUBLISH packet that contains that Packet Identifier as being a new Application Message.
* [MQTT-4.3.3-12] In the QoS 2 delivery protocol, the receiver After it has sent a PUBCOMP, the receiver MUST treat any subsequent PUBLISH packet that contains that Packet Identifier as being a new Application Message.
*/
public class QoSTests extends MQTT5TestSupport {
/*
* [MQTT-4.3.2-2] In the QoS 1 delivery protocol, the sender MUST send a PUBLISH packet containing this Packet
* Identifier with QoS 1 and DUP flag set to 0.
*
* This test looks at the PUBLISH packet coming from *the broker* to the client
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS1andDupFlag() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new DefaultMqttCallback() {
@Override
public void messageArrived(String incomingTopic, MqttMessage message) throws Exception {
assertEquals(1, message.getQos());
assertFalse(message.isDuplicate());
latch.countDown();
}
});
consumer.subscribe(TOPIC, 1);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 1, false);
producer.disconnect();
producer.close();
assertTrue(latch.await(2, TimeUnit.SECONDS));
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.2-3] In the QoS 1 delivery protocol, the sender MUST treat the PUBLISH packet as “unacknowledged” until
* it has received the corresponding PUBACK packet from the receiver.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS1PubAck() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBACK) {
// ensure the message is still in the queue before we get the ack from the client
assertEquals(1, getSubscriptionQueue(TOPIC).getMessageCount());
assertEquals(1, getSubscriptionQueue(TOPIC).getDeliveringCount());
// ensure the ids match so we know this is the "corresponding" PUBACK for the previous PUBLISH
assertEquals(packetId.get(), ((MqttPubReplyMessageVariableHeader)packet.variableHeader()).messageId());
ackLatch.countDown();
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
packetId.set(((MqttPublishMessage)packet).variableHeader().packetId());
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new LatchedMqttCallback(latch));
consumer.subscribe(TOPIC, 1);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 1, false);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertEquals(0, getSubscriptionQueue(TOPIC).getMessageCount());
assertEquals(0, getSubscriptionQueue(TOPIC).getDeliveringCount());
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.2-4] In the QoS 1 delivery protocol, the receiver MUST respond with a PUBACK packet containing the
* Packet Identifier from the incoming PUBLISH packet, having accepted ownership of the Application Message.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS1PubAckId() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
packetId.set(((MqttPublishMessage)packet).variableHeader().packetId());
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBACK) {
assertEquals(packetId.get(), ((MqttPubAckMessage)packet).variableHeader().messageId());
ackLatch.countDown();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new LatchedMqttCallback(latch));
consumer.subscribe(TOPIC, 1);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 1, false);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(2, TimeUnit.SECONDS));
consumer.disconnect();
consumer.close();
}
/*
* QoS 2 exactly-once delivery semantics. This diagram was adapted from:
* https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901237
*
* ================================================================================
* | Sender Action | MQTT Control Packet | Receiver Action |
* |-------------------------|--------------------------|-------------------------|
* | Store Message | | |
* --------------------------------------------------------------------------------
* | PUBLISH QoS 2, DUP=0 | | |
* | <Packet ID> | | |
* |-------------------------|--------------------------|-------------------------|
* | | =====> | |
* |-------------------------|--------------------------|-------------------------|
* | | |Store <Packet ID> then |
* | | |initiate onward delivery |
* | | |of the Application |
* | | |Message |
* |-------------------------|--------------------------|-------------------------|
* | | |PUBREC <Packet ID> |
* | | |<Reason Code> |
* |-------------------------|--------------------------|-------------------------|
* | | <===== | |
* |-------------------------|--------------------------|-------------------------|
* | Discard message, store | | |
* | PUBREC <Packet ID> | | |
* |-------------------------|--------------------------|-------------------------|
* | PUBREL <Packet ID> | | |
* |-------------------------|--------------------------|-------------------------|
* | | =====> | |
* |-------------------------|--------------------------|-------------------------|
* | | |Discard <Packet ID> |
* |-------------------------|--------------------------|-------------------------|
* | | |Send PUBCOMP <Packet ID> |
* |-------------------------|--------------------------|-------------------------|
* | | <===== | |
* |-------------------------|--------------------------|-------------------------|
* | Discard stored state | | |
* ================================================================================
*/
/*
* [MQTT-4.3.3-3] In the QoS 2 delivery protocol, the sender MUST treat the PUBLISH packet as “unacknowledged” until
* it has received the corresponding PUBREC packet from the receiver.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2PubRec() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREC) {
// ensure the message is still in the queue before we get the ack from the client
assertEquals(1, getSubscriptionQueue(TOPIC).getMessageCount());
assertEquals(1, getSubscriptionQueue(TOPIC).getDeliveringCount());
// ensure the ids match so we know this is the "corresponding" PUBREC for the previous PUBLISH
assertEquals(packetId.get(), ((MqttPubReplyMessageVariableHeader)packet.variableHeader()).messageId());
ackLatch.countDown();
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
packetId.set(((MqttPublishMessage)packet).variableHeader().packetId());
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new LatchedMqttCallback(latch));
consumer.subscribe(TOPIC, 2);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 2, false);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertEquals(0, getSubscriptionQueue(TOPIC).getMessageCount());
assertEquals(0, getSubscriptionQueue(TOPIC).getDeliveringCount());
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.3-4] In the QoS 2 delivery protocol, the sender MUST send a PUBREL packet when it receives a PUBREC
* packet from the receiver with a Reason Code value less than 0x80. This PUBREL packet MUST contain the same Packet
* Identifier as the original PUBLISH packet.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2PubRelId() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
final AtomicBoolean pubRecReceived = new AtomicBoolean(false);
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
packetId.set(((MqttPublishMessage)packet).variableHeader().packetId());
}
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREC) {
assertEquals(packetId.get(), ((MqttPubReplyMessageVariableHeader)packet.variableHeader()).messageId());
assertEquals(MQTTReasonCodes.SUCCESS, ((MqttPubReplyMessageVariableHeader)packet.variableHeader()).reasonCode());
pubRecReceived.set(true);
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREL) {
assertTrue(pubRecReceived.get());
assertEquals(packetId.get(), ((MqttPubAckMessage)packet).variableHeader().messageId());
ackLatch.countDown();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new LatchedMqttCallback(latch));
consumer.subscribe(TOPIC, 2);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 2, false);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(2, TimeUnit.SECONDS));
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.3-5] In the QoS 2 delivery protocol, the sender MUST treat the PUBREL packet as “unacknowledged” until
* it has received the corresponding PUBCOMP packet from the receiver.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2PubRel() throws Exception {
final String TOPIC = RandomUtil.randomString();
final String CONSUMER_CLIENT_ID = "consumer";
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBCOMP) {
try {
// ensure the message is still in the management queue before we get the PUBCOMP from the client
Wait.assertEquals(1L, () -> server.locateQueue(MQTTUtil.MANAGEMENT_QUEUE_PREFIX + CONSUMER_CLIENT_ID).getMessageCount(), 2000, 100);
Wait.assertEquals(1L, () -> server.locateQueue(MQTTUtil.MANAGEMENT_QUEUE_PREFIX + CONSUMER_CLIENT_ID).getDeliveringCount(), 2000, 100);
} catch (Exception e) {
return false;
}
// ensure the ids match so we know this is the "corresponding" PUBCOMP for the previous PUBLISH
assertEquals(packetId.get(), ((MqttPubReplyMessageVariableHeader)packet.variableHeader()).messageId());
ackLatch.countDown();
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
packetId.set(((MqttPublishMessage)packet).variableHeader().packetId());
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient(CONSUMER_CLIENT_ID);
consumer.connect();
consumer.setCallback(new LatchedMqttCallback(latch));
consumer.subscribe(TOPIC, 2);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 2, false);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertEquals(0, getSubscriptionQueue(TOPIC).getMessageCount());
assertEquals(0, getSubscriptionQueue(TOPIC).getDeliveringCount());
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.3-7] In the QoS 2 delivery protocol, the sender MUST NOT apply Application Message expiry if a PUBLISH
* packet has been sent.
*
* [MQTT-4.3.3-13] In the QoS 2 delivery protocol, the receiver MUST continue the QoS 2 acknowledgement sequence even if it has applied Application Message expiry.
*
* Due to the nature of the underlying queue semantics once a message is "in delivery" it's no longer available for
* expiration. This test demonstrates that.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2WithExpiration() throws Exception {
final String TOPIC = "myTopic";
final CountDownLatch ackLatch = new CountDownLatch(1);
final CountDownLatch expireRefsLatch = new CountDownLatch(1);
final long messageExpiryInterval = 2;
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREC) {
// ensure the message is still in the queue before we get the PUBREC from the client
assertEquals(1, getSubscriptionQueue(TOPIC).getMessageCount());
assertEquals(1, getSubscriptionQueue(TOPIC).getDeliveringCount());
try {
// ensure enough time has passed for the message to expire
Thread.sleep(messageExpiryInterval * 1500);
getSubscriptionQueue(TOPIC).expireReferences(expireRefsLatch::countDown);
assertTrue(expireRefsLatch.await(2, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
fail();
}
ackLatch.countDown();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new DefaultMqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
latch.countDown();
}
});
consumer.subscribe(TOPIC, 2);
MqttClient producer = createPahoClient("producer");
producer.connect();
MqttMessage m = new MqttMessage();
MqttProperties props = new MqttProperties();
props.setMessageExpiryInterval(messageExpiryInterval);
m.setProperties(props);
m.setQos(2);
m.setPayload("foo".getBytes(StandardCharsets.UTF_8));
producer.publish(TOPIC, m);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(messageExpiryInterval * 2, TimeUnit.SECONDS));
assertTrue(latch.await(messageExpiryInterval * 2, TimeUnit.SECONDS));
Wait.assertEquals(0, () -> getSubscriptionQueue(TOPIC).getMessageCount());
Wait.assertEquals(0, () -> getSubscriptionQueue(TOPIC).getDeliveringCount());
Wait.assertEquals(0, () -> getSubscriptionQueue(TOPIC).getMessagesExpired());
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.3-8] In the QoS 2 delivery protocol, the receiver MUST respond with a PUBREC containing the Packet
* Identifier from the incoming PUBLISH packet, having accepted ownership of the Application Message.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2PubRecId() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
packetId.set(((MqttPublishMessage)packet).variableHeader().packetId());
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREC) {
assertEquals(packetId.get(), ((MqttPubAckMessage)packet).variableHeader().messageId());
ackLatch.countDown();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new LatchedMqttCallback(latch));
consumer.subscribe(TOPIC, 2);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 2, false);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(2, TimeUnit.SECONDS));
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.3-10] In the QoS 2 delivery protocol, the receiver until it has received the corresponding PUBREL
* packet, the receiver MUST acknowledge any subsequent PUBLISH packet with the same Packet Identifier by sending a
* PUBREC. It MUST NOT cause duplicate messages to be delivered to any onward recipients in this case.
*
* In this test we simulate a client sending a QoS 2 message but being disconnected before it receives the initial
* PUBREC. When the client connects again it resends the message, but the broker does not duplicate it. It simply
* responds withe appropriate PUBREC and the client completes the QoS process.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2DuplicatePub() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
AtomicInteger count = new AtomicInteger(0);
final byte[] PAYLOAD = RandomUtil.randomString().getBytes();
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBLISH) {
packetId.set(((MqttPublishMessage)packet).variableHeader().packetId());
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (count.get() == 0 && packet.fixedHeader().messageType() == MqttMessageType.PUBREC) {
ackLatch.countDown();
count.incrementAndGet();
connection.disconnect(true);
return false;
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
Queue queue = server.createQueue(new QueueConfiguration(TOPIC));
MqttClient producer = createPahoClient("producer");
MqttConnectionOptions connectionOptions = new MqttConnectionOptions();
connectionOptions.setCleanStart(false);
connectionOptions.setSessionExpiryInterval(999L);
producer.connect(connectionOptions);
try {
producer.publish(TOPIC, PAYLOAD, 2, false);
} catch (Exception e) {
// ignore
}
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
producer.disconnectForcibly(1, 1, false);
producer.close();
producer = createPahoClient("producer");
producer.connect(connectionOptions);
producer.publish(TOPIC, PAYLOAD, 2, false);
producer.disconnect();
producer.close();
assertEquals(1, queue.getMessageCount());
}
/*
* [MQTT-4.3.3-11] In the QoS 2 delivery protocol, the receiver MUST respond to a PUBREL packet by sending a PUBCOMP
* packet containing the same Packet Identifier as the PUBREL.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2PubCompId() throws Exception {
final String TOPIC = RandomUtil.randomString();
final CountDownLatch ackLatch = new CountDownLatch(1);
final AtomicInteger packetId = new AtomicInteger();
MQTTInterceptor incomingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREL) {
packetId.set(((MqttPubReplyMessageVariableHeader)packet.variableHeader()).messageId());
}
return true;
};
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBCOMP) {
assertEquals(packetId.get(), ((MqttPubAckMessage)packet).variableHeader().messageId());
ackLatch.countDown();
}
return true;
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
final CountDownLatch latch = new CountDownLatch(1);
MqttClient consumer = createPahoClient("consumer");
consumer.connect();
consumer.setCallback(new LatchedMqttCallback(latch));
consumer.subscribe(TOPIC, 2);
MqttClient producer = createPahoClient("producer");
producer.connect();
producer.publish(TOPIC, RandomUtil.randomString().getBytes(), 2, false);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(2, TimeUnit.SECONDS));
consumer.disconnect();
consumer.close();
}
/*
* [MQTT-4.3.3-13] In the QoS 2 delivery protocol, the receiver MUST continue the QoS 2 acknowledgement sequence even
* if it has applied Application Message expiry.
*/
@Test(timeout = DEFAULT_TIMEOUT)
public void testQoS2WithExpiration2() throws Exception {
final String TOPIC = "myTopic";
server.createQueue(new QueueConfiguration(RandomUtil.randomString()).setAddress(TOPIC).setRoutingType(RoutingType.MULTICAST));
final CountDownLatch ackLatch = new CountDownLatch(1);
final CountDownLatch expireRefsLatch = new CountDownLatch(1);
final long messageExpiryInterval = 1;
MQTTInterceptor outgoingInterceptor = (packet, connection) -> {
if (packet.fixedHeader().messageType() == MqttMessageType.PUBREC) {
// ensure the message is in the queue before trying to expire
Wait.assertTrue(() -> getSubscriptionQueue(TOPIC).getMessageCount() == 1, 2000, 100);
try {
// ensure enough time has passed for the message to expire
Thread.sleep(messageExpiryInterval * 1500);
getSubscriptionQueue(TOPIC).expireReferences(expireRefsLatch::countDown);
assertTrue(expireRefsLatch.await(2, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
fail();
}
ackLatch.countDown();
}
return true;
};
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
MqttClient producer = createPahoClient("producer");
producer.connect();
MqttMessage m = new MqttMessage();
MqttProperties props = new MqttProperties();
props.setMessageExpiryInterval(messageExpiryInterval);
m.setProperties(props);
m.setQos(2);
m.setPayload("foo".getBytes(StandardCharsets.UTF_8));
producer.publish(TOPIC, m);
producer.disconnect();
producer.close();
assertTrue(ackLatch.await(messageExpiryInterval * 2, TimeUnit.SECONDS));
Wait.assertEquals(1, () -> getSubscriptionQueue(TOPIC).getMessagesExpired());
}
}