Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests and coverage #145

Merged
merged 3 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: test suite

on:
push:
branches: [master]
pull_request:

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest]
python-version:
["3.7", "3.8", "3.9", "3.10", "3.11", pypy-3.8, pypy-3.9]
include:
- os: macos-latest
python-version: "3.7"
- os: macos-latest
python-version: "3.11"
- os: windows-latest
python-version: "3.7"
- os: windows-latest
python-version: "3.11"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: pip-test-${{ matrix.python-version }}-${{ matrix.os }}
- name: Install dependencies
run: pip install .[tests]
- name: Start Mosquitto MQTT Broker
uses: Namoshek/mosquitto-github-action@v1
- name: Test with pytest
run: pytest --cov=asyncio_mqtt --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
name: ${{ matrix.os }} Python ${{ matrix.python-version }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
local_test.py
.venv
.idea/
.coverage
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ repos:
hooks:
- id: mypy
additional_dependencies:
- anyio
- pytest
- types-paho-mqtt == 1.6.0.1

- repo: https://github.com/pre-commit/mirrors-prettier
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<a href="https://github.com/sbtinstruments/asyncio-mqtt/blob/main/LICENSE"><img alt="License: BSD-3-Clause" src="https://img.shields.io/github/license/sbtinstruments/asyncio-mqtt"></a>
<a href="https://pypi.org/project/asyncio-mqtt"><img alt="PyPI version" src="https://img.shields.io/pypi/v/asyncio-mqtt"></a>
<a href="https://pypi.org/project/asyncio-mqtt"><img alt="PyPI downloads" src="https://img.shields.io/pypi/dm/asyncio-mqtt"></a>
<a href="https://github.com/sbtinstruments/asyncio-mqtt/actions/workflows/test.yml"><img alt="Coverage" src="https://github.com/sbtinstruments/asyncio-mqtt/actions/workflows/test.yml/badge.svg"></a>
<a href="https://codecov.io/gh/sbtinstruments/asyncio-mqtt"><img alt="Coverage" src="https://img.shields.io/codecov/c/github/sbtinstruments/asyncio-mqtt"></a>
<a href="https://results.pre-commit.ci/latest/github/sbtinstruments/asyncio-mqtt/master"><img alt="pre-commit.ci status" src="https://results.pre-commit.ci/badge/github/sbtinstruments/asyncio-mqtt/master.svg"></a>
<a href="https://github.com/sbtinstruments/asyncio-mqtt"><img alt="Typing: strict" src="https://img.shields.io/badge/typing-strict-green.svg"></a>
<a href="https://github.com/sbtinstruments/asyncio-mqtt"><img alt="Code Style: Black" src="https://img.shields.io/badge/code%20style-black-black"></a>
Expand Down
10 changes: 5 additions & 5 deletions asyncio_mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
cast,
)

if sys.version_info >= (3, 10):
if sys.version_info >= (3, 10): # pragma: no cover
from typing import ParamSpec
else:
else: # pragma: no cover
from typing_extensions import ParamSpec

if sys.version_info >= (3, 7):
from contextlib import asynccontextmanager
else:
else: # pragma: no cover
from async_generator import asynccontextmanager as _asynccontextmanager

