From 7df4f81388e937fd1ad3eefe23e11eb53613f37f Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Fri, 5 Mar 2021 11:05:14 +0100 Subject: [PATCH] asyncio.sleep(0.01) to asyncio.sleep(0) in tests A sleep time of 0 is enough to give control back to the event loop. --- controlpi/__init__.py | 5 +++-- controlpi/messagebus.py | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/controlpi/__init__.py b/controlpi/__init__.py index b472781..a2e3a53 100644 --- a/controlpi/__init__.py +++ b/controlpi/__init__.py @@ -47,7 +47,8 @@ when using the system in production: ... bus_task = asyncio.create_task(bus.run()) ... asyncio.create_task(p.run()) ... await bus.send({'sender': 'Test', 'target': 'Bus Test', 'key': 'v'}) -... await asyncio.sleep(0.01) +... await asyncio.sleep(0) +... await asyncio.sleep(0) ... bus_task.cancel() >>> asyncio.run(test_bus_plugin()) BusPlugin 'Bus Test' configured. @@ -67,7 +68,7 @@ client and its registered send and receive templates: ... bus_task = asyncio.create_task(bus.run()) ... await bus.send({'sender': 'Test', 'target': '', ... 'command': 'get clients'}) -... await asyncio.sleep(0.01) +... await asyncio.sleep(0) ... bus_task.cancel() >>> asyncio.run(test_bus()) BusPlugin 'Bus Test' configured. diff --git a/controlpi/messagebus.py b/controlpi/messagebus.py index 52ec624..ee5a242 100644 --- a/controlpi/messagebus.py +++ b/controlpi/messagebus.py @@ -59,7 +59,7 @@ have to explicitly cancel the task: ... await setup(bus) ... bus_task = asyncio.create_task(bus.run()) ... await send(bus) -... await asyncio.sleep(0.01) +... await asyncio.sleep(0) ... bus_task.cancel() >>> asyncio.run(main()) Sending messages. @@ -501,7 +501,7 @@ class MessageBus: ... await setup(bus) ... bus_task = asyncio.create_task(bus.run()) ... await send(bus) - ... await asyncio.sleep(0.01) + ... await asyncio.sleep(0) ... bus_task.cancel() >>> asyncio.run(main()) Setting up. @@ -609,7 +609,7 @@ class MessageBus: >>> async def main(): ... bus = MessageBus() ... bus_task = asyncio.create_task(bus.run()) - ... await asyncio.sleep(0.01) + ... await asyncio.sleep(0) ... bus_task.cancel() >>> asyncio.run(main()) """ @@ -645,7 +645,7 @@ class MessageBus: ... await bus.send({'sender': 'Client 2', 'target': 'Client 1'}) ... await bus.send({'sender': 'Client 1', 'target': 'Client 2', ... 'k1': 42}) - ... await asyncio.sleep(0.01) + ... await asyncio.sleep(0) ... bus_task.cancel() >>> asyncio.run(main()) Message not allowed for sender Client 1! -- 2.34.1