From: Benjamin Braatz Date: Sun, 21 Mar 2021 02:03:28 +0000 (+0100) Subject: Adapt message order in some tests. X-Git-Tag: v0.3.0~49 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=c23f531a668d5cd42ff95991dad4228f9b92dedf;p=graphit%2Fcontrolpi.git Adapt message order in some tests. --- diff --git a/controlpi-plugins/util.py b/controlpi-plugins/util.py index d3b42fd..313e50b 100644 --- a/controlpi-plugins/util.py +++ b/controlpi-plugins/util.py @@ -4,7 +4,6 @@ - Init sends list of messages on startup and on demand. - Alias translates messages to an alias. ->>> import asyncio >>> import controlpi >>> asyncio.run(controlpi.test( ... {"Test Log": {"plugin": "Log", @@ -37,6 +36,8 @@ test(): {'sender': 'Test Alias', 'id': 'translated', Test Log: {'sender': 'Test Alias', 'id': 'translated', 'content': 'Test Message'} """ +import asyncio + from controlpi import BasePlugin, Message, MessageTemplate @@ -50,7 +51,6 @@ class Log(BasePlugin): template and are logged by the instance "Test Log", while the second message does not match and is only logged by the test, but not by the Log instance: - >>> import asyncio >>> import controlpi >>> asyncio.run(controlpi.test( ... {"Test Log": {"plugin": "Log", @@ -130,7 +130,6 @@ class Init(BasePlugin): In the example, the two configured messages are sent twice, once at startup and a second time in reaction to the "execute" command sent by the test: - >>> import asyncio >>> import controlpi >>> asyncio.run(controlpi.test( ... {"Test Init": {"plugin": "Init", @@ -197,6 +196,8 @@ class Init(BasePlugin): """Send configured messages.""" for message in self.conf['messages']: await self.bus.send(Message(self.name, message)) + # Give immediate reactions to messages opportunity to happen: + await asyncio.sleep(0) def process_conf(self) -> None: """Register plugin as bus client.""" @@ -222,7 +223,6 @@ class Execute(BasePlugin): In the example, the first command sent by the test sets two messages, which are then sent in reaction to the second command sent by the test: - >>> import asyncio >>> import controlpi >>> asyncio.run(controlpi.test( ... {"Test Execute": {"plugin": "Execute"}}, @@ -266,6 +266,8 @@ class Execute(BasePlugin): elif message['command'] == 'execute': for message in self.messages: await self.bus.send(Message(self.name, message)) + # Give immediate reactions to messages opportunity to happen: + await asyncio.sleep(0) def process_conf(self) -> None: """Register plugin as bus client.""" @@ -298,7 +300,6 @@ class Alias(BasePlugin): In the example, the two messages sent by the test are translated by the Alias instance and the translated messages are sent by it preserving the "content" keys: - >>> import asyncio >>> import controlpi >>> asyncio.run(controlpi.test( ... {"Test Alias": {"plugin": "Alias", @@ -313,10 +314,10 @@ class Alias(BasePlugin): 'receives': [{'id': {'const': 42}}]} test(): {'sender': 'test()', 'id': 42, 'content': 'Test Message'} - test(): {'sender': 'test()', 'id': 42, - 'content': 'Second Message'} test(): {'sender': 'Test Alias', 'id': 'translated', 'content': 'Test Message'} + test(): {'sender': 'test()', 'id': 42, + 'content': 'Second Message'} test(): {'sender': 'Test Alias', 'id': 'translated', 'content': 'Second Message'} diff --git a/controlpi/__init__.py b/controlpi/__init__.py index 22d4e9e..e3e5d56 100644 --- a/controlpi/__init__.py +++ b/controlpi/__init__.py @@ -168,6 +168,6 @@ async def test(conf: Dict[str, PluginConf], await asyncio.sleep(0) for message in messages: await message_bus.send(Message('test()', message)) - # Give immediate reactions to these messages opportunity to happen: + # Give immediate reactions to messages opportunity to happen: await asyncio.sleep(0) await message_bus._queue.join()