Skip to content

Commit

Permalink
test dispatcher start and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Sep 13, 2020
1 parent debca80 commit 72e6f93
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion idom/client/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions idom/core/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
50 changes: 49 additions & 1 deletion tests/test_core/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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

0 comments on commit 72e6f93

Please sign in to comment.