-
-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #890 from Zac-HD/optionally-deterministic-scheduler
Support deterministic scheduling
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Added an internal mechanism for pytest-trio's | ||
`Hypothesis <https://hypothesis.readthedocs.io>`__ integration | ||
to make the task scheduler reproducible and avoid flaky tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import trio | ||
|
||
|
||
async def scheduler_trace(): | ||
"""Returns a scheduler-dependent value we can use to check determinism.""" | ||
trace = [] | ||
|
||
async def tracer(name): | ||
for i in range(10): | ||
trace.append((name, i)) | ||
await trio.sleep(0) | ||
|
||
async with trio.open_nursery() as nursery: | ||
for i in range(5): | ||
nursery.start_soon(tracer, i) | ||
|
||
return tuple(trace) | ||
|
||
|
||
def test_the_trio_scheduler_is_not_deterministic(): | ||
# At least, not yet. See https://github.com/python-trio/trio/issues/32 | ||
traces = [] | ||
for _ in range(10): | ||
traces.append(trio.run(scheduler_trace)) | ||
assert len(set(traces)) == len(traces) | ||
|
||
|
||
def test_the_trio_scheduler_is_deterministic_if_seeded(monkeypatch): | ||
monkeypatch.setattr( | ||
trio._core._run, "_ALLOW_DETERMINISTIC_SCHEDULING", True | ||
) | ||
traces = [] | ||
for _ in range(10): | ||
state = trio._core._run._r.getstate() | ||
try: | ||
trio._core._run._r.seed(0) | ||
traces.append(trio.run(scheduler_trace)) | ||
finally: | ||
trio._core._run._r.setstate(state) | ||
|
||
assert len(traces) == 10 | ||
assert len(set(traces)) == 1 |