Skip to content

Commit

Permalink
Final Trio api for deterministic scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Feb 11, 2019
1 parent 32e1c6c commit 86b44d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
28 changes: 4 additions & 24 deletions pytest_trio/_tests/test_hypothesis_interaction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import pytest
import trio
from trio.tests.test_scheduler_determinism import (
scheduler_trace, test_the_trio_scheduler_is_not_deterministic,
test_the_trio_scheduler_is_deterministic_if_seeded
)
from hypothesis import given, settings, strategies as st

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


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_under_hypothesis():
traces = []

Expand Down
4 changes: 4 additions & 0 deletions pytest_trio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
# even though it uses a module-scoped Random instance. This works
# regardless of whether or not the random_module strategy is used.
register_random(trio._core._run._r)
# We also have to enable determinism, which is disabled by default
# due to a small performance impact - but fine to enable in testing.
# See https://github.com/python-trio/trio/pull/890/ for details.
trio._core._run._ALLOW_DETERMINISTIC_SCHEDULING = True


def pytest_addoption(parser):
Expand Down

0 comments on commit 86b44d2

Please sign in to comment.