def test_021(self): c = MqttClient("cs:{seq}", connect=4, clean_session=0) c.subscribe("/cs/topic1/+", qos=2) c.subscribe("/cs/topic1/q2", qos=1) c.disconnect() pub = MqttClient("pub:{seq}", connect=4) pubmsg = { 'topic': "/cs/topic1/q2", 'qos': 2, 'payload': env.gen_msg(42) } ack = pub.publish(**pubmsg) pub.pubrel(ack.mid) pub.disconnect() msgs = env.db.lrange("queue:" + c.clientid(), 0, -1) if len(msgs) != 2 * 3: debug(msgs) return False for (topic, qos, msgid) in [msgs[i:i + 3] for i in range(0, len(msgs), 3)]: content = env.db.get("msg:" + msgid) if topic != pubmsg['topic'] or content != pubmsg['payload']: debug("{0}: {1}, {2}".format(topic, content, pubmsg)) return False if int(qos) not in (1, 2): debug("{0}: qos {1}".format(topic, qos)) return False return True
def test_003(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=0) client = MqttClient("rabbit:{seq}", keepalive=2) will = {'topic': '/node/disconnect', 'message': client.clientid()} client.connect(version=4, will=will) time.sleep(1) client.send_pingreq() evt = monitor.recv() if monitor.recv() != None: debug(evt) return False time.sleep(4) evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message']: debug(evt) return False monitor.disconnect() return True
def test_020(self): c = MqttClient("cs:{seq}", connect=4, clean_session=0) c.subscribe("/cs/topic1/+", qos=2) c.disconnect() pub = MqttClient("pub:{seq}", connect=4) pubmsgs = { "/cs/topic1/q0": [0, env.gen_msg(42)], "/cs/topic1/q1": [1, env.gen_msg(42)], "/cs/topic1/q2": [2, env.gen_msg(42)], } for (topic, (qos, msg)) in pubmsgs.iteritems(): ack = pub.publish(topic, msg, qos=qos) if qos == 2: pub.pubrel(ack.mid) pub.disconnect() msgs = env.db.lrange("queue:" + c.clientid(), 0, -1) for (topic, qos, msgid) in [msgs[i:i + 3] for i in range(0, len(msgs), 3)]: content = env.db.get("msg:" + msgid) origin = pubmsgs.get(topic, [-1, ""]) #print topic, origin, qos, content if int(qos) != origin[0] or content != origin[1]: debug("{0}: {1}, {2}".format(origin, qos, content)) return False return True
def test_003(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=0) client = MqttClient("rabbit:{seq}", keepalive=2) will = {'topic': '/node/disconnect', 'message': client.clientid()} client.connect(version=4, will=will) time.sleep(1) client.send_pingreq() evt = monitor.recv() if monitor.recv() != None: debug(evt) return False time.sleep(4) evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message']: debug(evt) return False monitor.disconnect() return True
def test_012(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=2) client = MqttClient("rabbit:{seq}") # no keepalive will = { 'topic': '/node/disconnect', 'message': client.clientid(), 'retain': True } client.connect(version=4, will=will) client.socket_close() evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message'] or \ evt.msg.qos != 0: debug(evt) return False if not evt.msg.retain: debug("evt remain flag set") return False monitor.disconnect() return True
def test_002(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=0) client = MqttClient("rabbit:{seq}") will = {'topic': '/node/disconnect', 'message': client.clientid()} client.connect(version=4, will=will) # close socket without disconnection client.socket_close() evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message']: debug(evt) return False monitor.disconnect() return True
def test_002(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=0) client = MqttClient("rabbit:{seq}") will = {'topic': '/node/disconnect', 'message': client.clientid()} client.connect(version=4, will=will) # close socket without disconnection client.socket_close() evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message']: debug(evt) return False monitor.disconnect() return True
def test_011(self): c = MqttClient("cs:{seq}", connect=4, clean_session=0) c.subscribe("/cs/qos-0", qos=0) c.subscribe("/cs/qos-1", qos=1) c.subscribe("/cs/qos-2", qos=2) c.disconnect() # reconnect w/ same clientid time.sleep(.5) c2 = MqttClient(client_id=c.client_id, connect=4, clean_session=0) topics = env.db.lrange("topics:" + c.clientid(), 0, -1) if len(topics) != 0: debug(topics) return False # checking CONNACK session-present if c2.connack().session_present != 1: debug("session not present") return False #TODO: list c2 subscriptions (needs specific API ?) return True
def test_010(self): c = MqttClient("cs:{seq}", connect=4, clean_session=0) c.subscribe("/cs/qos-0", qos=0) c.subscribe("/cs/qos-1", qos=1) c.subscribe("/cs/qos-2", qos=2) c.disconnect() # sleep ensure redis data has been written time.sleep(.5) topics = env.db.lrange("topics:" + c.clientid(), 0, -1) topics = dict(map(lambda x: topics[x:x + 2], xrange(0, len(topics), 2))) if len(topics) != 3: debug(topics) return False for i in (0, 1, 2): qos = int(topics.get("/cs/qos-{0}".format(i), -1)) if qos != i: debug("wrong qos{0} topic qos: {1}".format(i, qos)) return False return True
def test_004(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=0) client = MqttClient("rabbit:{seq}") # no keepalive will = {'topic': '/node/disconnect', 'message': client.clientid()} client.connect(version=4, will=will) # protocol errlr flags shoud be 0 client.forge(NC.CMD_PINGREQ, 4, [], send=True) if client.conn_is_alive(): debug("connection still alive") return False evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message']: debug(evt) return False monitor.disconnect() return True
def test_012(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=2) client = MqttClient("rabbit:{seq}") # no keepalive will = {'topic': '/node/disconnect', 'message': client.clientid(), 'retain': True} client.connect(version=4, will=will) client.socket_close() evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message'] or \ evt.msg.qos != 0: debug(evt) return False if not evt.msg.retain: debug("evt remain flag set") return False monitor.disconnect() return True
def test_004(self): monitor = MqttClient("monitor:{seq}", connect=4) # NOTE: '/' prefix skips $ messages # TODO: remove it when '$' filter will be impl. monitor.subscribe("/#", qos=0) client = MqttClient("rabbit:{seq}") # no keepalive will = {'topic': '/node/disconnect', 'message': client.clientid()} client.connect(version=4, will=will) # protocol errlr flags shoud be 0 client.forge(NC.CMD_PINGREQ, 4, [], send=True) if client.conn_is_alive(): debug("connection still alive") return False evt = monitor.recv() if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \ evt.msg.payload != will['message']: debug(evt) return False monitor.disconnect() return True