def cleanup(): # clean all client state print("clean up starting") clientids = ("myclientid", "myclientid2") for clientid in clientids: curclient = mqtt_client.Client(clientid.encode("utf-8")) curclient.connect(host=host, port=port, cleansession=True) time.sleep(.1) curclient.disconnect() time.sleep(.1) # clean retained messages callback = Callbacks() curclient = mqtt_client.Client("clean retained".encode("utf-8")) curclient.registerCallback(callback) curclient.connect(host=host, port=port, cleansession=True) curclient.subscribe(["#"], [0]) time.sleep(2) # wait for all retained messages to arrive for message in callback.messages: if message[3]: # retained flag print("deleting retained message for topic", message[0]) curclient.publish(message[0], b"", 0, retained=True) curclient.disconnect() time.sleep(.1) print("clean up finished")
def setUpClass(cls): global callback, callback2, aclient, bclient cleanup() callback = Callbacks() callback2 = Callbacks() #aclient = mqtt_client.Client(b"\xEF\xBB\xBF" + "myclientid".encode("utf-8")) aclient = mqtt_client.Client("myclientid".encode("utf-8")) aclient.registerCallback(callback) bclient = mqtt_client.Client("myclientid2".encode("utf-8")) bclient.registerCallback(callback2)
def test_zero_length_clientid(self): print("Zero length clientid test starting") succeeded = True try: client0 = mqtt_client.Client("") fails = False try: client0.connect(host=host, port=port, cleansession=False) # should be rejected except: fails = True self.assertEqual(fails, True) fails = False try: client0.connect(host=host, port=port, cleansession=True) # should work except: fails = True self.assertEqual(fails, False) client0.disconnect() except: traceback.print_exc() succeeded = False print("Zero length clientid test", "succeeded" if succeeded else "failed") self.assertEqual(succeeded, True) return succeeded
def clientidtest(self, clientid, username, apppassword): print("clientid test starting") succeeded = True try: client0 = mqtt_client.Client(clientid.encode("utf-8")) fails = True # try: client0.connect(host=host, port=port, cleansession=True, username=username, password=apppassword) # should work print(wildtopics[0], topics[1]) client0.subscribe([wildtopics[0]], [2]) time.sleep(.1) client0.publish(topics[1], "test cliendid", 2, retained=False) time.sleep(1) print(callback3.messages) assert len(callback3.messages) == 2 # except: # fails = False # self.assertEqual(fails, True) # fails = True # try: # client0.connect(host=host, port=port, cleansession=True,username=username,password=apppassword) # should work # except: # fails = False # self.assertEqual(fails, True) client0.disconnect() except: traceback.print_exc() succeeded = False print("error appkey clientid test", "succeeded" if succeeded else "failed") return succeeded
def test_duoge_connect(self): print("Basic test starting") succeeded = True number = 10 try: for i in range(number): clientid1 = "deviceid%s" % (i) + "@" + "1wyp94" print(clientid1) print("aclient is %s" % aclient) ac = mqtt_client.Client(clientid1.encode("utf-8")) callback = "callback%s" % (i) print(callback) ac.registerCallback(callback) ac.setUserName(username1, password1) print("succee") ac.connect(host=host, port=port) # build_user(self, number) # print("build end") # for i in range(number): # ac = "aclient%s"%i # print(ac) # ac.connect(host=host, port=port) # print("connect succeed") time.sleep(10) # aclient.disconnect() except: succeeded = False self.assertEqual(succeeded, True) return succeeded
def setUpClass(cls): global callback, callback2, callback3, aclient, bclient, cclient # cleanup() callback = Callbacks() callback2 = Callbacks() callback3 = Callbacks() #aclient = mqtt_client.Client(b"\xEF\xBB\xBF" + "myclientid".encode("utf-8")) aclient = mqtt_client.Client(clientid1.encode("utf-8")) aclient.registerCallback(callback) aclient.setUserName(username1, password1) bclient = mqtt_client.Client(clientid2.encode("utf-8")) bclient.registerCallback(callback2) bclient.setUserName(username2, password2) cclient = mqtt_client.Client(clientid1.encode("utf-8")) cclient.registerCallback(callback3) cclient.setUserName(username1, password1)
def build_user(self, number): for i in range(number): clientid1 = "deviceid%s" % (i) + "@" + "1wyp94" print(clientid1) aclient = "aclient%s" % (i) print("aclient is %s" % aclient) aclient = mqtt_client.Client(clientid1.encode("utf-8")) callback = "callback%s" % (i) print(callback) callback = Callbacks() aclient.registerCallback(callback) aclient.setUserName(username1, password1)
def test_session_less_than_connect(self): print("Basic test starting") succeeded = True try: aclient = mqtt_client.Client(clientid1.encode("utf-8")) aclient.registerCallback(callback) username1 = "errorusername" aclient.setUserName(username1, password1) aclient.connect(host=host, port=port, cleansession=True) print("connect succeeded") except: # traceback.print_exc() succeeded = False time.sleep(10) self.assertEqual(succeeded, True)