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 XmppExpireAdapter(object): """ LOGIN <==> LOGOUT """ def __init__(self): self.is_stop = False # mqtt client self.mqtt_client = MQTTClient() self.mqtt_client.init(MQTT_IP, MQTT_PORT) self.mqtt_client.start() @gevent_adaptor(use_join_result=False) def start(self): self.xmpp_login() while not self.is_stop: time.sleep(1) def stop(self): print "stop!!!!" self.is_stop = True def xmpp_login(self): print "xmpp_login start!!!!!!!!!!!!" # subscribe mqtt.c_sub_xmpp_login_res(self.mqtt_client, login_test_jid1, self.handle_xmpp_login_res) # publish mqtt.c_pub_xmpp_login_req(self.mqtt_client, mxadap_host, utest_access_token, login_test_jid1, login_test_password1) def xmpp_logout(self): print "xmpp_logout start!!!!!!!!!!!!" # subscribe mqtt.c_sub_xmpp_logout_res(self.mqtt_client, login_test_jid1, self.handle_xmpp_logout_res) # publish mqtt.c_pub_xmpp_logout_req(self.mqtt_client, mxadap_host, self.mxid) def handle_xmpp_login_res(self, mqttc, userdata, topic, payload): self.mxid = payload['mxid'] print "handle_xmpp_login_res handle_topic,topic:%s, payload:%s mxid:%s" % (topic, payload, self.mxid) assert payload['result'] == error_code.ERROR_SUCCESS time.sleep(MXADAP_WAIT_BASE_TIME+20) self.xmpp_logout() def handle_xmpp_logout_res(self, mqttc, userdata, topic, payload): print "handle_xmpp_logout_res handle_topic,topic:%s, payload:%s" % (topic, payload) global xmpp_logout_result xmpp_logout_result = payload['result'] self.stop()
def __init__(self): self.is_stop = False # mqtt client self.mqtt_client = MQTTClient() self.mqtt_client.init(MQTT_IP, MQTT_PORT) self.mqtt_client.start()
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()
def __init__(self): self.is_stop = False # xmpp client self.xmpp_client = XMPPClient() self.xmpp_client.init(JID(roster_test_jid1), roster_test_password1) self.xmpp_client.start() # mqtt client self.mqtt_client = MQTTClient() self.mqtt_client.init(MQTT_IP, MQTT_PORT) self.mqtt_client.start() self.mqtt_jid = roster_test_jid2 self.mqtt_jid_pass = roster_test_password2
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)
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)
user_name1 = "xmpp_message_test1" + "_%s" % get_cur_ip() user_name2 = "xmpp_message_test2" + "_%s" % get_cur_ip() user_name3 = "xmpp_message_test3" + "_%s" % get_cur_ip() [(user_jid1, user_jid_pass1), (user_jid2, user_jid_pass2), (user_jid3, user_jid_pass3)] \ = make_xmpp_relation([user_name1, user_name2, user_name3], add_roster=True) # xmpp client xmpp_client = XMPPClient() xmpp_client.init(JID(user_jid1), user_jid_pass1) xmpp_client.start() # mqtt client mqtt_client2 = MQTTClient() mqtt_client2.init(MQTT_IP, MQTT_PORT) mqtt_client2.start() mqtt_client3 = MQTTClient() mqtt_client3.init(MQTT_IP, MQTT_PORT) mqtt_client3.start() # mqtt2xmpp mqtt2xmpp_send_subject = "mqtt2xmpp_send_subject" mqtt2xmpp_send_body = "mqtt2xmpp_send_body" mqtt2xmpp_recv_body = None @xmpp_handler(xmpp_client, mqtt2xmpp_send_subject) def mqtt2xmpp_msg_handler(_, from_jid, body): global mqtt2xmpp_recv_body
class MqttRosterAdapter(object): """ roster_test_jid2 add roster_test_jid1 to the roster """ __metaclass__ = Singleton def __init__(self): self.is_stop = False # xmpp client self.xmpp_client = XMPPClient() self.xmpp_client.init(JID(roster_test_jid1), roster_test_password1) self.xmpp_client.start() # mqtt client self.mqtt_client = MQTTClient() self.mqtt_client.init(MQTT_IP, MQTT_PORT) self.mqtt_client.start() self.mqtt_jid = roster_test_jid2 self.mqtt_jid_pass = roster_test_password2 @gevent_adaptor(use_join_result=False) def start(self): self.xmpp_login() while not self.is_stop: time.sleep(1) def stop(self): print "stop!!!!" self.is_stop = True def xmpp_login(self): print "MqttRosterAdapter::xmpp_login start!!!!!!!!!!!!" # subscribe mqtt.c_sub_xmpp_login_res(self.mqtt_client, self.mqtt_jid, self.handle_xmpp_login_res) # publish mqtt.c_pub_xmpp_login_req(self.mqtt_client, mxadap_host, utest_access_token, self.mqtt_jid, self.mqtt_jid_pass) def xmpp_add_roster(self, jid): print "MqttRosterAdapter::xmpp_add_roster start!!!!!!!!!!!!" mqtt.c_sub_xmpp_roster_update_notify(self.mqtt_client, self.mqtt_jid, self.handle_xmpp_roster_update_notify) mqtt.c_sub_xmpp_roster_presence_notify(self.mqtt_client, self.mqtt_jid, self.handle_xmpp_roster_presence_notify) mqtt.c_sub_xmpp_roster_add_res(self.mqtt_client, self.mqtt_jid, self.handle_xmpp_roster_add_res) mqtt.c_pub_xmpp_roster_add_req(self.mqtt_client, mxadap_host, self.mxid, jid) def xmpp_del_roster(self, jid): print "MqttRosterAdapter::xmpp_del_roster start!!!!!!!!!!!!" mqtt.c_sub_xmpp_roster_del_res(self.mqtt_client, self.mqtt_jid, self.handle_xmpp_roster_del_res) mqtt.c_pub_xmpp_roster_del_req(self.mqtt_client, mxadap_host, self.mxid, jid) def handle_xmpp_login_res(self, mqttc, userdata, topic, payload): self.mxid = payload['mxid'] print "MqttRosterAdapter::handle_xmpp_login_res,topic:%s, payload:%s mxid:%s" % (topic, payload, self.mxid) self.xmpp_add_roster(roster_test_jid1) global xmpp_login_result xmpp_login_result = payload['result'] def handle_xmpp_roster_add_res(self, mqttc, userdata, topic, payload): print "MqttRosterAdapter::handle_xmpp_roster_add_res,topic:%s, payload:%s mxid:%s" % (topic, payload, self.mxid) global xmpp_roster_add_result xmpp_roster_add_result = payload['result'] self.xmpp_del_roster(roster_test_jid1) def handle_xmpp_roster_del_res(self, mqttc, userdata, topic, payload): print "MqttRosterAdapter::handle_xmpp_roster_del_res,topic:%s, payload:%s mxid:%s" % (topic, payload, self.mxid) global xmpp_roster_del_result xmpp_roster_del_result = payload['result'] self.stop() def handle_xmpp_roster_presence_notify(self, mqttc, userdata, topic, payload): print "MqttRosterAdapter::handle_xmpp_roster_presence_notify,topic:%s, payload:%s mxid:%s" % (topic, payload, self.mxid) global xmpp_roster_present_notify_dic xmpp_roster_present_notify_dic[payload['presence_type']] = payload def handle_xmpp_roster_update_notify(self, mqttc, userdata, topic, payload): print "MqttRosterAdapter::handle_xmpp_roster_update_notify,topic:%s, payload:%s mxid:%s" % (topic, payload, self.mxid)
#!/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]
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 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)