Skip to content

Commit 86b44d2

Browse files
committed
Final Trio api for deterministic scheduling
1 parent 32e1c6c commit 86b44d2

2 files changed

Lines changed: 8 additions & 24 deletions

File tree

pytest_trio/_tests/test_hypothesis_interaction.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import pytest
22
import trio
3+
from trio.tests.test_scheduler_determinism import (
4+
scheduler_trace, test_the_trio_scheduler_is_not_deterministic,
5+
test_the_trio_scheduler_is_deterministic_if_seeded
6+
)
37
from hypothesis import given, settings, strategies as st
48

59
from pytest_trio.plugin import _trio_test_runner_factory
@@ -33,30 +37,6 @@ async def test_mark_and_parametrize(x, y):
3337
assert y in (1, 2)
3438

3539

36-
async def scheduler_trace():
37-
"""Returns a scheduler-dependent value we can use to check determinism."""
38-
trace = []
39-
40-
async def tracer(name):
41-
for i in range(10):
42-
trace.append((name, i))
43-
await trio.sleep(0)
44-
45-
async with trio.open_nursery() as nursery:
46-
for i in range(5):
47-
nursery.start_soon(tracer, i)
48-
49-
return tuple(trace)
50-
51-
52-
def test_the_trio_scheduler_is_not_deterministic():
53-
# At least, not yet. See https://github.com/python-trio/trio/issues/32
54-
traces = []
55-
for _ in range(10):
56-
traces.append(trio.run(scheduler_trace))
57-
assert len(set(traces)) == len(traces)
58-
59-
6040
def test_the_trio_scheduler_is_deterministic_under_hypothesis():
6141
traces = []
6242

pytest_trio/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
# even though it uses a module-scoped Random instance. This works
3232
# regardless of whether or not the random_module strategy is used.
3333
register_random(trio._core._run._r)
34+
# We also have to enable determinism, which is disabled by default
35+
# due to a small performance impact - but fine to enable in testing.
36+
# See https://github.com/python-trio/trio/pull/890/ for details.
37+
trio._core._run._ALLOW_DETERMINISTIC_SCHEDULING = True
3438

3539

3640
def pytest_addoption(parser):

0 commit comments

Comments
 (0)