@@ -180,4 +180,52 @@ function (string $topic, string $message, bool $retained) use ($subscriber, $sub
180
180
$ publisher ->disconnect ();
181
181
$ subscriber ->disconnect ();
182
182
}
183
+
184
+ public function test_shared_subscriptions_using_quality_of_service_0_work_as_intended (): void
185
+ {
186
+ $ subscriptionTopicFilter = '$share/test-shared-subscriptions/foo/+ ' ;
187
+ $ publishTopic = 'foo/bar ' ;
188
+
189
+ // We connect and subscribe to a topic using the first client with a shared subscription.
190
+ $ subscriber1 = new MqttClient ($ this ->mqttBrokerHost , $ this ->mqttBrokerPort , 'subscriber1 ' );
191
+ $ subscriber1 ->connect (null , true );
192
+
193
+ $ subscriber1 ->subscribe ($ subscriptionTopicFilter , function (string $ topic , string $ message , bool $ retained ) use ($ subscriber1 , $ publishTopic ) {
194
+ // By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
195
+ $ this ->assertEquals ($ publishTopic , $ topic );
196
+ $ this ->assertEquals ('hello world #1 ' , $ message );
197
+ $ this ->assertFalse ($ retained );
198
+
199
+ $ subscriber1 ->interrupt (); // This allows us to exit the test as soon as possible.
200
+ }, 0 );
201
+
202
+ // We connect and subscribe to a topic using the second client with a shared subscription.
203
+ $ subscriber2 = new MqttClient ($ this ->mqttBrokerHost , $ this ->mqttBrokerPort , 'subscriber2 ' );
204
+ $ subscriber2 ->connect (null , true );
205
+
206
+ $ subscriber2 ->subscribe ($ subscriptionTopicFilter , function (string $ topic , string $ message , bool $ retained ) use ($ subscriber2 , $ publishTopic ) {
207
+ // By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
208
+ $ this ->assertEquals ($ publishTopic , $ topic );
209
+ $ this ->assertEquals ('hello world #2 ' , $ message );
210
+ $ this ->assertFalse ($ retained );
211
+
212
+ $ subscriber2 ->interrupt (); // This allows us to exit the test as soon as possible.
213
+ }, 0 );
214
+
215
+ // We publish a message from a second client on the same topic.
216
+ $ publisher = new MqttClient ($ this ->mqttBrokerHost , $ this ->mqttBrokerPort , 'publisher ' );
217
+ $ publisher ->connect (null , true );
218
+
219
+ $ publisher ->publish ($ publishTopic , 'hello world #1 ' , 0 , false );
220
+ $ publisher ->publish ($ publishTopic , 'hello world #2 ' , 0 , false );
221
+
222
+ // Then we loop on the subscribers to (hopefully) receive the published messages.
223
+ $ subscriber1 ->loop (true );
224
+ $ subscriber2 ->loop (true );
225
+
226
+ // Finally, we disconnect for a graceful shutdown on the broker side.
227
+ $ publisher ->disconnect ();
228
+ $ subscriber1 ->disconnect ();
229
+ $ subscriber2 ->disconnect ();
230
+ }
183
231
}
0 commit comments