Debian Codegraph @

test/test_mqtt5to3_adapter.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 from concurrent.futures import Future
5 from awscrt import mqtt5, io, http, exceptions
6 from awscrt.mqtt import Connection, ConnectReturnCode, OnConnectionSuccessData, OnConnectionFailureData, OnConnectionClosedData, QoS
7 from test import test_retry_wrapper, NativeResourceTest
8 from test.test_mqtt5 import Mqtt5TestCallbacks, _get_env_variable, create_client_id
9 import unittest
10 import uuid
11
12
13 """
14 # Environmental variables used by mqtt5 bindings tests
15
16 # AWS_TEST_MQTT5_DIRECT_MQTT_HOST - host to connect to in direct mqtt tests
17 # AWS_TEST_MQTT5_DIRECT_MQTT_PORT - port to connect to in direct mqtt tests
18 # AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_HOST - host to connect to in direct mqtt basic authentication tests
19 # AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_PORT - port to connect to in direct mqtt basic authentication tests
20 # AWS_TEST_MQTT5_DIRECT_MQTT_TLS_HOST - host to connect to in direct mqtt tls tests
21 # AWS_TEST_MQTT5_DIRECT_MQTT_TLS_PORT - port to connect to in direct mqtt tls tests
22 # AWS_TEST_MQTT5_WS_MQTT_HOST - host to connect to in websocket tests
23 # AWS_TEST_MQTT5_WS_MQTT_PORT - port to connect to in websocket tests
24 # AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_HOST - host to connect to in websocket basic authentication tests
25 # AWS_TEST_MQTT5_WS_MQTT_BASIC_AUTH_PORT - port to connect to in websocket basic authentication tests
26 # AWS_TEST_MQTT5_WS_MQTT_TLS_HOST - host to connect to in websocket tls tests
27 # AWS_TEST_MQTT5_WS_MQTT_TLS_PORT - port to connect to in websocket tls tests
28 # AWS_TEST_MQTT5_IOT_CORE_HOST - host to connect to in MTLS tests
29 # AWS_TEST_MQTT5_BASIC_AUTH_USERNAME - username to use in basic authentication tests
30 # AWS_TEST_MQTT5_BASIC_AUTH_PASSWORD - password to use in basic authentication tests
31 # AWS_TEST_MQTT5_PROXY_HOST - name of http proxy host to use in proxy-based tests
32 # AWS_TEST_MQTT5_PROXY_PORT - port of http proxy host to use in proxy-based tests
33 # AWS_TEST_MQTT5_CERTIFICATE_FILE - certificate file path
34 # AWS_TEST_MQTT5_KEY_FILE - private key file path
35 # AWS_TEST_MQTT5_IOT_KEY_PATH - private key file path for MTLS tests
36 # AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH - certificate file path for MTLS tests
37
38 """
39
40 TIMEOUT = 100.0
41
42
43 class Mqtt311TestCallbacks():
44 def __init__(self):
45 self.last_exception = None
46
47 self.future_connection_success = Future()
48 self.future_connection_failure = Future()
49 self.future_resumed = Future()
50 self.future_interrupted = Future()
51 self.future_closed = Future()
52 self.future_message_received = Future()
53 self.received_message = 0
54
55 def on_connection_interrupted(self, connection: Connection, error: exceptions.AwsCrtError):
56 if self.future_interrupted:
57 self.future_interrupted.set_result(error)
58 self.future_connection_success = self._reset_future(self.future_connection_success)
59
60 def on_connection_resumed(self, connection: Connection, return_code: ConnectReturnCode, session_present: bool):
61 if self.future_resumed:
62 self.future_resumed.set_result({'return_code': return_code, "session_present": session_present})
63
64 def on_connection_success(self, connection: Connection, callback_data: OnConnectionSuccessData):
65 if self.future_connection_success:
66 self.future_connection_success.set_result(
67 {'return_code': callback_data.return_code, "session_present": callback_data.session_present})
68
69 def on_connection_failure(self, connection: Connection, callback_data: OnConnectionFailureData):
70 if self.future_connection_failure:
71 self.future_connection_failure.set_result({'error': callback_data.error})
72
73 def on_connection_closed(self, connection: Connection, callback_data: OnConnectionClosedData):
74 if self.future_closed:
75 self.future_closed.set_result({})
76
77 def on_message(self, **kwargs):
78 self.received_message += 1
79 if self.future_message_received:
80 self.future_message_received.set_result(kwargs)
81
82 def _reset_future(self, future: Future):
83 if future.done():
84 return Future()
85 return future
86
87
88 class Mqtt5to3AdapterTest(NativeResourceTest):
89 TEST_MSG = 'NOTICE ME!'.encode('utf8')
90
91 def _create_client(
92 self,
93 client_options: mqtt5.ClientOptions = None,
94 callbacks: Mqtt5TestCallbacks = None):
95
96 default_host = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST")
97 if client_options is None:
98 client_options = mqtt5.ClientOptions(
99 host_name=default_host,
100 port=8883
101 )
102 if client_options.connect_options is None:
103 client_options.connect_options = mqtt5.ConnectPacket()
104 client_options.connect_options.client_id = create_client_id()
105
106 if callbacks is not None:
107 client_options.on_publish_callback_fn = callbacks.on_publish_received
108 client_options.on_lifecycle_event_stopped_fn = callbacks.on_lifecycle_stopped
109 client_options.on_lifecycle_event_attempting_connect_fn = callbacks.on_lifecycle_attempting_connect
110 client_options.on_lifecycle_event_connection_success_fn = callbacks.on_lifecycle_connection_success
111 client_options.on_lifecycle_event_connection_failure_fn = callbacks.on_lifecycle_connection_failure
112 client_options.on_lifecycle_event_disconnection_fn = callbacks.on_lifecycle_disconnection
113
114 client = mqtt5.Client(client_options)
115 return client
116
117 def _create_connection(self,
118 client: mqtt5.Client,
119 mqtt3_callbacks: Mqtt311TestCallbacks = None):
120 connection = client.new_connection(on_connection_closed=mqtt3_callbacks.on_connection_closed,
121 on_connection_failure=mqtt3_callbacks.on_connection_failure,
122 on_connection_success=mqtt3_callbacks.on_connection_success,
123 on_connection_resumed=mqtt3_callbacks.on_connection_resumed,
124 on_connection_interrupted=mqtt3_callbacks.on_connection_interrupted,)
125 return connection
126
127 # # ==============================================================
128 # # MQTT5 CLIENT SETUP
129 # # ==============================================================
130
131 def _setup_direct_connect_minimum(self):
132 input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_HOST")
133 input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_PORT"))
134
135 client_options = mqtt5.ClientOptions(
136 host_name=input_host_name,
137 port=input_port
138 )
139 callbacks = Mqtt5TestCallbacks()
140 return self._create_client(client_options=client_options, callbacks=callbacks), callbacks
141
142 def _setup_direct_connect_basic_auth(self):
143 input_username = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_USERNAME")
144 input_password = _get_env_variable("AWS_TEST_MQTT5_BASIC_AUTH_PASSWORD")
145 input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_HOST")
146 input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_BASIC_AUTH_PORT"))
147
148 connect_options = mqtt5.ConnectPacket(
149 client_id=create_client_id(),
150 username=input_username,
151 password=input_password
152 )
153 client_options = mqtt5.ClientOptions(
154 host_name=input_host_name,
155 port=input_port,
156 connect_options=connect_options
157 )
158 callbacks = Mqtt5TestCallbacks()
159 return self._create_client(client_options=client_options, callbacks=callbacks), callbacks
160
161 def _setup_direct_connect_mutual_tls(self):
162 input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST")
163 input_cert = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_CERT")
164 input_key = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_RSA_KEY")
165
166 client_options = mqtt5.ClientOptions(
167 host_name=input_host_name,
168 port=8883
169 )
170 tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(
171 input_cert,
172 input_key
173 )
174 client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options)
175
176 callbacks = Mqtt5TestCallbacks()
177 return self._create_client(client_options=client_options, callbacks=callbacks), callbacks
178
179 # ==============================================================
180 # WEBSOCKET CONNECT TEST SETUP
181 # ==============================================================
182
183 def _setup_websocket_connect_minimum(self):
184 input_host_name = _get_env_variable("AWS_TEST_MQTT5_WS_MQTT_HOST")
185 input_port = int(_get_env_variable("AWS_TEST_MQTT5_WS_MQTT_PORT"))
186
187 client_options = mqtt5.ClientOptions(
188 host_name=input_host_name,
189 port=input_port
190 )
191 callbacks = Mqtt5TestCallbacks()
192 client_options.websocket_handshake_transform = callbacks.ws_handshake_transform
193
194 return self._create_client(client_options=client_options, callbacks=callbacks), callbacks
195
196 def _setup_websocket_connect_http_proxy_tls(self):
197 input_host_name = _get_env_variable("AWS_TEST_MQTT5_WS_MQTT_TLS_HOST")
198 input_port = int(_get_env_variable("AWS_TEST_MQTT5_WS_MQTT_TLS_PORT"))
199 input_proxy_host = _get_env_variable("AWS_TEST_MQTT5_PROXY_HOST")
200 input_proxy_port = int(_get_env_variable("AWS_TEST_MQTT5_PROXY_PORT"))
201
202 client_options = mqtt5.ClientOptions(
203 host_name=input_host_name,
204 port=input_port
205 )
206 tls_ctx_options = io.TlsContextOptions()
207 tls_ctx_options.verify_peer = False
208 client_options.tls_ctx = io.ClientTlsContext(tls_ctx_options)
209
210 http_proxy_options = http.HttpProxyOptions(
211 host_name=input_proxy_host,
212 port=input_proxy_port
213 )
214 http_proxy_options.connection_type = http.HttpProxyConnectionType.Tunneling
215 http_proxy_options.auth_type = http.HttpProxyAuthenticationType.Nothing
216 client_options.http_proxy_options = http_proxy_options
217
218 callbacks = Mqtt5TestCallbacks()
219 client_options.websocket_handshake_transform = callbacks.ws_handshake_transform
220
221 return self._create_client(client_options=client_options, callbacks=callbacks), callbacks
222
223 # ==============================================================
224 # CONNECTION TEST HELPER FUNCTIONS
225 # ==============================================================
226
227 def _test_with_mqtt3_connect(self, setup_client: callable):
228 client, callbacks = setup_client()
229 connection = client.new_connection()
230 connection.connect().result(TIMEOUT)
231 connection.disconnect().result(TIMEOUT)
232
233 def _test_with_mqtt5_connect(self, setup_client: callable):
234 client, callbacks = setup_client()
235 connection = client.new_connection()
236 client.start()
237 callbacks.future_connection_success.result(TIMEOUT)
238 client.stop()
239 callbacks.future_stopped.result(TIMEOUT)
240
241 # ==============================================================
242 # CREATION TEST CASES
243 # ==============================================================
244
245 def test_client_creation_minimum(self):
246 client = self._create_client()
247 connection = client.new_connection()
248
249 def test_client_creation_maximum(self):
250 input_host_name = _get_env_variable("AWS_TEST_MQTT5_IOT_CORE_HOST")
251
252 user_properties = []
253 user_properties.append(mqtt5.UserProperty(name="name1", value="value1"))
254 user_properties.append(mqtt5.UserProperty(name="name2", value="value2"))
255
256 publish_packet = mqtt5.PublishPacket(
257 payload="TEST_PAYLOAD",
258 qos=mqtt5.QoS.AT_LEAST_ONCE,
259 retain=False,
260 topic="TEST_TOPIC",
261 payload_format_indicator=mqtt5.PayloadFormatIndicator.AWS_MQTT5_PFI_UTF8,
262 message_expiry_interval_sec=10,
263 topic_alias=1,
264 response_topic="TEST_RESPONSE_TOPIC",
265 correlation_data="TEST_CORRELATION_DATA",
266 content_type="TEST_CONTENT_TYPE",
267 user_properties=user_properties
268 )
269
270 connect_options = mqtt5.ConnectPacket(
271 keep_alive_interval_sec=10,
272 client_id=create_client_id(),
273 username="USERNAME",
274 password="PASSWORD",
275 session_expiry_interval_sec=100,
276 request_response_information=1,
277 request_problem_information=1,
278 receive_maximum=1000,
279 maximum_packet_size=10000,
280 will_delay_interval_sec=1000,
281 will=publish_packet,
282 user_properties=user_properties
283 )
284 client_options = mqtt5.ClientOptions(
285 host_name=input_host_name,
286 port=8883,
287 connect_options=connect_options,
288 session_behavior=mqtt5.ClientSessionBehaviorType.CLEAN,
289 extended_validation_and_flow_control_options=mqtt5.ExtendedValidationAndFlowControlOptions.AWS_IOT_CORE_DEFAULTS,
290 offline_queue_behavior=mqtt5.ClientOperationQueueBehaviorType.FAIL_ALL_ON_DISCONNECT,
291 retry_jitter_mode=mqtt5.ExponentialBackoffJitterMode.DECORRELATED,
292 min_reconnect_delay_ms=100,
293 max_reconnect_delay_ms=50000,
294 min_connected_time_to_reset_reconnect_delay_ms=1000,
295 ping_timeout_ms=1000,
296 connack_timeout_ms=1000,
297 ack_timeout_sec=100)
298 client = self._create_client(client_options=client_options)
299 connection = client.new_connection()
300
301 # ==============================================================
302 # CONNECT THROUGH MQTT311 INTERFACE TEST CASES
303 # ==============================================================
304 def _test_direct_connect_through_mqtt311_minimum(self):
305 self._test_with_mqtt3_connect(self._setup_direct_connect_minimum)
306
307 def test_direct_connect_through_mqtt311_minimum(self):
308 test_retry_wrapper(self._test_direct_connect_through_mqtt311_minimum)
309
310 # def test_direct_connect_through_mqtt311_basic_auth(self):
311 # self._test_with_mqtt3_connect(self._setup_direct_connect_basic_auth)
312
313 def _test_direct_connect_through_mqtt311_mutual_tls(self):
314 self._test_with_mqtt3_connect(self._setup_direct_connect_mutual_tls)
315
316 def test_direct_connect_through_mqtt311_mutual_tls(self):
317 test_retry_wrapper(self._test_direct_connect_through_mqtt311_mutual_tls)
318
319 # def test_direct_connect_through_mqtt311_websocket_minimum(self):
320 # self._test_with_mqtt3_connect(self._setup_websocket_connect_minimum)
321
322 # def test_direct_connect_through_mqtt311_websocket_http_proxy_tls(self):
323 # self._test_with_mqtt3_connect(self._setup_websocket_connect_http_proxy_tls)
324
325 # ==============================================================
326 # CONNECT THROUGH MQTT5 INTERFACE TEST CASES
327 # ==============================================================
328
329 def _test_direct_connect_through_mqtt5_minimum(self):
330 self._test_with_mqtt5_connect(self._setup_direct_connect_minimum)
331
332 def test_direct_connect_through_mqtt5_minimum(self):
333 test_retry_wrapper(self._test_direct_connect_through_mqtt5_minimum)
334
335 # def test_direct_connect_through_mqtt5_basic_auth(self):
336 # self._test_with_mqtt5_connect(self._setup_direct_connect_basic_auth)
337
338 def _test_direct_connect_through_mqtt5_mutual_tls(self):
339 self._test_with_mqtt5_connect(self._setup_direct_connect_mutual_tls)
340
341 def test_direct_connect_through_mqtt5_mutual_tls(self):
342 test_retry_wrapper(self._test_direct_connect_through_mqtt5_mutual_tls)
343
344 # def test_direct_connect_through_mqtt5_websocket_minimum(self):
345 # self._test_with_mqtt5_connect(self._setup_websocket_connect_minimum)
346
347 # def test_direct_connect_through_mqtt5_websocket_http_proxy_tls(self):
348 # self._test_with_mqtt5_connect(self._setup_websocket_connect_http_proxy_tls)
349
350 # ==============================================================
351 # OPERATION TEST CASES
352 # ==============================================================
353
354 def _test_operation_sub_unsub(self):
355 TEST_TOPIC = '/test/topic/adapter' + str(uuid.uuid4())
356
357 client, mqtt5_callbacks = self._setup_direct_connect_mutual_tls()
358 mqtt311_callbacks = Mqtt311TestCallbacks()
359 connection = self._create_connection(client, mqtt311_callbacks)
360
361 connection.connect().result(TIMEOUT)
362
363 # subscribe
364 subscribed, packet_id = connection.subscribe(TEST_TOPIC, QoS.AT_LEAST_ONCE, mqtt311_callbacks.on_message)
365 suback = subscribed.result(TIMEOUT)
366 self.assertEqual(packet_id, suback['packet_id'])
367 self.assertEqual(TEST_TOPIC, suback['topic'])
368 self.assertIs(QoS.AT_LEAST_ONCE, suback['qos'])
369
370 # publish
371 published, packet_id = connection.publish(TEST_TOPIC, self.TEST_MSG, QoS.AT_LEAST_ONCE)
372 puback = published.result(TIMEOUT)
373 self.assertEqual(packet_id, puback['packet_id'])
374
375 # receive message
376 rcv = mqtt311_callbacks.future_message_received.result(TIMEOUT)
377 self.assertEqual(TEST_TOPIC, rcv['topic'])
378 self.assertEqual(self.TEST_MSG, rcv['payload'])
379 self.assertFalse(rcv['dup'])
380 self.assertEqual(QoS.AT_LEAST_ONCE, rcv['qos'])
381 self.assertFalse(rcv['retain'])
382
383 # unsubscribe
384 unsubscribed, packet_id = connection.unsubscribe(TEST_TOPIC)
385 unsuback = unsubscribed.result(TIMEOUT)
386 self.assertEqual(packet_id, unsuback['packet_id'])
387
388 # publish
389 published, packet_id = connection.publish(TEST_TOPIC, self.TEST_MSG, QoS.AT_LEAST_ONCE)
390 puback = published.result(TIMEOUT)
391 self.assertEqual(packet_id, puback['packet_id'])
392
393 self.assertEqual(mqtt311_callbacks.received_message, 1)
394 self.assertEqual(mqtt5_callbacks.on_publish_received_counter, 1)
395
396 connection.disconnect().result(TIMEOUT)
397
398 def test_operation_sub_unsub(self):
399 test_retry_wrapper(self._test_operation_sub_unsub)
400
401 def _test_operation_null_ack(self):
402 TEST_TOPIC = '/test/topic/adapter' + str(uuid.uuid4())
403 exception_occurred = False
404
405 client, mqtt5_callbacks = self._setup_direct_connect_mutual_tls()
406 mqtt311_callbacks = Mqtt311TestCallbacks()
407 connection = self._create_connection(client, mqtt311_callbacks)
408
409 # publish an offline packet so that the publish operation would be incomplete
410 published, packet_id = connection.publish(TEST_TOPIC, self.TEST_MSG, QoS.AT_LEAST_ONCE)
411
412 # Manually destroyed the connection so that the incomplete publish operation should fail with exception
413 del connection
414 try:
415 published.result(TIMEOUT)
416 except TimeoutError:
417 # Directly failed the test if the result time out
418 self.assertTrue(False, "Operation Time out. The connection does not fail the callback on destroy. ")
419 except Exception:
420 exception_occurred = True
421
422 assert (exception_occurred)
423
424 def test_operation_null_ack(self):
425 test_retry_wrapper(self._test_operation_null_ack)
426
427 # ==============================================================
428 # MQTT311 CALLBACK TEST CASES
429 # ==============================================================
430 def _test_connection_success_callback(self):
431 client, _ = self._setup_direct_connect_minimum()
432 mqtt311_callbacks = Mqtt311TestCallbacks()
433 connection = self._create_connection(client, mqtt311_callbacks)
434
435 connection.connect().result(TIMEOUT)
436 mqtt311_callbacks.future_connection_success.result(TIMEOUT)
437 connection.disconnect().result(TIMEOUT)
438
439 def test_connection_success_callback(self):
440 test_retry_wrapper(self._test_connection_success_callback)
441
442 def _test_connection_failure_callback(self):
443 client_options = mqtt5.ClientOptions(
444 host_name="badhost",
445 port=1883
446 )
447 callbacks = Mqtt5TestCallbacks()
448 client = self._create_client(client_options=client_options, callbacks=callbacks)
449 mqtt311_callbacks = Mqtt311TestCallbacks()
450 connection = self._create_connection(client, mqtt311_callbacks)
451
452 try:
453 connection.connect().result(TIMEOUT)
454 except Exception:
455 exception_occurred = True
456 self.assertTrue(exception_occurred, "Exception did not occur when connecting with invalid arguments!")
457
458 failure_data = mqtt311_callbacks.future_connection_failure.result(TIMEOUT)
459 self.assertTrue(failure_data['error'] is not None)
460
461 def test_connection_failure_callback(self):
462 test_retry_wrapper(self._test_connection_failure_callback)
463
464 def _test_connection_interrupted_and_resumed_callback(self):
465 input_host_name = _get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_HOST")
466 input_port = int(_get_env_variable("AWS_TEST_MQTT5_DIRECT_MQTT_PORT"))
467
468 client_options = mqtt5.ClientOptions(
469 host_name=input_host_name,
470 port=input_port
471 )
472 client_options.connect_options = mqtt5.ConnectPacket()
473 client_options.connect_options.client_id = create_client_id()
474
475 client1 = self._create_client(client_options=client_options)
476 client2 = self._create_client(client_options=client_options)
477
478 mqtt311_callbacks1 = Mqtt311TestCallbacks()
479 mqtt311_callbacks2 = Mqtt311TestCallbacks()
480
481 # Create two connection with the same client id
482 connection1 = self._create_connection(client1, mqtt311_callbacks1)
483 connection2 = self._create_connection(client2, mqtt311_callbacks2)
484
485 connection1.connect().result(TIMEOUT)
486
487 connection2.connect().result(TIMEOUT)
488
489 # with connection2 connected, connection1 should be interrupted
490 mqtt311_callbacks1.future_interrupted.result(TIMEOUT)
491 # The connection1 should automatically re-connected
492 mqtt311_callbacks1.future_resumed.result(TIMEOUT)
493
494 # At this point, connection2 should be interrupted
495 mqtt311_callbacks2.future_interrupted.result(TIMEOUT)
496
497 connection1.disconnect().result(TIMEOUT)
498 connection2.disconnect().result(TIMEOUT)
499
500 def test_connection_interrupted_and_resumed_callback(self):
501 test_retry_wrapper(self._test_connection_interrupted_and_resumed_callback)
502
503 # ==============================================================
504 # ADAPTER TEST CASES
505 # ==============================================================
506 def _test_multiple_adapters(self):
507 TEST_TOPIC1 = '/test/topic/adapter1' + str(uuid.uuid4())
508 TEST_TOPIC2 = '/test/topic/adapter2' + str(uuid.uuid4())
509 TEST_TOPIC3 = '/test/topic/adapter3' + str(uuid.uuid4())
510
511 client, mqtt5_callbacks = self._setup_direct_connect_mutual_tls()
512 mqtt311_callbacks1 = Mqtt311TestCallbacks()
513 connection1 = self._create_connection(client, mqtt3_callbacks=mqtt311_callbacks1)
514 mqtt311_callbacks2 = Mqtt311TestCallbacks()
515 connection2 = self._create_connection(client, mqtt3_callbacks=mqtt311_callbacks2)
516 mqtt311_callbacks3 = Mqtt311TestCallbacks()
517 connection3 = self._create_connection(client, mqtt3_callbacks=mqtt311_callbacks3)
518
519 client.start()
520 mqtt5_callbacks.future_connection_success.result(TIMEOUT)
521
522 # subscribe
523 subscribed, packet_id = connection1.subscribe(
524 TEST_TOPIC1, QoS.AT_LEAST_ONCE, mqtt311_callbacks1.on_message)
525 suback = subscribed.result(TIMEOUT)
526 subscribed, packet_id = connection2.subscribe(
527 TEST_TOPIC2, QoS.AT_LEAST_ONCE, mqtt311_callbacks2.on_message)
528 suback = subscribed.result(TIMEOUT)
529 subscribed, packet_id = connection3.subscribe(
530 TEST_TOPIC3, QoS.AT_LEAST_ONCE, mqtt311_callbacks3.on_message)
531 suback = subscribed.result(TIMEOUT)
532
533 # publish on topic1
534 publish_packet = mqtt5.PublishPacket(
535 payload=self.TEST_MSG,
536 topic=TEST_TOPIC1,
537 qos=mqtt5.QoS.AT_LEAST_ONCE)
538
539 client.publish(publish_packet=publish_packet).result(TIMEOUT)
540
541 # only connection1 should receive message
542 mqtt311_callbacks1.future_message_received.result(TIMEOUT)
543
544 self.assertEqual(mqtt311_callbacks1.received_message, 1)
545 self.assertEqual(mqtt311_callbacks2.received_message, 0)
546 self.assertEqual(mqtt311_callbacks3.received_message, 0)
547
548 # publish on topic2
549 publish_packet = mqtt5.PublishPacket(
550 payload=self.TEST_MSG,
551 topic=TEST_TOPIC2,
552 qos=mqtt5.QoS.AT_LEAST_ONCE)
553
554 client.publish(publish_packet=publish_packet).result(TIMEOUT)
555
556 # connection2 should receive message
557 mqtt311_callbacks2.future_message_received.result(TIMEOUT)
558
559 self.assertEqual(mqtt311_callbacks1.received_message, 1)
560 self.assertEqual(mqtt311_callbacks2.received_message, 1)
561 self.assertEqual(mqtt311_callbacks3.received_message, 0)
562
563 # publish on topic3
564 publish_packet = mqtt5.PublishPacket(
565 payload=self.TEST_MSG,
566 topic=TEST_TOPIC3,
567 qos=mqtt5.QoS.AT_LEAST_ONCE)
568
569 client.publish(publish_packet=publish_packet).result(TIMEOUT)
570
571 # connection3 should receive message
572 mqtt311_callbacks3.future_message_received.result(TIMEOUT)
573 self.assertEqual(mqtt311_callbacks1.received_message, 1)
574 self.assertEqual(mqtt311_callbacks2.received_message, 1)
575 self.assertEqual(mqtt311_callbacks3.received_message, 1)
576
577 client.stop()
578 mqtt5_callbacks.future_stopped.result(TIMEOUT)
579
580 def test_multiple_adapters(self):
581 test_retry_wrapper(self._test_multiple_adapters)
582
583
584 if __name__ == 'main':
585 unittest.main()