class MqttPublishSubscribeUserDataTest(unittest.TestCase): publish_client = MQTTClient() publish_client.init(MQTT_IP, MQTT_PORT) subscribe_client = MQTTClient() subscribe_client.init(MQTT_IP, MQTT_PORT) publish_user_data = "publish_data" subscribe_user_data = "subscribe_data" publish_client.user_data_set(publish_user_data) subscribe_client.user_data_set(subscribe_user_data) publish_client.start() subscribe_client.start() time.sleep(SYNC_WAIT_TIME) topic = random_str(10) msg = random_str(10) @unittest_adaptor() def test_publish_subscribe(self): self.subscribe_client.subscribe(self.topic, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(self.topic, self.msg, 2) time.sleep(SYNC_WAIT_TIME) def handle_topic(self, mqttc, userdata, topic, payload): print "MqttPublishSubscribeUserDataTest::handle_topic,",topic, payload self.assertTrue(mqttc == self.subscribe_client) self.assertTrue(userdata == self.subscribe_user_data) self.assertTrue(payload == self.msg)
class APIChatMsgBcastTest(AsyncTestCase): GMqttClient = MQTTClient() GMqttClient.init(MQTT_IP, 1883) GMqttClient.start() test_chat_msg_bcast_hdl = None def test_chat_msg_bcast(self): self.test_chat_msg_bcast_hdl = self file_type = "3" fn = "test_fn.amr" ref = "reftest" * 20 self.bcast_topic = C_SUB_BEIQI_MSG_BCAST.replace("{gid}", "#") print self.bcast_topic self.GMqttClient.subscribe(self.bcast_topic, self.chat_bcast_res) time.sleep(SYNC_WAIT_TIME) chat_bcast(SERVER_IP, gen_test_tk(), APP_SECRET, file_type, fn, ref) self.wait(timeout=SYNC_WAIT_TIME) def chat_bcast_res(self, mqttc, userdata, topic, payload): print topic self.test_chat_msg_bcast_hdl.stop()
class APIAudioSend2DevTest(AsyncTestCase): GMqttClient = MQTTClient() GMqttClient.init(MQTT_IP, 1883) GMqttClient.start() test_pub_2_dev_hdl = None def test_pub_2_dev(self): self.test_pub_2_dev_hdl = self audio_ls = get_rdm_list(SERVER_IP, gen_test_tk(), APP_SECRET) print audio_ls select_audio = random.choice(audio_ls) sn = "PNZHANCHENJIN" self.p2d_topic = C_SUB_BEIQI_MSG_P2P.format(sn=sn) self.GMqttClient.subscribe(self.p2d_topic, self.pub_2_dev_res) time.sleep(SYNC_WAIT_TIME) pub_2_dev(SERVER_IP, gen_test_tk(), APP_SECRET, sn, select_audio['name'], select_audio['ref']) self.wait(timeout=5) def pub_2_dev_res(self, mqttc, userdata, topic, payload): print "pub_2_dev_res,", payload self.assertTrue(topic == self.p2d_topic) self.test_pub_2_dev_hdl.stop()
class MqttPublishSubscribeJIDTopic(unittest.TestCase): publish_client = MQTTClient() publish_client.init(MQTT_IP, MQTT_PORT) subscribe_client = MQTTClient() subscribe_client.init(MQTT_IP, MQTT_PORT) publish_client.start() subscribe_client.start() time.sleep(SYNC_WAIT_TIME) topic = "xmpp_message/bridge" msg = random_str(10) @unittest_adaptor() def test_publish_subscribe_jid_topic(self): self.subscribe_client.subscribe(self.topic, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(self.topic, self.msg, 2) time.sleep(SYNC_WAIT_TIME) def handle_topic(self, mqttc, userdata, topic, payload): print "MqttPublishSubscribeJIDTopic::handle_topic,",topic, payload self.assertTrue(mqttc == self.subscribe_client) self.assertTrue(userdata is None) self.assertTrue(payload == self.msg)
def test_subscribe_match_any_name(self): field1 = random_str(10) field2 = random_str(10) field3 = random_str(10) pub_topic = "%s/%s/%s" % (field1, field2,field3) sub_topic1 = "%s/+/%s" % (field1, field3) msg = "match_any_name_middle" self.subscribe_client.subscribe(sub_topic1, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME) subscribe_client2 = MQTTClient() subscribe_client2.init(MQTT_IP, MQTT_PORT) subscribe_client2.start() time.sleep(SYNC_WAIT_TIME) sub_topic2 = "%s/%s/+" % (field1, field2) msg = "match_any_name_right" subscribe_client2.subscribe(sub_topic2, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME) subscribe_client3 = MQTTClient() subscribe_client3.init(MQTT_IP, MQTT_PORT) subscribe_client3.start() time.sleep(SYNC_WAIT_TIME) sub_topic3 = "+/%s/%s" % (field2, field3) msg = "match_any_name_left" subscribe_client3.subscribe(sub_topic3, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME)
class MqttServerMsgP2PTest(AsyncTestCase): GMqttClient = MQTTClient() GMqttClient.init(MQTT_IP, 1883) GMqttClient.start() test_common_msg_p2p_hdl = None special_common_msg_p2p_hdl = None def test_mqtt_msg_p2p_common(self): self.test_common_msg_p2p_hdl = self sn = random_str() self.common_payload = random_str() self.common_topic = C_SUB_BEIQI_MSG_P2P.format(sn=sn) self.GMqttClient.subscribe(self.common_topic, self.comon_msg_p2p_res) time.sleep(SYNC_WAIT_TIME) beiqi_msg_p2p(MQTT_SERVER_IP, sn, self.common_payload) self.wait(timeout=SYNC_WAIT_TIME) def comon_msg_p2p_res(self, mqttc, userdata, topic, payload): self.assertTrue(topic == self.common_topic) self.assertTrue(payload == self.common_payload) self.test_common_msg_p2p_hdl.stop() def test_mqtt_msg_p2p_special(self): self.special_common_msg_p2p_hdl = self sn = "PNZHANCHENJIN" self.special_payload = "1:play:audio:Sleep+Away.mp3:88d7dce4248c314fdd773f86b7198b31dd0aff81719dfd54aa033641df0a83b25cf5c6a1a146d340b0bbf3e2475dabefe4cbd1e8f4adeb706a91d6a02b28a80e57586dbfa413e19b29987f484660ec6709a94b74c2307ee1cb5e0f5d421cf0fdef08d10006197e47ce64eaeeafa8293e58a61d" self.special_topic = C_SUB_BEIQI_MSG_P2P.format(sn=sn) self.GMqttClient.subscribe(self.special_topic, self.special_msg_p2p_res) time.sleep(SYNC_WAIT_TIME) beiqi_msg_p2p(MQTT_SERVER_IP, sn, self.special_payload) self.wait(timeout=SYNC_WAIT_TIME) def special_msg_p2p_res(self, mqttc, userdata, topic, payload): self.assertTrue(topic == self.special_topic) self.assertTrue(payload == self.special_payload) self.special_common_msg_p2p_hdl.stop()
class MqttServerMsgBCastTest(AsyncTestCase): GMqttClient = MQTTClient() GMqttClient.init(MQTT_IP, 1883) GMqttClient.start() test_common_gid_payload_hdl = None test_special_gid_payload_hdl = None gid = random_str() payload = random_str() topic = C_SUB_BEIQI_MSG_BCAST.format(gid=gid) def test_mqtt_msg_bcast_common(self): self.test_common_gid_payload_hdl = self self.GMqttClient.subscribe(self.topic, self.beiqi_msg_bcast_res) time.sleep(SYNC_WAIT_TIME) beiqi_msg_bcast(MQTT_SERVER_IP, self.gid, self.payload) self.wait(timeout=SYNC_WAIT_TIME) def beiqi_msg_bcast_res(self, mqttc, userdata, topic, payload): self.assertTrue(topic == self.topic) self.assertTrue(payload == self.payload) self.test_common_gid_payload_hdl.stop() def test_special_gid_payload(self): gid = 775817 self.sgp_payload = "18610060484%40jiashu.com:3:test_fn.amr:reftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftestreftest:::" self._sgp_topic = C_SUB_BEIQI_MSG_BCAST.format(gid=gid) self.test_special_gid_payload_hdl = self self.GMqttClient.subscribe(self._sgp_topic, self.special_gid_payload_res) time.sleep(SYNC_WAIT_TIME) beiqi_msg_bcast(MQTT_SERVER_IP, gid, self.sgp_payload) self.wait(timeout=SYNC_WAIT_TIME) def special_gid_payload_res(self, mqttc, userdata, topic, payload): self.assertTrue(topic == self._sgp_topic) self.assertTrue(payload == self.sgp_payload) self.test_special_gid_payload_hdl.stop()
class MqttPublishSubscribeSelfTest(unittest.TestCase): client = MQTTClient() client.init(MQTT_IP, MQTT_PORT) client.start() time.sleep(SYNC_WAIT_TIME) topic = random_str(10) msg = random_str(10) @unittest_adaptor() def test_publish_subscribe(self): self.client.subscribe(self.topic, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.client.publish(self.topic, self.msg, 2) time.sleep(SYNC_WAIT_TIME) def handle_topic(self, mqttc, userdata, topic, payload): print "MqttPublishSubscribeSelf::handle_topic,",topic, payload self.assertTrue(mqttc == self.client) self.assertTrue(userdata is None) self.assertTrue(payload == self.msg)
def test_subscribe_recursively_match_names(self): field1 = random_str(10) field2 = random_str(10) field3 = random_str(10) pub_topic = "%s/%s/%s" % (field1, field2,field3) sub_topic1 = "%s/#" % (field1) msg = "recursively_match_names_one" self.subscribe_client.subscribe(sub_topic1, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME) subscribe_client2 = MQTTClient() subscribe_client2.init(MQTT_IP, MQTT_PORT) subscribe_client2.start() time.sleep(SYNC_WAIT_TIME) sub_topic2 = "%s/%s/#" % (field1, field2) msg = "recursively_match_names__two" subscribe_client2.subscribe(sub_topic2, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME)
#!/usr/bin/python2.7 # coding=utf-8 """ Created on 2016/6/13 @author: Jay """ from utest_lib.common import * from interfaces.mqtt_server.http_rpc import get_status from interfaces.mqtt_server.mqtt import c_pub_offline_req, c_pub_online_req from utils.network.mqtt import MQTTClient mqtt_client = MQTTClient() mqtt_client.init(MQTT_IP, MQTT_PORT) mqtt_client.start() def check_status(user_list, status): get_status_res = get_status(MQTT_SERVER_IP, user_list) for k, v in get_status_res.items(): if k not in user_list: return False if v.split(":")[0] != status: return False return True class MqttServerStatusTest(unittest.TestCase): def test_mqtt_status(self): test_count = 3 user_list = [TEST_SN]
class MqttWildcardSubscriptionsTest(unittest.TestCase): publish_client = MQTTClient() publish_client.init(MQTT_IP, MQTT_PORT) subscribe_client = MQTTClient() subscribe_client.init(MQTT_IP, MQTT_PORT) publish_client.start() subscribe_client.start() time.sleep(SYNC_WAIT_TIME) def handle_topic(self, mqttc, userdata, topic, payload): print "MqttWildcardSubscriptionsTest::handle_topic,",topic, payload pass @unittest_adaptor() def test_subscribe_separate_names(self): topic = "%s/%s" % (random_str(10), random_str(10)) self.subscribe_client.subscribe(topic, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) msg = "separate_names" self.publish_client.publish(topic, msg, 2) time.sleep(SYNC_WAIT_TIME) @unittest_adaptor() def test_subscribe_match_any_name(self): field1 = random_str(10) field2 = random_str(10) field3 = random_str(10) pub_topic = "%s/%s/%s" % (field1, field2,field3) sub_topic1 = "%s/+/%s" % (field1, field3) msg = "match_any_name_middle" self.subscribe_client.subscribe(sub_topic1, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME) subscribe_client2 = MQTTClient() subscribe_client2.init(MQTT_IP, MQTT_PORT) subscribe_client2.start() time.sleep(SYNC_WAIT_TIME) sub_topic2 = "%s/%s/+" % (field1, field2) msg = "match_any_name_right" subscribe_client2.subscribe(sub_topic2, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME) subscribe_client3 = MQTTClient() subscribe_client3.init(MQTT_IP, MQTT_PORT) subscribe_client3.start() time.sleep(SYNC_WAIT_TIME) sub_topic3 = "+/%s/%s" % (field2, field3) msg = "match_any_name_left" subscribe_client3.subscribe(sub_topic3, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME) @unittest_adaptor() def test_subscribe_recursively_match_names(self): field1 = random_str(10) field2 = random_str(10) field3 = random_str(10) pub_topic = "%s/%s/%s" % (field1, field2,field3) sub_topic1 = "%s/#" % (field1) msg = "recursively_match_names_one" self.subscribe_client.subscribe(sub_topic1, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME) subscribe_client2 = MQTTClient() subscribe_client2.init(MQTT_IP, MQTT_PORT) subscribe_client2.start() time.sleep(SYNC_WAIT_TIME) sub_topic2 = "%s/%s/#" % (field1, field2) msg = "recursively_match_names__two" subscribe_client2.subscribe(sub_topic2, self.handle_topic, 2) time.sleep(SYNC_WAIT_TIME) self.publish_client.publish(pub_topic, msg, 2) time.sleep(SYNC_WAIT_TIME)