| 87 | |
| 88 | @pytest.mark.skipif(IS_WASM, reason="wasm doesn't support asyncio") |
| 89 | def test_asyncio_safe(self): |
| 90 | # asyncio may not always work, let's assume its fine if missing |
| 91 | # Pyodide/wasm doesn't support it. If this test makes problems, |
| 92 | # it should just be skipped liberally (or run differently). |
| 93 | asyncio = pytest.importorskip("asyncio") |
| 94 | |
| 95 | @np.errstate(invalid="ignore") |
| 96 | def decorated(): |
| 97 | # Decorated non-async function (it is not safe to decorate an |
| 98 | # async one) |
| 99 | assert np.geterr()["invalid"] == "ignore" |
| 100 | |
| 101 | async def func1(): |
| 102 | decorated() |
| 103 | await asyncio.sleep(0.1) |
| 104 | decorated() |
| 105 | |
| 106 | async def func2(): |
| 107 | with np.errstate(invalid="raise"): |
| 108 | assert np.geterr()["invalid"] == "raise" |
| 109 | await asyncio.sleep(0.125) |
| 110 | assert np.geterr()["invalid"] == "raise" |
| 111 | |
| 112 | # for good sport, a third one with yet another state: |
| 113 | async def func3(): |
| 114 | with np.errstate(invalid="print"): |
| 115 | assert np.geterr()["invalid"] == "print" |
| 116 | await asyncio.sleep(0.11) |
| 117 | assert np.geterr()["invalid"] == "print" |
| 118 | |
| 119 | async def main(): |
| 120 | # simply run all three function multiple times: |
| 121 | await asyncio.gather( |
| 122 | func1(), func2(), func3(), func1(), func2(), func3(), |
| 123 | func1(), func2(), func3(), func1(), func2(), func3()) |
| 124 | |
| 125 | loop = asyncio.new_event_loop() |
| 126 | with np.errstate(invalid="warn"): |
| 127 | asyncio.run(main()) |
| 128 | assert np.geterr()["invalid"] == "warn" |
| 129 | |
| 130 | assert np.geterr()["invalid"] == "warn" # the default |
| 131 | loop.close() |