2015-09-27 19:17:42 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
import unittest
|
|
|
|
import asyncio
|
2015-09-27 19:39:43 +00:00
|
|
|
import os
|
2015-09-27 19:17:42 +00:00
|
|
|
import logging
|
2015-10-07 20:42:04 +00:00
|
|
|
from hbmqtt.client import MQTTClient, ConnectException
|
2015-09-27 20:07:09 +00:00
|
|
|
from hbmqtt.mqtt.constants import *
|
2015-09-27 19:17:42 +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__)
|
|
|
|
|
|
|
|
|
|
|
|
class MQTTClientTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
def test_connect_tcp(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 19:17:42 +00:00
|
|
|
try:
|
|
|
|
client = MQTTClient()
|
2015-11-01 14:58:20 +00:00
|
|
|
ret = yield from client.connect('mqtt://test.mosquitto.org/')
|
2015-09-27 19:17:42 +00:00
|
|
|
self.assertIsNotNone(client.session)
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.disconnect()
|
2015-09-27 19:17:42 +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()
|
|
|
|
|
|
|
|
def test_connect_tcp_secure(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 19:17:42 +00:00
|
|
|
try:
|
|
|
|
client = MQTTClient()
|
2015-09-27 19:39:43 +00:00
|
|
|
ca = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mosquitto.org.crt')
|
2015-11-01 14:58:20 +00:00
|
|
|
ret = yield from client.connect('mqtts://test.mosquitto.org/', cafile=ca)
|
2015-09-27 19:17:42 +00:00
|
|
|
self.assertIsNotNone(client.session)
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.disconnect()
|
2015-09-27 19:17:42 +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()
|
|
|
|
|
|
|
|
def test_connect_tcp_failure(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 19:17:42 +00:00
|
|
|
try:
|
2015-10-07 20:42:04 +00:00
|
|
|
config = {'auto_reconnect': False}
|
|
|
|
client = MQTTClient(config=config)
|
2015-11-01 14:58:20 +00:00
|
|
|
ret = yield from client.connect('mqtt://localhost/')
|
2015-10-07 20:42:04 +00:00
|
|
|
except ConnectException as e:
|
2015-09-27 19:17:42 +00:00
|
|
|
future.set_result(True)
|
|
|
|
|
|
|
|
future = asyncio.Future(loop=self.loop)
|
|
|
|
self.loop.run_until_complete(test_coro())
|
|
|
|
if future.exception():
|
|
|
|
raise future.exception()
|
|
|
|
|
|
|
|
def test_connect_ws(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 19:17:42 +00:00
|
|
|
try:
|
|
|
|
client = MQTTClient()
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.connect('ws://test.mosquitto.org:8080/')
|
2015-09-27 19:17:42 +00:00
|
|
|
self.assertIsNotNone(client.session)
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.disconnect()
|
2015-09-27 19:17:42 +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()
|
|
|
|
|
|
|
|
def test_connect_ws_secure(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 19:17:42 +00:00
|
|
|
try:
|
|
|
|
client = MQTTClient()
|
2015-09-27 19:39:43 +00:00
|
|
|
ca = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mosquitto.org.crt')
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.connect('wss://test.mosquitto.org:8081/', cafile=ca)
|
2015-09-27 19:17:42 +00:00
|
|
|
self.assertIsNotNone(client.session)
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.disconnect()
|
2015-09-27 19:17:42 +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()
|
|
|
|
|
|
|
|
def test_ping(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 19:17:42 +00:00
|
|
|
try:
|
|
|
|
client = MQTTClient()
|
2015-11-01 14:58:20 +00:00
|
|
|
ret = yield from client.connect('mqtt://test.mosquitto.org/')
|
2015-09-27 19:17:42 +00:00
|
|
|
self.assertIsNotNone(client.session)
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.ping()
|
|
|
|
yield from client.disconnect()
|
2015-09-27 19:17:42 +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-09-27 20:07:09 +00:00
|
|
|
|
|
|
|
def test_subscribe(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 20:07:09 +00:00
|
|
|
try:
|
|
|
|
client = MQTTClient()
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.connect('mqtt://test.mosquitto.org/')
|
2015-09-27 20:07:09 +00:00
|
|
|
self.assertIsNotNone(client.session)
|
2015-11-01 14:58:20 +00:00
|
|
|
ret = yield from client.subscribe([
|
2015-09-27 20:07:09 +00:00
|
|
|
('$SYS/broker/uptime', QOS_0),
|
|
|
|
('$SYS/broker/uptime', QOS_1),
|
|
|
|
('$SYS/broker/uptime', QOS_2),
|
|
|
|
])
|
|
|
|
self.assertEquals(ret[0], QOS_0)
|
|
|
|
self.assertEquals(ret[1], QOS_1)
|
|
|
|
self.assertEquals(ret[2], QOS_2)
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.disconnect()
|
2015-09-27 20:07:09 +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()
|
|
|
|
|
|
|
|
def test_unsubscribe(self):
|
2015-11-01 14:58:20 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def test_coro():
|
2015-09-27 20:07:09 +00:00
|
|
|
try:
|
|
|
|
client = MQTTClient()
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.connect('mqtt://test.mosquitto.org/')
|
2015-09-27 20:07:09 +00:00
|
|
|
self.assertIsNotNone(client.session)
|
2015-11-01 14:58:20 +00:00
|
|
|
ret = yield from client.subscribe([
|
2015-09-27 20:07:09 +00:00
|
|
|
('$SYS/broker/uptime', QOS_0),
|
|
|
|
])
|
|
|
|
self.assertEquals(ret[0], QOS_0)
|
2015-11-01 14:58:20 +00:00
|
|
|
yield from client.unsubscribe(['$SYS/broker/uptime'])
|
|
|
|
yield from client.disconnect()
|
2015-09-27 20:07:09 +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()
|