1
1
import AWSIoTPythonSDK
2
2
from AWSIoTPythonSDK .core .protocol .mqtt_core import MqttCore
3
+ from AWSIoTPythonSDK .core .protocol .mqtt_core import SubackPacket
3
4
from AWSIoTPythonSDK .core .protocol .internal .clients import InternalAsyncMqttClient
4
5
from AWSIoTPythonSDK .core .protocol .internal .clients import ClientStatusContainer
5
6
from AWSIoTPythonSDK .core .protocol .internal .clients import ClientStatus
20
21
from AWSIoTPythonSDK .exception .AWSIoTExceptions import publishQueueFullException
21
22
from AWSIoTPythonSDK .exception .AWSIoTExceptions import publishQueueDisabledException
22
23
from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeError
24
+ from AWSIoTPythonSDK .exception .AWSIoTExceptions import subackError
23
25
from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeTimeoutException
24
26
from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeQueueFullException
25
27
from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeQueueDisabledException
29
31
from AWSIoTPythonSDK .exception .AWSIoTExceptions import unsubscribeQueueDisabledException
30
32
from AWSIoTPythonSDK .core .protocol .paho .client import MQTT_ERR_SUCCESS
31
33
from AWSIoTPythonSDK .core .protocol .paho .client import MQTT_ERR_ERRNO
34
+ from AWSIoTPythonSDK .core .protocol .paho .client import SUBACK_ERROR
32
35
from AWSIoTPythonSDK .core .protocol .paho .client import MQTTv311
33
36
from AWSIoTPythonSDK .core .protocol .internal .defaults import ALPN_PROTCOLS
34
37
try :
61
64
KEY_EXPECTED_QUEUE_APPEND_RESULT = "ExpectedQueueAppendResult"
62
65
KEY_EXPECTED_REQUEST_MID_OVERRIDE = "ExpectedRequestMidOverride"
63
66
KEY_EXPECTED_REQUEST_TIMEOUT = "ExpectedRequestTimeout"
67
+ KEY_EXPECTED_ACK_RESULT = "ExpectedAckPacketResult"
64
68
SUCCESS_RC_EXPECTED_VALUES = {
65
69
KEY_EXPECTED_REQUEST_RC : DUMMY_SUCCESS_RC
66
70
}
73
77
NO_TIMEOUT_EXPECTED_VALUES = {
74
78
KEY_EXPECTED_REQUEST_TIMEOUT : False
75
79
}
80
+ ERROR_SUBACK_EXPECTED_VALUES = {
81
+ KEY_EXPECTED_ACK_RESULT : (SUBACK_ERROR , None )
82
+ }
83
+
76
84
QUEUED_EXPECTED_VALUES = {
77
85
KEY_EXPECTED_QUEUE_APPEND_RESULT : AppendResults .APPEND_SUCCESS
78
86
}
@@ -121,6 +129,9 @@ def setup_class(cls):
121
129
RequestTypes .SUBSCRIBE : subscribeError ,
122
130
RequestTypes .UNSUBSCRIBE : unsubscribeError
123
131
}
132
+ cls .ack_error = {
133
+ RequestTypes .SUBSCRIBE : subackError ,
134
+ }
124
135
cls .request_queue_full = {
125
136
RequestTypes .PUBLISH : publishQueueFullException ,
126
137
RequestTypes .SUBSCRIBE : subscribeQueueFullException ,
@@ -518,6 +529,9 @@ def test_subscribe_success(self):
518
529
519
530
def test_subscribe_timeout (self ):
520
531
self ._internal_test_sync_api_with (RequestTypes .SUBSCRIBE , TIMEOUT_EXPECTED_VALUES )
532
+
533
+ def test_subscribe_error_suback (self ):
534
+ self ._internal_test_sync_api_with (RequestTypes .SUBSCRIBE , ERROR_SUBACK_EXPECTED_VALUES )
521
535
522
536
def test_subscribe_queued (self ):
523
537
self ._internal_test_sync_api_with (RequestTypes .SUBSCRIBE , QUEUED_EXPECTED_VALUES )
@@ -547,6 +561,7 @@ def _internal_test_sync_api_with(self, request_type, expected_values):
547
561
expected_request_mid = expected_values .get (KEY_EXPECTED_REQUEST_MID_OVERRIDE )
548
562
expected_timeout = expected_values .get (KEY_EXPECTED_REQUEST_TIMEOUT )
549
563
expected_append_result = expected_values .get (KEY_EXPECTED_QUEUE_APPEND_RESULT )
564
+ expected_suback_result = expected_values .get (KEY_EXPECTED_ACK_RESULT )
550
565
551
566
if expected_request_mid is None :
552
567
expected_request_mid = DUMMY_REQUEST_MID
@@ -562,7 +577,16 @@ def _internal_test_sync_api_with(self, request_type, expected_values):
562
577
self .invoke_mqtt_core_sync_api [request_type ](self , message_callback )
563
578
else :
564
579
self .python_event_mock .wait .return_value = True
565
- assert self .invoke_mqtt_core_sync_api [request_type ](self , message_callback ) is True
580
+ if expected_suback_result is not None :
581
+ self ._use_mock_python_suback ()
582
+ # mock the suback with expected suback result
583
+ self .python_suback_mock .data = expected_suback_result
584
+ if expected_suback_result [0 ] == SUBACK_ERROR :
585
+ with pytest .raises (self .ack_error [request_type ]):
586
+ self .invoke_mqtt_core_sync_api [request_type ](self , message_callback )
587
+ self .python_suback_patcher .stop ()
588
+ else :
589
+ assert self .invoke_mqtt_core_sync_api [request_type ](self , message_callback ) is True
566
590
567
591
if expected_append_result is not None :
568
592
self .client_status_mock .get_status .return_value = ClientStatus .ABNORMAL_DISCONNECT
@@ -583,3 +607,10 @@ def _use_mock_python_event(self):
583
607
self .python_event_constructor = self .python_event_patcher .start ()
584
608
self .python_event_mock = MagicMock ()
585
609
self .python_event_constructor .return_value = self .python_event_mock
610
+
611
+ # Create a SubackPacket mock, which would mock the data in SubackPacket
612
+ def _use_mock_python_suback (self ):
613
+ self .python_suback_patcher = patch (PATCH_MODULE_LOCATION + "SubackPacket" , spec = SubackPacket )
614
+ self .python_suback_constructor = self .python_suback_patcher .start ()
615
+ self .python_suback_mock = MagicMock ()
616
+ self .python_suback_constructor .return_value = self .python_suback_mock
0 commit comments