Skip to content

Commit 72e6f93

Browse files
committed
test dispatcher start and stop
1 parent debca80 commit 72e6f93

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

idom/client/app/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

idom/core/dispatcher.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def __init__(self, layout: Layout) -> None:
2626
super().__init__()
2727
self._layout = layout
2828

29+
async def start(self) -> None:
30+
await self.__aenter__()
31+
32+
async def stop(self) -> None:
33+
await self.task_group.cancel_scope.cancel()
34+
await self.__aexit__(None, None, None)
35+
2936
@async_resource
3037
async def layout(self) -> AsyncIterator[Layout]:
3138
async with self._layout as layout:
@@ -99,13 +106,6 @@ def __init__(self, layout: Layout) -> None:
99106
self._model_state: Any = {}
100107
self._update_queues: Dict[str, asyncio.Queue[LayoutUpdate]] = {}
101108

102-
async def start(self) -> None:
103-
await self.__aenter__()
104-
105-
async def stop(self):
106-
self.task_group.cancel_scope.cancel()
107-
await self.__aexit__(None, None, None)
108-
109109
@async_resource
110110
async def task_group(self) -> AsyncIterator[TaskGroup]:
111111
async with create_task_group() as group:

tests/test_core/test_dispatcher.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
import idom
88
from idom.core.layout import Layout, LayoutEvent
9-
from idom.core.dispatcher import SharedViewDispatcher, AbstractDispatcher
9+
from idom.core.dispatcher import (
10+
SharedViewDispatcher,
11+
AbstractDispatcher,
12+
SingleViewDispatcher,
13+
)
1014

1115

1216
async def test_shared_state_dispatcher():
@@ -120,3 +124,47 @@ async def recv():
120124
with pytest.raises(ExceptionGroup, match="this is a bug"):
121125
async with DispatcherWithBug(idom.Layout(AnyElement())) as dispatcher:
122126
await dispatcher.run(send, recv, None)
127+
128+
129+
async def test_dispatcher_start_stop():
130+
cancelled_recv = False
131+
cancelled_send = False
132+
133+
async def send(patch):
134+
nonlocal cancelled_send
135+
try:
136+
await asyncio.sleep(100)
137+
except asyncio.CancelledError:
138+
cancelled_send = True
139+
raise
140+
else:
141+
assert False, "this should never be reached"
142+
143+
async def recv():
144+
nonlocal cancelled_recv
145+
try:
146+
await asyncio.sleep(100)
147+
except asyncio.CancelledError:
148+
cancelled_recv = True
149+
raise
150+
else:
151+
assert False, "this should never be reached"
152+
153+
@idom.element
154+
def AnElement():
155+
return idom.html.div()
156+
157+
dispatcher = SingleViewDispatcher(Layout(AnElement()))
158+
159+
await dispatcher.start()
160+
161+
await dispatcher.run(send, recv, None)
162+
163+
# let it run until it hits the sleeping recv/send calls
164+
for i in range(10):
165+
await asyncio.sleep(0)
166+
167+
await dispatcher.stop()
168+
169+
assert cancelled_recv
170+
assert cancelled_send

0 commit comments

Comments
 (0)