2015-08-16 19:37:18 +00:00
|
|
|
import asyncio
|
2024-12-21 10:52:26 +00:00
|
|
|
import logging
|
2024-12-29 18:23:27 +00:00
|
|
|
from typing import Any
|
2024-12-21 10:52:26 +00:00
|
|
|
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
|
|
|
|
2024-12-21 10:52:26 +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:
|
2015-08-21 11:08:52 +00:00
|
|
|
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:
|
2024-12-21 10:52:26 +00:00
|
|
|
async def fire_event() -> None:
|
2020-12-31 00:16:45 +00:00
|
|
|
await manager.fire_event("test")
|
2021-12-31 10:12:59 +00:00
|
|
|
await asyncio.sleep(1)
|
2020-12-31 00:16:45 +00:00
|
|
|
await manager.close()
|
2015-08-19 11:54:09 +00:00
|
|
|
|
2021-12-31 10:12:59 +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:
|
2024-12-21 10:52:26 +00:00
|
|
|
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
|
|
|
|
2021-12-31 10:12:59 +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:
|
2024-12-21 10:52:26 +00:00
|
|
|
async def call_coro() -> None:
|
2021-03-14 20:44:41 +00:00
|
|
|
await manager.map_plugin_coro("test_coro")
|
2015-08-16 19:37:18 +00:00
|
|
|
|
2021-12-31 10:12:59 +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
|
2015-08-21 11:08:52 +00:00
|
|
|
|
2024-12-29 18:23:27 +00:00
|
|
|
def test_map_coro_return(self) -> None:
|
|
|
|
async def call_coro() -> dict[Plugin, str]:
|
2021-03-14 20:44:41 +00:00
|
|
|
return await manager.map_plugin_coro("ret_coro")
|
2015-08-21 11:08:52 +00:00
|
|
|
|
2021-12-31 10:12:59 +00:00
|
|
|
manager = PluginManager("amqtt.test.plugins", context=None)
|
2015-08-21 11:08:52 +00:00
|
|
|
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
|
2024-12-21 10:52:26 +00:00
|
|
|
assert ret[plugin] == "TEST"
|
2015-08-21 11:37:57 +00:00
|
|
|
|
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."""
|
2021-03-14 20:44:41 +00:00
|
|
|
|
2024-12-29 18:23:27 +00:00
|
|
|
async def call_coro() -> dict[Plugin, Any]:
|
2021-03-14 20:44:41 +00:00
|
|
|
return await manager.map_plugin_coro("ret_coro", filter_plugins=[])
|
2015-08-21 11:37:57 +00:00
|
|
|
|
2021-12-31 10:12:59 +00:00
|
|
|
manager = PluginManager("amqtt.test.plugins", context=None)
|
2015-08-21 11:37:57 +00:00
|
|
|
ret = self.loop.run_until_complete(call_coro())
|
2021-03-06 17:37:23 +00:00
|
|
|
assert len(ret) == 0
|