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 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)
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)
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 mqtt2xmpp_recv_body = body Mqtt2XmppMsgAdapter().stop()
#!/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 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)
""" from lib.common import * from lib.service import * from utils.network.mqtt import MQTTClient import time from interfaces.mxadap import mqtt from utils.wapper.stackless import gevent_adaptor from utils.meta.singleton import Singleton from lib.xmpp import make_xmpp_relation from utils.service_control.finder import get_cur_ip from utest_mxadap.utest_smoke import MXADAP_WAIT_BASE_TIME # mqtt client mqtt_client1 = MQTTClient() mqtt_client1.init(MQTT_IP, MQTT_PORT) mqtt_client1.start() mqtt_client2 = MQTTClient() mqtt_client2.init(MQTT_IP, MQTT_PORT) mqtt_client2.start() # add jid and roster # xmpp client mqtt_name1 = "xmpp_presence_test1" + "_%s" % get_cur_ip() mqtt_name2 = "xmpp_presence_test2" + "_%s" % get_cur_ip() [(mqtt_jid1, mqtt_jid_pass1), (mqtt_jid2, mqtt_jid_pass2)] = make_xmpp_relation( [mqtt_name1, mqtt_name2], add_roster=True ) # should result