amqtt/tests/plugins/test_manager.py

94 wiersze
3.1 KiB
Python
Czysty Zwykły widok Historia

2015-08-16 19:37:18 +00:00
import asyncio
import logging
2024-12-29 18:23:27 +00:00
from typing import Any
import unittest
2021-03-06 15:20:48 +00:00
2024-12-29 18:23:27 +00:00
from amqtt.plugins.manager import BaseContext, Plugin, PluginManager
2015-08-16 19:37:18 +00:00
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
2015-08-17 19:52:26 +00:00
logging.basicConfig(level=logging.INFO, format=formatter)
2015-08-16 19:37:18 +00:00
2021-03-21 17:00:10 +00:00
class EmptyTestPlugin:
2024-12-29 18:23:27 +00:00
def __init__(self, context: BaseContext) -> None:
2015-08-20 19:43:18 +00:00
self.context = context
2015-08-16 19:37:18 +00:00
class EventTestPlugin:
2024-12-29 18:23:27 +00:00
def __init__(self, context: BaseContext) -> None:
2015-08-20 19:43:18 +00:00
self.context = context
2015-08-16 19:37:18 +00:00
self.test_flag = False
self.coro_flag = False
2024-12-29 18:23:27 +00:00
async def on_test(self) -> None:
2015-08-16 19:37:18 +00:00
self.test_flag = True
2015-08-20 19:43:18 +00:00
self.context.logger.info("on_test")
2015-08-16 19:37:18 +00:00
2024-12-29 18:23:27 +00:00
async def test_coro(self) -> None:
2015-08-16 19:37:18 +00:00
self.coro_flag = True
2024-12-29 18:23:27 +00:00
async def ret_coro(self) -> str:
return "TEST"
2015-08-16 19:37:18 +00:00
class TestPluginManager(unittest.TestCase):
2024-12-29 18:23:27 +00:00
def setUp(self) -> None:
2015-08-16 19:37:18 +00:00
self.loop = asyncio.new_event_loop()
2024-12-29 18:23:27 +00:00
def test_load_plugin(self) -> None:
2021-03-27 12:16:42 +00:00
manager = PluginManager("amqtt.test.plugins", context=None)
2021-03-06 17:37:23 +00:00
assert len(manager._plugins) > 0
2015-08-16 19:37:18 +00:00
2024-12-29 18:23:27 +00:00
def test_fire_event(self) -> None:
async def fire_event() -> None:
2020-12-31 00:16:45 +00:00
await manager.fire_event("test")
await asyncio.sleep(1)
2020-12-31 00:16:45 +00:00
await manager.close()
2015-08-19 11:54:09 +00:00
manager = PluginManager("amqtt.test.plugins", context=None)
2015-08-19 11:54:09 +00:00
self.loop.run_until_complete(fire_event())
plugin = manager.get_plugin("event_plugin")
2024-12-29 18:23:27 +00:00
assert plugin is not None
2021-03-06 17:37:23 +00:00
assert plugin.object.test_flag
2015-08-19 11:54:09 +00:00
2024-12-29 18:23:27 +00:00
def test_fire_event_wait(self) -> None:
async def fire_event() -> None:
2020-12-31 00:16:45 +00:00
await manager.fire_event("test", wait=True)
await manager.close()
2015-08-16 19:37:18 +00:00
manager = PluginManager("amqtt.test.plugins", context=None)
2015-08-16 19:37:18 +00:00
self.loop.run_until_complete(fire_event())
plugin = manager.get_plugin("event_plugin")
2024-12-29 18:23:27 +00:00
assert plugin is not None
2021-03-06 17:37:23 +00:00
assert plugin.object.test_flag
2015-08-16 19:37:18 +00:00
2024-12-29 18:23:27 +00:00
def test_map_coro(self) -> None:
async def call_coro() -> None:
await manager.map_plugin_coro("test_coro")
2015-08-16 19:37:18 +00:00
manager = PluginManager("amqtt.test.plugins", context=None)
2015-08-16 19:37:18 +00:00
self.loop.run_until_complete(call_coro())
plugin = manager.get_plugin("event_plugin")
2024-12-29 18:23:27 +00:00
assert plugin is not None
2021-03-06 17:37:23 +00:00
assert plugin.object.test_coro
2024-12-29 18:23:27 +00:00
def test_map_coro_return(self) -> None:
async def call_coro() -> dict[Plugin, str]:
return await manager.map_plugin_coro("ret_coro")
manager = PluginManager("amqtt.test.plugins", context=None)
ret = self.loop.run_until_complete(call_coro())
plugin = manager.get_plugin("event_plugin")
2024-12-29 18:23:27 +00:00
assert plugin is not None
assert ret[plugin] == "TEST"
2024-12-29 18:23:27 +00:00
def test_map_coro_filter(self) -> None:
"""Run plugin coro but expect no return as an empty filter is given."""
2024-12-29 18:23:27 +00:00
async def call_coro() -> dict[Plugin, Any]:
return await manager.map_plugin_coro("ret_coro", filter_plugins=[])
manager = PluginManager("amqtt.test.plugins", context=None)
ret = self.loop.run_until_complete(call_coro())
2021-03-06 17:37:23 +00:00
assert len(ret) == 0