| 164 | |
| 165 | @pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") |
| 166 | def test_async_callbacks(): |
| 167 | # serves as state for async callback |
| 168 | class Item: |
| 169 | def __init__(self, value): |
| 170 | self.value = value |
| 171 | |
| 172 | res = [] |
| 173 | |
| 174 | # generate stateful lambda that will store result in `res` |
| 175 | def gen_f(): |
| 176 | s = Item(3) |
| 177 | return lambda j: res.append(s.value + j) |
| 178 | |
| 179 | # do some work async |
| 180 | work = [1, 2, 3, 4] |
| 181 | m.test_async_callback(gen_f(), work) |
| 182 | # Wait for all detached worker threads to finish. |
| 183 | deadline = time.monotonic() + 5.0 |
| 184 | while len(res) < len(work) and time.monotonic() < deadline: |
| 185 | time.sleep(0.01) |
| 186 | assert len(res) == len(work), f"Timed out waiting for callbacks: res={res!r}" |
| 187 | assert sum(res) == sum(x + 3 for x in work) |
| 188 | |
| 189 | |
| 190 | @pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") |