From 72e6f93a790546c2a8871f596ddb14b5200b510b Mon Sep 17 00:00:00 2001 From: rmorshea Date: Sun, 13 Sep 2020 13:53:54 -0700 Subject: [PATCH] test dispatcher start and stop --- idom/client/app/package-lock.json | 2 +- idom/core/dispatcher.py | 14 ++++----- tests/test_core/test_dispatcher.py | 50 +++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/idom/client/app/package-lock.json b/idom/client/app/package-lock.json index ecc4cafda..d71b84e58 100644 --- a/idom/client/app/package-lock.json +++ b/idom/client/app/package-lock.json @@ -1,6 +1,6 @@ { "name": "idom-client-react", - "version": "0.4.1", + "version": "0.4.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/idom/core/dispatcher.py b/idom/core/dispatcher.py index 2b3c86f0d..cfe4c5c14 100644 --- a/idom/core/dispatcher.py +++ b/idom/core/dispatcher.py @@ -26,6 +26,13 @@ def __init__(self, layout: Layout) -> None: super().__init__() self._layout = layout + async def start(self) -> None: + await self.__aenter__() + + async def stop(self) -> None: + await self.task_group.cancel_scope.cancel() + await self.__aexit__(None, None, None) + @async_resource async def layout(self) -> AsyncIterator[Layout]: async with self._layout as layout: @@ -99,13 +106,6 @@ def __init__(self, layout: Layout) -> None: self._model_state: Any = {} self._update_queues: Dict[str, asyncio.Queue[LayoutUpdate]] = {} - async def start(self) -> None: - await self.__aenter__() - - async def stop(self): - self.task_group.cancel_scope.cancel() - await self.__aexit__(None, None, None) - @async_resource async def task_group(self) -> AsyncIterator[TaskGroup]: async with create_task_group() as group: diff --git a/tests/test_core/test_dispatcher.py b/tests/test_core/test_dispatcher.py index 685613014..43d874edd 100644 --- a/tests/test_core/test_dispatcher.py +++ b/tests/test_core/test_dispatcher.py @@ -6,7 +6,11 @@ import idom from idom.core.layout import Layout, LayoutEvent -from idom.core.dispatcher import SharedViewDispatcher, AbstractDispatcher +from idom.core.dispatcher import ( + SharedViewDispatcher, + AbstractDispatcher, + SingleViewDispatcher, +) async def test_shared_state_dispatcher(): @@ -120,3 +124,47 @@ async def recv(): with pytest.raises(ExceptionGroup, match="this is a bug"): async with DispatcherWithBug(idom.Layout(AnyElement())) as dispatcher: await dispatcher.run(send, recv, None) + + +async def test_dispatcher_start_stop(): + cancelled_recv = False + cancelled_send = False + + async def send(patch): + nonlocal cancelled_send + try: + await asyncio.sleep(100) + except asyncio.CancelledError: + cancelled_send = True + raise + else: + assert False, "this should never be reached" + + async def recv(): + nonlocal cancelled_recv + try: + await asyncio.sleep(100) + except asyncio.CancelledError: + cancelled_recv = True + raise + else: + assert False, "this should never be reached" + + @idom.element + def AnElement(): + return idom.html.div() + + dispatcher = SingleViewDispatcher(Layout(AnElement())) + + await dispatcher.start() + + await dispatcher.run(send, recv, None) + + # let it run until it hits the sleeping recv/send calls + for i in range(10): + await asyncio.sleep(0) + + await dispatcher.stop() + + assert cancelled_recv + assert cancelled_send