2015-09-30 19:34:21 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
import unittest
|
2015-09-30 20:41:39 +00:00
|
|
|
from unittest.mock import patch, call
|
2015-09-30 19:34:21 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2015-09-30 20:41:39 +00:00
|
|
|
from hbmqtt.broker import *
|
2015-09-30 19:34:21 +00:00
|
|
|
from hbmqtt.mqtt.constants import *
|
2015-10-07 20:42:04 +00:00
|
|
|
from hbmqtt.client import MQTTClient
|
2015-09-30 19:34:21 +00:00
|
|
|
|
|
|
|
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format=formatter)
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-10-07 20:42:04 +00:00
|
|
|
test_config = {
|
|
|
|
'listeners': {
|
|
|
|
'default': {
|
|
|
|
'type': 'tcp',
|
|
|
|
'bind': 'localhost:1883',
|
|
|
|
'max_connections': 10
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'sys_interval': 0,
|
|
|
|
'auth': {
|
|
|
|
'allow-anonymous': True,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:34:21 +00:00
|
|
|
|
|
|
|
class BrokerTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|
|
|
|
|
2015-09-30 20:41:39 +00:00
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_start_stop(self, MockPluginManager):
|
2015-09-30 19:34:21 +00:00
|
|
|
def test_coro():
|
2015-09-30 20:41:39 +00:00
|
|
|
try:
|
2015-10-07 20:42:04 +00:00
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
2015-09-30 20:41:39 +00:00
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
2015-10-07 20:42:04 +00:00
|
|
|
self.assertDictEqual(broker._sessions, {})
|
2015-09-30 20:41:39 +00:00
|
|
|
self.assertIn('default', broker._servers)
|
|
|
|
MockPluginManager.assert_has_calls(
|
|
|
|
[call().fire_event(EVENT_BROKER_PRE_START),
|
|
|
|
call().fire_event(EVENT_BROKER_POST_START)], any_order=True)
|
|
|
|
MockPluginManager.reset_mock()
|
|
|
|
yield from broker.shutdown()
|
|
|
|
MockPluginManager.assert_has_calls(
|
|
|
|
[call().fire_event(EVENT_BROKER_PRE_SHUTDOWN),
|
|
|
|
call().fire_event(EVENT_BROKER_POST_SHUTDOWN)], any_order=True)
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
2015-09-30 19:34:21 +00:00
|
|
|
|
2015-09-30 20:41:39 +00:00
|
|
|
future = asyncio.Future(loop=self.loop)
|
2015-09-30 19:34:21 +00:00
|
|
|
self.loop.run_until_complete(test_coro())
|
2015-09-30 20:41:39 +00:00
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
2015-10-07 20:42:04 +00:00
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_connect(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
|
|
|
client = MQTTClient()
|
|
|
|
ret = yield from client.connect('mqtt://localhost/')
|
|
|
|
self.assertEqual(ret, 0)
|
2015-10-08 20:09:34 +00:00
|
|
|
self.assertIn(client.session.client_id, broker._sessions)
|
2015-10-07 20:42:04 +00:00
|
|
|
yield from client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
2015-10-08 19:25:15 +00:00
|
|
|
self.assertDictEqual(broker._sessions, {})
|
2015-10-07 20:42:04 +00:00
|
|
|
MockPluginManager.assert_has_calls(
|
2015-10-08 20:09:34 +00:00
|
|
|
[call().fire_event(EVENT_BROKER_CLIENT_CONNECTED, client_id=client.session.client_id),
|
|
|
|
call().fire_event(EVENT_BROKER_CLIENT_DISCONNECTED, client_id=client.session.client_id)],
|
|
|
|
any_order=True)
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_subscribe(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
|
|
|
client = MQTTClient()
|
|
|
|
ret = yield from client.connect('mqtt://localhost/')
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
yield from client.subscribe([('/topic', QOS_0)])
|
|
|
|
|
|
|
|
# Test if the client test client subscription is registered
|
|
|
|
self.assertIn('/topic', broker._subscriptions)
|
|
|
|
subs = broker._subscriptions['/topic']
|
|
|
|
self.assertEquals(len(subs), 1)
|
|
|
|
(s, qos) = subs[0]
|
|
|
|
self.assertEquals(s, client.session)
|
|
|
|
self.assertEquals(qos, QOS_0)
|
|
|
|
|
|
|
|
yield from client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
MockPluginManager.assert_has_calls(
|
|
|
|
[call().fire_event(EVENT_BROKER_CLIENT_SUBSCRIBED,
|
|
|
|
client_id=client.session.client_id,
|
|
|
|
topic='/topic', qos=QOS_0)], any_order=True)
|
2015-10-07 20:42:04 +00:00
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
2015-10-08 20:38:32 +00:00
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_subscribe_twice(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
|
|
|
client = MQTTClient()
|
|
|
|
ret = yield from client.connect('mqtt://localhost/')
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
yield from client.subscribe([('/topic', QOS_0)])
|
|
|
|
|
|
|
|
# Test if the client test client subscription is registered
|
|
|
|
self.assertIn('/topic', broker._subscriptions)
|
|
|
|
subs = broker._subscriptions['/topic']
|
|
|
|
self.assertEquals(len(subs), 1)
|
|
|
|
(s, qos) = subs[0]
|
|
|
|
self.assertEquals(s, client.session)
|
|
|
|
self.assertEquals(qos, QOS_0)
|
|
|
|
|
|
|
|
yield from client.subscribe([('/topic', QOS_0)])
|
|
|
|
self.assertEquals(len(subs), 1)
|
|
|
|
(s, qos) = subs[0]
|
|
|
|
self.assertEquals(s, client.session)
|
|
|
|
self.assertEquals(qos, QOS_0)
|
|
|
|
|
|
|
|
yield from client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
MockPluginManager.assert_has_calls(
|
|
|
|
[call().fire_event(EVENT_BROKER_CLIENT_SUBSCRIBED,
|
|
|
|
client_id=client.session.client_id,
|
|
|
|
topic='/topic', qos=QOS_0)], any_order=True)
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_unsubscribe(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
|
|
|
client = MQTTClient()
|
|
|
|
ret = yield from client.connect('mqtt://localhost/')
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
yield from client.subscribe([('/topic', QOS_0)])
|
|
|
|
|
|
|
|
# Test if the client test client subscription is registered
|
|
|
|
self.assertIn('/topic', broker._subscriptions)
|
|
|
|
subs = broker._subscriptions['/topic']
|
|
|
|
self.assertEquals(len(subs), 1)
|
|
|
|
(s, qos) = subs[0]
|
|
|
|
self.assertEquals(s, client.session)
|
|
|
|
self.assertEquals(qos, QOS_0)
|
|
|
|
|
|
|
|
yield from client.unsubscribe(['/topic'])
|
2015-10-12 19:33:14 +00:00
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
self.assertEquals(broker._subscriptions['/topic'], [])
|
2015-10-08 20:38:32 +00:00
|
|
|
yield from client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
MockPluginManager.assert_has_calls(
|
|
|
|
[call().fire_event(EVENT_BROKER_CLIENT_SUBSCRIBED,
|
|
|
|
client_id=client.session.client_id,
|
|
|
|
topic='/topic', qos=QOS_0),
|
|
|
|
call().fire_event(EVENT_BROKER_CLIENT_UNSUBSCRIBED,
|
|
|
|
client_id=client.session.client_id,
|
|
|
|
topic='/topic')
|
|
|
|
], any_order=True)
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
2015-10-10 13:01:17 +00:00
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_publish(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
|
|
|
pub_client = MQTTClient()
|
|
|
|
ret = yield from pub_client.connect('mqtt://localhost/')
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
|
|
|
|
ret_message = yield from pub_client.publish('/topic', b'data', QOS_0)
|
|
|
|
yield from pub_client.disconnect()
|
|
|
|
self.assertEquals(broker._retained_messages, {})
|
|
|
|
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
MockPluginManager.assert_has_calls(
|
|
|
|
[call().fire_event(EVENT_BROKER_MESSAGE_RECEIVED,
|
|
|
|
client_id=pub_client.session.client_id,
|
|
|
|
message=ret_message),
|
|
|
|
], any_order=True)
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_publish_retain(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
2015-10-10 20:25:31 +00:00
|
|
|
|
2015-10-10 13:01:17 +00:00
|
|
|
pub_client = MQTTClient()
|
|
|
|
ret = yield from pub_client.connect('mqtt://localhost/')
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
ret_message = yield from pub_client.publish('/topic', b'data', QOS_0, retain=True)
|
|
|
|
yield from pub_client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
self.assertIn('/topic', broker._retained_messages)
|
|
|
|
retained_message = broker._retained_messages['/topic']
|
|
|
|
self.assertEquals(retained_message.source_session, pub_client.session)
|
|
|
|
self.assertEquals(retained_message.topic, '/topic')
|
|
|
|
self.assertEquals(retained_message.data, b'data')
|
|
|
|
self.assertEquals(retained_message.qos, QOS_0)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
2015-10-10 20:25:31 +00:00
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_subscribe_publish(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
|
|
|
sub_client = MQTTClient()
|
|
|
|
yield from sub_client.connect('mqtt://localhost')
|
|
|
|
ret = yield from sub_client.subscribe([('/qos0', QOS_0), ('/qos1', QOS_1), ('/qos2', QOS_2)])
|
|
|
|
self.assertEquals(ret, [QOS_0, QOS_1, QOS_2])
|
|
|
|
|
|
|
|
yield from self._client_publish('/qos0', b'data', QOS_0)
|
|
|
|
yield from self._client_publish('/qos1', b'data', QOS_1)
|
|
|
|
yield from self._client_publish('/qos2', b'data', QOS_2)
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
for qos in [QOS_0, QOS_1, QOS_2]:
|
|
|
|
message = yield from sub_client.deliver_message()
|
|
|
|
self.assertIsNotNone(message)
|
|
|
|
self.assertEquals(message.topic, '/qos%s' % qos)
|
|
|
|
self.assertEquals(message.data, b'data')
|
|
|
|
self.assertEquals(message.qos, qos)
|
|
|
|
yield from sub_client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
|
|
|
|
|
|
|
@patch('hbmqtt.broker.PluginManager')
|
|
|
|
def test_client_publish_retain_subscribe(self, MockPluginManager):
|
|
|
|
def test_coro():
|
|
|
|
try:
|
|
|
|
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
|
|
|
|
yield from broker.start()
|
|
|
|
self.assertTrue(broker.transitions.is_started())
|
|
|
|
sub_client = MQTTClient()
|
|
|
|
yield from sub_client.connect('mqtt://localhost', cleansession=False)
|
|
|
|
ret = yield from sub_client.subscribe([('/qos0', QOS_0), ('/qos1', QOS_1), ('/qos2', QOS_2)])
|
|
|
|
self.assertEquals(ret, [QOS_0, QOS_1, QOS_2])
|
|
|
|
yield from sub_client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
yield from self._client_publish('/qos0', b'data', QOS_0, retain=True)
|
|
|
|
yield from self._client_publish('/qos1', b'data', QOS_1, retain=True)
|
|
|
|
yield from self._client_publish('/qos2', b'data', QOS_2, retain=True)
|
|
|
|
yield from sub_client.reconnect()
|
|
|
|
for qos in [QOS_0, QOS_1, QOS_2]:
|
|
|
|
log.debug("TEST QOS: %d" % qos)
|
|
|
|
message = yield from sub_client.deliver_message()
|
|
|
|
log.debug("Message: " + repr(message.publish_packet))
|
|
|
|
self.assertIsNotNone(message)
|
|
|
|
self.assertEquals(message.topic, '/qos%s' % qos)
|
|
|
|
self.assertEquals(message.data, b'data')
|
|
|
|
self.assertEquals(message.qos, qos)
|
|
|
|
yield from sub_client.disconnect()
|
|
|
|
yield from asyncio.sleep(0.1)
|
|
|
|
yield from broker.shutdown()
|
|
|
|
self.assertTrue(broker.transitions.is_stopped())
|
|
|
|
future.set_result(True)
|
|
|
|
except Exception as ae:
|
|
|
|
future.set_exception(ae)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def _client_publish(self, topic, data, qos, retain=False):
|
|
|
|
pub_client = MQTTClient()
|
|
|
|
ret = yield from pub_client.connect('mqtt://localhost/')
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
ret = yield from pub_client.publish(topic, data, qos, retain)
|
|
|
|
yield from pub_client.disconnect()
|
|
|
|
return ret
|