Skip to content

Commit 40053c2

Browse files
committed
add suback test
1 parent e939ae8 commit 40053c2

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

test/core/protocol/test_mqtt_core.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AWSIoTPythonSDK
22
from AWSIoTPythonSDK.core.protocol.mqtt_core import MqttCore
3+
from AWSIoTPythonSDK.core.protocol.mqtt_core import SubackPacket
34
from AWSIoTPythonSDK.core.protocol.internal.clients import InternalAsyncMqttClient
45
from AWSIoTPythonSDK.core.protocol.internal.clients import ClientStatusContainer
56
from AWSIoTPythonSDK.core.protocol.internal.clients import ClientStatus
@@ -20,6 +21,7 @@
2021
from AWSIoTPythonSDK.exception.AWSIoTExceptions import publishQueueFullException
2122
from AWSIoTPythonSDK.exception.AWSIoTExceptions import publishQueueDisabledException
2223
from AWSIoTPythonSDK.exception.AWSIoTExceptions import subscribeError
24+
from AWSIoTPythonSDK.exception.AWSIoTExceptions import subackError
2325
from AWSIoTPythonSDK.exception.AWSIoTExceptions import subscribeTimeoutException
2426
from AWSIoTPythonSDK.exception.AWSIoTExceptions import subscribeQueueFullException
2527
from AWSIoTPythonSDK.exception.AWSIoTExceptions import subscribeQueueDisabledException
@@ -29,6 +31,7 @@
2931
from AWSIoTPythonSDK.exception.AWSIoTExceptions import unsubscribeQueueDisabledException
3032
from AWSIoTPythonSDK.core.protocol.paho.client import MQTT_ERR_SUCCESS
3133
from AWSIoTPythonSDK.core.protocol.paho.client import MQTT_ERR_ERRNO
34+
from AWSIoTPythonSDK.core.protocol.paho.client import SUBACK_ERROR
3235
from AWSIoTPythonSDK.core.protocol.paho.client import MQTTv311
3336
from AWSIoTPythonSDK.core.protocol.internal.defaults import ALPN_PROTCOLS
3437
try:
@@ -61,6 +64,7 @@
6164
KEY_EXPECTED_QUEUE_APPEND_RESULT = "ExpectedQueueAppendResult"
6265
KEY_EXPECTED_REQUEST_MID_OVERRIDE = "ExpectedRequestMidOverride"
6366
KEY_EXPECTED_REQUEST_TIMEOUT = "ExpectedRequestTimeout"
67+
KEY_EXPECTED_ACK_RESULT = "ExpectedAckPacketResult"
6468
SUCCESS_RC_EXPECTED_VALUES = {
6569
KEY_EXPECTED_REQUEST_RC : DUMMY_SUCCESS_RC
6670
}
@@ -73,6 +77,10 @@
7377
NO_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+
7684
QUEUED_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

Comments
 (0)