11import AWSIoTPythonSDK
22from AWSIoTPythonSDK .core .protocol .mqtt_core import MqttCore
3+ from AWSIoTPythonSDK .core .protocol .mqtt_core import SubackPacket
34from AWSIoTPythonSDK .core .protocol .internal .clients import InternalAsyncMqttClient
45from AWSIoTPythonSDK .core .protocol .internal .clients import ClientStatusContainer
56from AWSIoTPythonSDK .core .protocol .internal .clients import ClientStatus
2021from AWSIoTPythonSDK .exception .AWSIoTExceptions import publishQueueFullException
2122from AWSIoTPythonSDK .exception .AWSIoTExceptions import publishQueueDisabledException
2223from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeError
24+ from AWSIoTPythonSDK .exception .AWSIoTExceptions import subackError
2325from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeTimeoutException
2426from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeQueueFullException
2527from AWSIoTPythonSDK .exception .AWSIoTExceptions import subscribeQueueDisabledException
2931from AWSIoTPythonSDK .exception .AWSIoTExceptions import unsubscribeQueueDisabledException
3032from AWSIoTPythonSDK .core .protocol .paho .client import MQTT_ERR_SUCCESS
3133from AWSIoTPythonSDK .core .protocol .paho .client import MQTT_ERR_ERRNO
34+ from AWSIoTPythonSDK .core .protocol .paho .client import SUBACK_ERROR
3235from AWSIoTPythonSDK .core .protocol .paho .client import MQTTv311
3336from AWSIoTPythonSDK .core .protocol .internal .defaults import ALPN_PROTCOLS
3437try :
6164KEY_EXPECTED_QUEUE_APPEND_RESULT = "ExpectedQueueAppendResult"
6265KEY_EXPECTED_REQUEST_MID_OVERRIDE = "ExpectedRequestMidOverride"
6366KEY_EXPECTED_REQUEST_TIMEOUT = "ExpectedRequestTimeout"
67+ KEY_EXPECTED_ACK_RESULT = "ExpectedAckPacketResult"
6468SUCCESS_RC_EXPECTED_VALUES = {
6569 KEY_EXPECTED_REQUEST_RC : DUMMY_SUCCESS_RC
6670}
7377NO_TIMEOUT_EXPECTED_VALUES = {
7478 KEY_EXPECTED_REQUEST_TIMEOUT : False
7579}
80+ ERROR_SUBACK_EXPECTED_VALUES = {
81+ KEY_EXPECTED_ACK_RESULT : (SUBACK_ERROR , None )
82+ }
83+
7684QUEUED_EXPECTED_VALUES = {
7785 KEY_EXPECTED_QUEUE_APPEND_RESULT : AppendResults .APPEND_SUCCESS
7886}
@@ -121,6 +129,9 @@ def setup_class(cls):
121129 RequestTypes .SUBSCRIBE : subscribeError ,
122130 RequestTypes .UNSUBSCRIBE : unsubscribeError
123131 }
132+ cls .ack_error = {
133+ RequestTypes .SUBSCRIBE : subackError ,
134+ }
124135 cls .request_queue_full = {
125136 RequestTypes .PUBLISH : publishQueueFullException ,
126137 RequestTypes .SUBSCRIBE : subscribeQueueFullException ,
@@ -518,6 +529,9 @@ def test_subscribe_success(self):
518529
519530 def test_subscribe_timeout (self ):
520531 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 )
521535
522536 def test_subscribe_queued (self ):
523537 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):
547561 expected_request_mid = expected_values .get (KEY_EXPECTED_REQUEST_MID_OVERRIDE )
548562 expected_timeout = expected_values .get (KEY_EXPECTED_REQUEST_TIMEOUT )
549563 expected_append_result = expected_values .get (KEY_EXPECTED_QUEUE_APPEND_RESULT )
564+ expected_suback_result = expected_values .get (KEY_EXPECTED_ACK_RESULT )
550565
551566 if expected_request_mid is None :
552567 expected_request_mid = DUMMY_REQUEST_MID
@@ -562,7 +577,16 @@ def _internal_test_sync_api_with(self, request_type, expected_values):
562577 self .invoke_mqtt_core_sync_api [request_type ](self , message_callback )
563578 else :
564579 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
566590
567591 if expected_append_result is not None :
568592 self .client_status_mock .get_status .return_value = ClientStatus .ABNORMAL_DISCONNECT
@@ -583,3 +607,10 @@ def _use_mock_python_event(self):
583607 self .python_event_constructor = self .python_event_patcher .start ()
584608 self .python_event_mock = MagicMock ()
585609 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