11package com.ss.mqtt.broker.test.integration
22
3-
43import com.hivemq.client.mqtt.datatypes.MqttQos
4+ import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient
55import com.hivemq.client.mqtt.mqtt5.message.Mqtt5MessageType
66import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5PayloadFormatIndicator
77import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish
@@ -15,27 +15,16 @@ class ConnectSubscribePublishTest extends IntegrationSpecification {
1515 given :
1616 def received = new AtomicReference<Mqtt5Publish > ()
1717 def subscriber = buildClient()
18+ def subscriberId = subscriber. getConfig(). clientIdentifier. toString()
1819 def publisher = buildClient()
1920 when :
2021 subscriber. connect(). join()
2122 publisher. connect(). join()
22-
23- def subscribeResult = subscriber. subscribeWith()
24- .topicFilter(topicFilter)
25- .qos(MqttQos . AT_MOST_ONCE )
26- .callback({ publish -> received. set(publish) })
27- .send()
28- .join()
29-
30- def publishResult = publisher. publishWith()
31- .topic(topicFilter)
32- .qos(MqttQos . AT_MOST_ONCE )
33- .payload(publishPayload)
34- .payloadFormatIndicator(Mqtt5PayloadFormatIndicator . UTF_8 )
35- .send()
36- .join()
3723
38- Thread . sleep(500 )
24+ def subscribeResult = subscribe(subscriber, subscriberId, MqttQos . AT_MOST_ONCE , received)
25+ def publishResult = publish(publisher, subscriberId, MqttQos . AT_MOST_ONCE )
26+
27+ Thread . sleep(100 )
3928 then :
4029 noExceptionThrown()
4130
@@ -46,12 +35,10 @@ class ConnectSubscribePublishTest extends IntegrationSpecification {
4635 publishResult != null
4736 publishResult. publish. qos == MqttQos . AT_MOST_ONCE
4837 publishResult. publish. type == Mqtt5MessageType . PUBLISH
49- publishResult. publish. topic. levels. join(" /" ) == topicFilter
5038
5139 received. get() != null
5240 received. get(). qos == MqttQos . AT_MOST_ONCE
5341 received. get(). type == Mqtt5MessageType . PUBLISH
54- received. get(). topic. levels. join(" /" ) == topicFilter
5542 cleanup :
5643 subscriber. disconnect()
5744 publisher. disconnect()
@@ -61,28 +48,17 @@ class ConnectSubscribePublishTest extends IntegrationSpecification {
6148 given :
6249 def received = new AtomicReference<Mqtt5Publish > ()
6350 def subscriber = buildClient()
51+ def subscriberId = subscriber. getConfig(). clientIdentifier. toString()
6452 def publisher = buildClient()
6553 when :
6654
6755 subscriber. connect(). join()
6856 publisher. connect(). join()
69-
70- def subscribeResult = subscriber. subscribeWith()
71- .topicFilter(topicFilter)
72- .qos(MqttQos . AT_LEAST_ONCE )
73- .callback({ publish -> received. set(publish) })
74- .send()
75- .join()
76-
77- def publishResult = publisher. publishWith()
78- .topic(topicFilter)
79- .qos(MqttQos . AT_LEAST_ONCE )
80- .payload(publishPayload)
81- .payloadFormatIndicator(Mqtt5PayloadFormatIndicator . UTF_8 )
82- .send()
83- .join()
84-
85- Thread . sleep(500 )
57+
58+ def subscribeResult = subscribe(subscriber, subscriberId, MqttQos . AT_LEAST_ONCE , received)
59+ def publishResult = publish(publisher, subscriberId, MqttQos . AT_LEAST_ONCE )
60+
61+ Thread . sleep(100 )
8662 then :
8763 noExceptionThrown()
8864
@@ -93,12 +69,10 @@ class ConnectSubscribePublishTest extends IntegrationSpecification {
9369 publishResult != null
9470 publishResult. publish. qos == MqttQos . AT_LEAST_ONCE
9571 publishResult. publish. type == Mqtt5MessageType . PUBLISH
96- publishResult. publish. topic. levels. join(" /" ) == topicFilter
9772
9873 received. get() != null
9974 received. get(). qos == MqttQos . AT_LEAST_ONCE
10075 received. get(). type == Mqtt5MessageType . PUBLISH
101- received. get(). topic. levels. join(" /" ) == topicFilter
10276 cleanup :
10377 subscriber. disconnect(). join()
10478 publisher. disconnect(). join()
@@ -108,28 +82,17 @@ class ConnectSubscribePublishTest extends IntegrationSpecification {
10882 given :
10983 def received = new AtomicReference<Mqtt5Publish > ()
11084 def subscriber = buildClient()
85+ def subscriberId = subscriber. getConfig(). clientIdentifier. toString()
11186 def publisher = buildClient()
11287 when :
11388
11489 subscriber. connect(). join()
11590 publisher. connect(). join()
91+
92+ def subscribeResult = subscribe(subscriber, subscriberId, MqttQos . EXACTLY_ONCE , received)
93+ def publishResult = publish(publisher, subscriberId, MqttQos . EXACTLY_ONCE )
11694
117- def subscribeResult = subscriber. subscribeWith()
118- .topicFilter(topicFilter)
119- .qos(MqttQos . EXACTLY_ONCE )
120- .callback({ publish -> received. set(publish) })
121- .send()
122- .join()
123-
124- def publishResult = publisher. publishWith()
125- .topic(topicFilter)
126- .qos(MqttQos . EXACTLY_ONCE )
127- .payload(publishPayload)
128- .payloadFormatIndicator(Mqtt5PayloadFormatIndicator . UTF_8 )
129- .send()
130- .join()
131-
132- Thread . sleep(500 )
95+ Thread . sleep(100 )
13396 then :
13497 noExceptionThrown()
13598
@@ -140,14 +103,37 @@ class ConnectSubscribePublishTest extends IntegrationSpecification {
140103 publishResult != null
141104 publishResult. publish. qos == MqttQos . EXACTLY_ONCE
142105 publishResult. publish. type == Mqtt5MessageType . PUBLISH
143- publishResult. publish. topic. levels. join(" /" ) == topicFilter
144106
145107 received. get() != null
146108 received. get(). qos == MqttQos . EXACTLY_ONCE
147109 received. get(). type == Mqtt5MessageType . PUBLISH
148- received. get(). topic. levels. join(" /" ) == topicFilter
149110 cleanup :
150111 subscriber. disconnect()
151112 publisher. disconnect()
152113 }
114+
115+ def publish (Mqtt5AsyncClient publisher , String subscriberId , MqttQos qos ) {
116+ return publisher. publishWith()
117+ .topic(" test/$subscriberId " )
118+ .qos(qos)
119+ .payload(publishPayload)
120+ .payloadFormatIndicator(Mqtt5PayloadFormatIndicator . UTF_8 )
121+ .send()
122+ .join()
123+ }
124+
125+ def subscribe (
126+ Mqtt5AsyncClient subscriber ,
127+ String subscriberId ,
128+ MqttQos qos ,
129+ AtomicReference<Mqtt5Publish > received
130+ ) {
131+ return subscriber. subscribeWith()
132+ .topicFilter(" test/$subscriberId " )
133+ .qos(qos)
134+ .callback({ publish -> received. set(publish) })
135+ .send()
136+ .join()
137+ }
138+
153139}
0 commit comments