|
| 1 | +# This integration test verifies the functionality in the Python core of Yun/Python SDK |
| 2 | +# for auto-reconnect and auto-resubscribe. |
| 3 | +# It starts two threads using two different connections to AWS IoT: |
| 4 | +# Thread A publishes 10 messages to topicB first, then quiet for a while, and finally |
| 5 | +# publishes another 10 messages to topicB. |
| 6 | +# Thread B subscribes to topicB and waits to receive messages. Once it receives the first |
| 7 | +# 10 messages. It simulates a network error, disconnecting from the broker. In a short time, |
| 8 | +# it should automatically reconnect and resubscribe to the previous topic and be able to |
| 9 | +# receive the next 10 messages from thread A. |
| 10 | +# Because of auto-reconnect/resubscribe, thread B should be able to receive all of the |
| 11 | +# messages from topicB published by thread A without calling subscribe again in user code |
| 12 | +# explicitly. |
| 13 | + |
| 14 | + |
| 15 | +import random |
| 16 | +import string |
| 17 | +import sys |
| 18 | +import time |
| 19 | +sys.path.insert(0, "./test-integration/IntegrationTests/TestToolLibrary") |
| 20 | +sys.path.insert(0, "./test-integration/IntegrationTests/TestToolLibrary/SDKPackage") |
| 21 | + |
| 22 | +import TestToolLibrary.checkInManager as checkInManager |
| 23 | +import TestToolLibrary.MQTTClientManager as MQTTClientManager |
| 24 | +from TestToolLibrary import simpleThreadManager |
| 25 | +from TestToolLibrary.SDKPackage.AWSIoTPythonSDK.exception.AWSIoTExceptions import publishError |
| 26 | +from TestToolLibrary.SDKPackage.AWSIoTPythonSDK.exception.AWSIoTExceptions import subscribeError |
| 27 | +from TestToolLibrary.SDKPackage.AWSIoTPythonSDK.exception.AWSIoTExceptions import subscribeTimeoutException |
| 28 | +from TestToolLibrary.skip import skip_when_match |
| 29 | +from TestToolLibrary.skip import ModeIsALPN |
| 30 | +from TestToolLibrary.skip import Python2VersionLowerThan |
| 31 | +from TestToolLibrary.skip import Python3VersionLowerThan |
| 32 | + |
| 33 | +CLIENT_ID_PUB = "integrationTestMQTT_ClientPub" + "".join(random.choice(string.ascii_lowercase) for i in range(4)) |
| 34 | +CLIENT_ID_SUB = "integrationTestMQTT_ClientSub" + "".join(random.choice(string.ascii_lowercase) for i in range(4)) |
| 35 | + |
| 36 | +# Callback unit |
| 37 | +class callbackUnit: |
| 38 | + def __init__(self): |
| 39 | + self._internalSet = set() |
| 40 | + |
| 41 | + # Callback fro clientSub |
| 42 | + def messageCallback(self, client, userdata, message): |
| 43 | + print("Received a new message: " + str(message.payload)) |
| 44 | + self._internalSet.add(message.payload.decode('utf-8')) |
| 45 | + |
| 46 | + def getInternalSet(self): |
| 47 | + return self._internalSet |
| 48 | + |
| 49 | + |
| 50 | +# Simulate a network error |
| 51 | +def manualNetworkError(srcPyMQTTCore): |
| 52 | + # Ensure we close the socket |
| 53 | + if srcPyMQTTCore._internal_async_client._paho_client._sock: |
| 54 | + srcPyMQTTCore._internal_async_client._paho_client._sock.close() |
| 55 | + srcPyMQTTCore._internal_async_client._paho_client._sock = None |
| 56 | + if srcPyMQTTCore._internal_async_client._paho_client._ssl: |
| 57 | + srcPyMQTTCore._internal_async_client._paho_client._ssl.close() |
| 58 | + srcPyMQTTCore._internal_async_client._paho_client._ssl = None |
| 59 | + # Fake that we have detected the disconnection |
| 60 | + srcPyMQTTCore._internal_async_client._paho_client.on_disconnect(None, None, 0) |
| 61 | + |
| 62 | + |
| 63 | +# runFunctionUnit |
| 64 | +class runFunctionUnit(): |
| 65 | + def __init__(self): |
| 66 | + self._messagesPublished = set() |
| 67 | + self._topicB = "topicB/" + "".join(random.choice(string.ascii_lowercase) for i in range(4)) |
| 68 | + |
| 69 | + # ThreadA runtime function: |
| 70 | + # 1. Publish 10 messages to topicB. |
| 71 | + # 2. Take a nap: 20 sec |
| 72 | + # 3. Publish another 10 messages to topicB. |
| 73 | + def threadARuntime(self, pyCoreClient): |
| 74 | + time.sleep(3) # Ensure a valid subscription |
| 75 | + messageCount = 0 |
| 76 | + # First 10 messages |
| 77 | + while messageCount < 10: |
| 78 | + try: |
| 79 | + pyCoreClient.publish(self._topicB, str(messageCount), 1, False) |
| 80 | + self._messagesPublished.add(str(messageCount)) |
| 81 | + except publishError: |
| 82 | + print("Publish error!") |
| 83 | + except Exception as e: |
| 84 | + print("Unknown exception!") |
| 85 | + print("Type: " + str(type(e))) |
| 86 | + print("Message: " + str(e.message)) |
| 87 | + messageCount += 1 |
| 88 | + time.sleep(0.5) # TPS = 2 |
| 89 | + # Take a nap |
| 90 | + time.sleep(20) |
| 91 | + # Second 10 messages |
| 92 | + while messageCount < 20: |
| 93 | + try: |
| 94 | + pyCoreClient.publish(self._topicB, str(messageCount), 1, False) |
| 95 | + self._messagesPublished.add(str(messageCount)) |
| 96 | + except publishError: |
| 97 | + print("Publish Error!") |
| 98 | + except Exception as e: |
| 99 | + print("Unknown exception!") |
| 100 | + print("Type: " + str(type(e))) |
| 101 | + print("Message: " + str(e.message)) |
| 102 | + messageCount += 1 |
| 103 | + time.sleep(0.5) |
| 104 | + print("Publish thread terminated.") |
| 105 | + |
| 106 | + # ThreadB runtime function: |
| 107 | + # 1. Subscribe to topicB |
| 108 | + # 2. Wait for a while |
| 109 | + # 3. Create network blocking, triggering auto-reconnect and auto-resubscribe |
| 110 | + # 4. On connect, wait for another while |
| 111 | + def threadBRuntime(self, pyCoreClient, callback): |
| 112 | + try: |
| 113 | + # Subscribe to topicB |
| 114 | + pyCoreClient.subscribe(self._topicB, 1, callback) |
| 115 | + except subscribeTimeoutException: |
| 116 | + print("Subscribe timeout!") |
| 117 | + except subscribeError: |
| 118 | + print("Subscribe error!") |
| 119 | + except Exception as e: |
| 120 | + print("Unknown exception!") |
| 121 | + print("Type: " + str(type(e))) |
| 122 | + print("Message: " + str(e.message)) |
| 123 | + # Wait to get the first 10 messages from thread A |
| 124 | + time.sleep(10) |
| 125 | + # Block the network for 3 sec |
| 126 | + print("Block the network for 3 sec...") |
| 127 | + blockingTimeTenMs = 300 |
| 128 | + while blockingTimeTenMs != 0: |
| 129 | + manualNetworkError(pyCoreClient) |
| 130 | + blockingTimeTenMs -= 1 |
| 131 | + time.sleep(0.01) |
| 132 | + print("Leave it to the main thread to keep waiting...") |
| 133 | + |
| 134 | + |
| 135 | +############################################################################ |
| 136 | +# Main # |
| 137 | +# Check inputs |
| 138 | +myCheckInManager = checkInManager.checkInManager(1) |
| 139 | +myCheckInManager.verify(sys.argv) |
| 140 | + |
| 141 | +host = "ajje7lpljulm4-ats.iot.us-east-1.amazonaws.com" |
| 142 | +rootCA = "./test-integration/Credentials/rootCA.crt" |
| 143 | +certificate = "./test-integration/Credentials/certificate.pem.crt" |
| 144 | +privateKey = "./test-integration/Credentials/privateKey.pem.key" |
| 145 | +mode = myCheckInManager.mode |
| 146 | + |
| 147 | +skip_when_match(ModeIsALPN(mode).And( |
| 148 | + Python2VersionLowerThan((2, 7, 10)).Or(Python3VersionLowerThan((3, 5, 0))) |
| 149 | +), "This test is not applicable for mode %s and Python verison %s. Skipping..." % (mode, sys.version_info[:3])) |
| 150 | + |
| 151 | +# Init Python core and connect |
| 152 | +myMQTTClientManager = MQTTClientManager.MQTTClientManager() |
| 153 | +clientPub = myMQTTClientManager.create_connected_mqtt_core(CLIENT_ID_PUB, host, rootCA, |
| 154 | + certificate, privateKey, mode=mode) |
| 155 | +clientSub = myMQTTClientManager.create_connected_mqtt_core(CLIENT_ID_SUB, host, rootCA, |
| 156 | + certificate, privateKey, mode=mode) |
| 157 | + |
| 158 | +if clientPub is None or clientSub is None: |
| 159 | + print("Clients not init!") |
| 160 | + exit(4) |
| 161 | + |
| 162 | +print("Two clients are connected!") |
| 163 | + |
| 164 | +# Configurations |
| 165 | +################ |
| 166 | +# Callback unit |
| 167 | +subCallbackUnit = callbackUnit() |
| 168 | +# Threads |
| 169 | +mySimpleThreadManager = simpleThreadManager.simpleThreadManager() |
| 170 | +myRunFunctionUnit = runFunctionUnit() |
| 171 | +publishThreadID = mySimpleThreadManager.createOneTimeThread(myRunFunctionUnit.threadARuntime, [clientPub]) |
| 172 | +subscribeThreadID = mySimpleThreadManager.createOneTimeThread(myRunFunctionUnit.threadBRuntime, |
| 173 | + [clientSub, subCallbackUnit.messageCallback]) |
| 174 | + |
| 175 | +# Performing |
| 176 | +############ |
| 177 | +mySimpleThreadManager.startThreadWithID(subscribeThreadID) |
| 178 | +mySimpleThreadManager.startThreadWithID(publishThreadID) |
| 179 | +mySimpleThreadManager.joinOneTimeThreadWithID(subscribeThreadID) |
| 180 | +mySimpleThreadManager.joinOneTimeThreadWithID(publishThreadID) |
| 181 | +time.sleep(3) # Just in case messages arrive slowly |
| 182 | + |
| 183 | +# Verifying |
| 184 | +########### |
| 185 | +# Length |
| 186 | +print("Check if the length of the two sets are equal...") |
| 187 | +print("Received from subscription: " + str(len(subCallbackUnit.getInternalSet()))) |
| 188 | +print("Sent through publishes: " + str(len(myRunFunctionUnit._messagesPublished))) |
| 189 | +if len(myRunFunctionUnit._messagesPublished) != len(subCallbackUnit.getInternalSet()): |
| 190 | + print("Number of messages not equal!") |
| 191 | + exit(4) |
| 192 | +# Content |
| 193 | +print("Check if the content if the two sets are equivalent...") |
| 194 | +if myRunFunctionUnit._messagesPublished != subCallbackUnit.getInternalSet(): |
| 195 | + print("Sent through publishes:") |
| 196 | + print(myRunFunctionUnit._messagesPublished) |
| 197 | + print("Received from subscription:") |
| 198 | + print(subCallbackUnit.getInternalSet()) |
| 199 | + print("Set content not equal!") |
| 200 | + exit(4) |
| 201 | +else: |
| 202 | + print("Yes!") |
0 commit comments