Skip to content

Commit

Permalink
test: Add test for messages iterator len()
Browse files Browse the repository at this point in the history
  • Loading branch information
empicano committed Aug 6, 2024
1 parent 5a636b4 commit 7713377
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,33 @@ async def handle() -> None:
await client.subscribe(topic)
await client.publish(topic, "foo")
await client.publish(topic, "bar")


@pytest.mark.network
async def test_messages_view_len() -> None:
"""Test that the ``__len__`` method of the messages view works correctly."""
topic = TOPIC_PREFIX + "test_messages_view_len"
count = 3

class TestClient(Client):
fut: asyncio.Future[None] = asyncio.Future()

def _on_message(
self, client: mqtt.Client, userdata: Any, message: mqtt.MQTTMessage
) -> None:
super()._on_message(client, userdata, message)
self.fut.set_result(None)
self.fut = asyncio.Future()

async with TestClient(HOSTNAME) as client:
assert len(client.messages) == 0
await client.subscribe(topic, qos=2)
# Publish a message and wait for it to arrive
for index in range(count):
await client.publish(topic, None, qos=2)
await asyncio.wait_for(client.fut, timeout=1)
assert len(client.messages) == index + 1
# Empty the queue
for _ in range(count):
await client.messages.__anext__()
assert len(client.messages) == 0

0 comments on commit 7713377

Please sign in to comment.