_P = ParamSpec("_P")
Expand Down Expand Up @@ -415,7 +415,7 @@ async def publish(
@asynccontextmanager
async def filtered_messages(
self, topic_filter: str, *, queue_maxsize: int = 0
) -> AsyncIterator[AsyncGenerator[mqtt.MQTTMessage, None]]:
) -> AsyncGenerator[AsyncGenerator[mqtt.MQTTMessage, None], None]:
"""Return async generator of messages that match the given filter.

Use queue_maxsize to restrict the queue size. If the queue is full,
Expand All @@ -441,7 +441,7 @@ async def filtered_messages(
@asynccontextmanager
async def unfiltered_messages(
self, *, queue_maxsize: int = 0
) -> AsyncIterator[AsyncGenerator[mqtt.MQTTMessage, None]]:
) -> AsyncGenerator[AsyncGenerator[mqtt.MQTTMessage, None], None]:
"""Return async generator of all messages that are not caught in filters."""
# Early out
if self._client.on_message is not None:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
extras_require={
"lint": ["mypy>=0.982", "flake8>=5.0.4", "types-paho-mqtt>=1.6.0.1"],
"format": ["black>=22.10.0", "isort>=5.10.1"],
"tests": ["pytest>=7.2.0", "pytest-cov>=4.0.0", "anyio>=3.6.2"],
},
)
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture
def anyio_backend() -> str:
return "asyncio"
109 changes: 109 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import anyio
import anyio.abc
import pytest

from asyncio_mqtt import Client
from asyncio_mqtt.client import ProtocolVersion, Will

pytestmark = pytest.mark.anyio


async def test_client_filtered_messages() -> None:
topic_header = "tests/asyncio_mqtt/filtered_messages/"
good_topic = topic_header + "good"
bad_topic = topic_header + "bad"

async def handle_messages(tg: anyio.abc.TaskGroup) -> None:
async with client.filtered_messages(good_topic) as messages:
async for message in messages:
assert message.topic == good_topic
tg.cancel_scope.cancel()

async with Client("localhost") as client:
async with anyio.create_task_group() as tg:
await client.subscribe(topic_header + "#")
tg.start_soon(handle_messages, tg)
await client.publish(bad_topic)
await client.publish(good_topic)


async def test_client_unfiltered_messages() -> None:
topic_header = "tests/asyncio_mqtt/unfiltered_messages/"
topic_filtered = topic_header + "filtered"
topic_unfiltered = topic_header + "unfiltered"

async def handle_unfiltered_messages(tg: anyio.abc.TaskGroup) -> None:
async with client.unfiltered_messages() as messages:
async for message in messages:
assert message.topic == topic_unfiltered
tg.cancel_scope.cancel()

async def handle_filtered_messages() -> None:
async with client.filtered_messages(topic_filtered) as messages:
async for message in messages:
assert message.topic == topic_filtered

async with Client("localhost") as client:
async with anyio.create_task_group() as tg:
await client.subscribe(topic_header + "#")
tg.start_soon(handle_filtered_messages)
tg.start_soon(handle_unfiltered_messages, tg)
await client.publish(topic_filtered)
await client.publish(topic_unfiltered)


async def test_client_unsubscribe() -> None:
topic_header = "tests/asyncio_mqtt/unsubscribe/"
topic1 = topic_header + "1"
topic2 = topic_header + "2"

async def handle_messages(tg: anyio.abc.TaskGroup) -> None:
async with client.unfiltered_messages() as messages:
i = 0
async for message in messages:
if i == 0:
assert message.topic == topic1
elif i == 1:
assert message.topic == topic2
tg.cancel_scope.cancel()
i += 1

async with Client("localhost") as client:
async with anyio.create_task_group() as tg:
await client.subscribe(topic1)
await client.subscribe(topic2)
tg.start_soon(handle_messages, tg)
await client.publish(topic1)
await client.unsubscribe(topic1)
await client.publish(topic1)
await client.publish(topic2)


@pytest.mark.parametrize(
"protocol, length",
((ProtocolVersion.V31, 22), (ProtocolVersion.V311, 0), (ProtocolVersion.V5, 0)),
)
async def test_client_id(protocol: ProtocolVersion, length: int) -> None:
client = Client("localhost", protocol=protocol)
assert len(client.id) == length


async def test_client_will() -> None:
topic = "tests/asyncio_mqtt/will"
event = anyio.Event()

async def launch_client() -> None:
with anyio.CancelScope(shield=True) as cs:
async with Client("localhost") as client:
await client.subscribe(topic)
event.set()
async with client.filtered_messages(topic) as messages:
async for message in messages:
assert message.topic == topic
cs.cancel()

async with anyio.create_task_group() as tg:
tg.start_soon(launch_client)
await event.wait()
async with Client("localhost", will=Will(topic)) as client:
client._client._sock_close() # type: ignore[attr-defined]