Skip to content

Commit

Permalink
♻️ Migrate tests from hbmqtt to aiomqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
agmangas committed Oct 24, 2023
1 parent 14dbd33 commit 15e11d7
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 730 deletions.
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@

if is_mqtt_supported():
install_requires.append("aiomqtt>=1.2,<2.0")
test_requires.append("hbmqtt>=0.9.4,<1.0")
test_requires.append("websockets>=8.0,<9.0")

if is_dnssd_supported():
install_requires.append("zeroconf>=0.30.0,<0.37.0")
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

coloredlogs.install(level=logging.DEBUG)

for logger_name in ["hbmqtt", "faker", "transitions.core"]:
for logger_name in ["faker", "transitions.core"]:
logging.getLogger(logger_name).setLevel(logging.WARNING)
except ImportError:
pass
25 changes: 5 additions & 20 deletions tests/protocols/coap/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def triple(parameters):

server = CoAPServer(port=port, **request.param)
server.add_exposed_thing(exposed_thing)
server.start()

@tornado.gen.coroutine
def start():
Expand All @@ -89,22 +88,16 @@ def stop():


@pytest.fixture
def coap_servient():
async def coap_servient():
"""Returns a Servient that exposes a CoAP server and one ExposedThing."""

from wotpy.protocols.coap.server import CoAPServer

coap_port = find_free_port()
the_coap_server = CoAPServer(port=coap_port)

servient = Servient(catalogue_port=None)
servient.add_server(the_coap_server)

@tornado.gen.coroutine
def start():
raise tornado.gen.Return((yield servient.start()))

wot = tornado.ioloop.IOLoop.current().run_sync(start)
wot = await servient.start()

property_name_01 = uuid.uuid4().hex
action_name_01 = uuid.uuid4().hex
Expand All @@ -124,21 +117,13 @@ def start():
}

td = ThingDescription(td_dict)

exposed_thing = wot.produce(td.to_str())
exposed_thing.expose()

@tornado.gen.coroutine
def action_handler(parameters):
async def action_handler(parameters):
input_value = parameters.get("input")
raise tornado.gen.Return(int(input_value) * 2)
return int(input_value) * 2

exposed_thing.set_action_handler(action_name_01, action_handler)

yield servient

@tornado.gen.coroutine
def shutdown():
yield servient.shutdown()

tornado.ioloop.IOLoop.current().run_sync(shutdown)
await servient.shutdown()
73 changes: 45 additions & 28 deletions tests/protocols/coap/test_client.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,72 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tests.protocols.helpers import \
client_test_on_property_change, \
client_test_on_event, \
client_test_read_property, \
client_test_write_property, \
client_test_invoke_action, \
client_test_invoke_action_error, \
client_test_on_property_change_error
import pytest

from tests.protocols.helpers import (
client_test_invoke_action_async,
client_test_invoke_action_error_async,
client_test_on_event_async,
client_test_on_property_change_async,
client_test_on_property_change_error_async,
client_test_read_property_async,
client_test_write_property_async,
)
from wotpy.protocols.coap.client import CoAPClient


def test_read_property(coap_servient):
"""The CoAP client can read properties."""
@pytest.mark.asyncio
async def test_read_property(coap_servient):
"""Property values may be retrieved using the CoAP binding client."""

client_test_read_property(coap_servient, CoAPClient)
async for servient in coap_servient:
await client_test_read_property_async(servient, CoAPClient)


def test_write_property(coap_servient):
"""The CoAP client can write properties."""
@pytest.mark.asyncio
async def test_write_property(coap_servient):
"""Properties may be updated using the CoAP binding client."""

client_test_write_property(coap_servient, CoAPClient)
async for servient in coap_servient:
await client_test_write_property_async(servient, CoAPClient)


def test_on_property_change(coap_servient):
@pytest.mark.asyncio
async def test_on_property_change(coap_servient):
"""The CoAP client can subscribe to property updates."""

client_test_on_property_change(coap_servient, CoAPClient)
async for servient in coap_servient:
await client_test_on_property_change_async(servient, CoAPClient)


def test_on_property_change_error(coap_servient):
"""Errors that arise in the middle of an ongoing Property
observation are propagated to the subscription as expected."""
@pytest.mark.asyncio
async def test_invoke_action(coap_servient):
"""The CoAP client can invoke actions."""

client_test_on_property_change_error(coap_servient, CoAPClient)
async for servient in coap_servient:
await client_test_invoke_action_async(servient, CoAPClient)


def test_invoke_action(coap_servient):
"""The CoAP client can invoke actions."""
@pytest.mark.asyncio
async def test_on_event(coap_servient):
"""The CoAP client can subscribe to event emissions."""

client_test_invoke_action(coap_servient, CoAPClient)
async for servient in coap_servient:
await client_test_on_event_async(servient, CoAPClient)


