|
6 | 6 |
|
7 | 7 | import idom |
8 | 8 | 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 | +) |
10 | 14 |
|
11 | 15 |
|
12 | 16 | async def test_shared_state_dispatcher(): |
@@ -120,3 +124,47 @@ async def recv(): |
120 | 124 | with pytest.raises(ExceptionGroup, match="this is a bug"): |
121 | 125 | async with DispatcherWithBug(idom.Layout(AnyElement())) as dispatcher: |
122 | 126 | 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