|
1 | 1 | import asyncio |
2 | 2 | from time import sleep |
3 | | -from typing import Callable |
| 3 | +from typing import Callable, List, Tuple |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
@@ -88,3 +88,99 @@ class _(App[None]): |
88 | 88 | @work(thread=False) |
89 | 89 | def foo(self) -> None: |
90 | 90 | pass |
| 91 | + |
| 92 | + |
| 93 | +class NestedWorkersApp(App[None]): |
| 94 | + def __init__(self, call_stack: List[str]): |
| 95 | + self.call_stack = call_stack |
| 96 | + super().__init__() |
| 97 | + |
| 98 | + def call_from_stack(self): |
| 99 | + if self.call_stack: |
| 100 | + call_now = self.call_stack.pop() |
| 101 | + getattr(self, call_now)() |
| 102 | + |
| 103 | + @work(thread=False) |
| 104 | + async def async_no_thread(self): |
| 105 | + self.call_from_stack() |
| 106 | + |
| 107 | + @work(thread=True) |
| 108 | + async def async_thread(self): |
| 109 | + self.call_from_stack() |
| 110 | + |
| 111 | + @work(thread=True) |
| 112 | + def thread(self): |
| 113 | + self.call_from_stack() |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.parametrize( |
| 117 | + "call_stack", |
| 118 | + [ # from itertools import product; list(product("async_no_thread async_thread thread".split(), repeat=3)) |
| 119 | + ("async_no_thread", "async_no_thread", "async_no_thread"), |
| 120 | + ("async_no_thread", "async_no_thread", "async_thread"), |
| 121 | + ("async_no_thread", "async_no_thread", "thread"), |
| 122 | + ("async_no_thread", "async_thread", "async_no_thread"), |
| 123 | + ("async_no_thread", "async_thread", "async_thread"), |
| 124 | + ("async_no_thread", "async_thread", "thread"), |
| 125 | + ("async_no_thread", "thread", "async_no_thread"), |
| 126 | + ("async_no_thread", "thread", "async_thread"), |
| 127 | + ("async_no_thread", "thread", "thread"), |
| 128 | + ("async_thread", "async_no_thread", "async_no_thread"), |
| 129 | + ("async_thread", "async_no_thread", "async_thread"), |
| 130 | + ("async_thread", "async_no_thread", "thread"), |
| 131 | + ("async_thread", "async_thread", "async_no_thread"), |
| 132 | + ("async_thread", "async_thread", "async_thread"), |
| 133 | + ("async_thread", "async_thread", "thread"), |
| 134 | + ("async_thread", "thread", "async_no_thread"), |
| 135 | + ("async_thread", "thread", "async_thread"), |
| 136 | + ("async_thread", "thread", "thread"), |
| 137 | + ("thread", "async_no_thread", "async_no_thread"), |
| 138 | + ("thread", "async_no_thread", "async_thread"), |
| 139 | + ("thread", "async_no_thread", "thread"), |
| 140 | + ("thread", "async_thread", "async_no_thread"), |
| 141 | + ("thread", "async_thread", "async_thread"), |
| 142 | + ("thread", "async_thread", "thread"), |
| 143 | + ("thread", "thread", "async_no_thread"), |
| 144 | + ("thread", "thread", "async_thread"), |
| 145 | + ("thread", "thread", "thread"), |
| 146 | + ( # Plus a longer chain to stress test this mechanism. |
| 147 | + "async_no_thread", |
| 148 | + "async_no_thread", |
| 149 | + "thread", |
| 150 | + "thread", |
| 151 | + "async_thread", |
| 152 | + "async_thread", |
| 153 | + "async_no_thread", |
| 154 | + "async_thread", |
| 155 | + "async_no_thread", |
| 156 | + "async_thread", |
| 157 | + "thread", |
| 158 | + "async_thread", |
| 159 | + "async_thread", |
| 160 | + "async_no_thread", |
| 161 | + "async_no_thread", |
| 162 | + "thread", |
| 163 | + "thread", |
| 164 | + "async_no_thread", |
| 165 | + "async_no_thread", |
| 166 | + "thread", |
| 167 | + "async_no_thread", |
| 168 | + "thread", |
| 169 | + "thread", |
| 170 | + ), |
| 171 | + ], |
| 172 | +) |
| 173 | +async def test_calling_workers_from_within_workers(call_stack: Tuple[str]): |
| 174 | + """Regression test for https://github.com/Textualize/textual/issues/3472. |
| 175 | +
|
| 176 | + This makes sure we can nest worker calls without a problem. |
| 177 | + """ |
| 178 | + app = NestedWorkersApp(list(call_stack)) |
| 179 | + async with app.run_test(): |
| 180 | + app.call_from_stack() |
| 181 | + # We need multiple awaits because we're creating a chain of workers that may |
| 182 | + # have multiple async workers, each of which may need the await to have enough |
| 183 | + # time to call the next one in the chain. |
| 184 | + for _ in range(len(call_stack)): |
| 185 | + await app.workers.wait_for_complete() |
| 186 | + assert app.call_stack == [] |
0 commit comments