def test_invoke_action_error(coap_servient):
@pytest.mark.asyncio
async def test_invoke_action_error(coap_servient):
"""Errors raised by Actions are propagated propertly by the CoAP binding client."""

client_test_invoke_action_error(coap_servient, CoAPClient)
async for servient in coap_servient:
await client_test_invoke_action_error_async(servient, CoAPClient)


def test_on_event(coap_servient):
"""The CoAP client can subscribe to event emissions."""
@pytest.mark.asyncio
async def test_on_property_change_error(coap_servient):
"""Errors that arise in the middle of an ongoing Property
observation are propagated to the subscription as expected."""

client_test_on_event(coap_servient, CoAPClient)
async for servient in coap_servient:
await client_test_on_property_change_error_async(servient, CoAPClient)
43 changes: 19 additions & 24 deletions tests/protocols/coap/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-

import asyncio
import datetime
import json

import aiocoap
Expand Down Expand Up @@ -67,48 +66,44 @@ def _next_observation(request):
raise tornado.gen.Return(val)


def test_start_stop():
@pytest.mark.asyncio
async def test_start_stop():
"""The CoAP server can be started and stopped."""

coap_port = find_free_port()
coap_server = CoAPServer(port=coap_port)
ping_uri = "coap://127.0.0.1:{}/.well-known/core".format(coap_port)

@tornado.gen.coroutine
def ping():
async def ping():
try:
coap_client = yield aiocoap.Context.create_client_context()
coap_client = await aiocoap.Context.create_client_context()
request_msg = aiocoap.Message(code=aiocoap.Code.GET, uri=ping_uri)
response = yield tornado.gen.with_timeout(
datetime.timedelta(seconds=2), coap_client.request(request_msg).response
response = await asyncio.wait_for(
coap_client.request(request_msg).response, timeout=2
)
except Exception:
raise tornado.gen.Return(False)

raise tornado.gen.Return(response.code.is_successful())
return False

@tornado.gen.coroutine
def test_coroutine():
assert not (yield ping())
return response.code.is_successful()

yield coap_server.start()
assert not (await ping())

assert (yield ping())
assert (yield ping())
await coap_server.start()

for _ in range(5):
yield coap_server.stop()
assert await ping()
assert await ping()

assert not (yield ping())
for _ in range(5):
await coap_server.stop()

yield coap_server.stop()
assert not (await ping())

for _ in range(5):
yield coap_server.start()
await coap_server.stop()

assert (yield ping())
for _ in range(5):
await coap_server.start()

run_test_coroutine(test_coroutine)
assert await ping()


def test_property_read(coap_server):
Expand Down
37 changes: 12 additions & 25 deletions tests/protocols/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import uuid

import pytest
import tornado.gen
import tornado.ioloop
from faker import Faker
from OpenSSL import crypto

from tests.utils import find_free_port
from wotpy.protocols.http.server import HTTPServer
from wotpy.protocols.ws.server import WebsocketServer
Expand All @@ -20,7 +19,7 @@


@pytest.fixture
def all_protocols_servient():
async def all_protocols_servient():
"""Returns a Servient configured to use all available protocol bindings."""

servient = Servient(catalogue_port=None)
Expand All @@ -35,47 +34,35 @@ def all_protocols_servient():

if is_coap_supported():
from wotpy.protocols.coap.server import CoAPServer

coap_port = find_free_port()
coap_server = CoAPServer(port=coap_port)
servient.add_server(coap_server)

if is_mqtt_supported():
from tests.protocols.mqtt.broker import (get_test_broker_url,
is_test_broker_online)
from tests.protocols.mqtt.broker import (
get_test_broker_url,
is_test_broker_online_async,
)
from wotpy.protocols.mqtt.server import MQTTServer
if is_test_broker_online():

if await is_test_broker_online_async():
mqtt_server = MQTTServer(broker_url=get_test_broker_url())
servient.add_server(mqtt_server)

@tornado.gen.coroutine
def start():
raise tornado.gen.Return((yield servient.start()))

wot = tornado.ioloop.IOLoop.current().run_sync(start)
wot = await servient.start()

td_dict = {
"id": uuid.uuid4().urn,
"title": uuid.uuid4().hex,
"properties": {
uuid.uuid4().hex: {
"observable": True,
"type": "string"
}
}
"properties": {uuid.uuid4().hex: {"observable": True, "type": "string"}},
}

td = ThingDescription(td_dict)

exposed_thing = wot.produce(td.to_str())
exposed_thing.expose()

yield servient

@tornado.gen.coroutine
def shutdown():
yield servient.shutdown()

tornado.ioloop.IOLoop.current().run_sync(shutdown)
await servient.shutdown()


@pytest.fixture
Expand Down
Loading

0 comments on commit 15e11d7

Please sign in to comment.