Skip to content

Commit

Permalink
Test for scheduler determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jan 28, 2019
1 parent 3775f98 commit 5e12d31
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pytest_trio/_tests/test_hypothesis_interaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest
import trio
from hypothesis import given, settings, strategies as st

from pytest_trio.plugin import _trio_test_runner_factory

# deadline=None avoids unpredictable warnings/errors when CI happens to be
# slow (example: https://travis-ci.org/python-trio/pytest-trio/jobs/406738296)
# max_examples=5 speeds things up a bit
Expand Down Expand Up @@ -28,3 +31,47 @@ async def test_mark_outer(n):
async def test_mark_and_parametrize(x, y):
assert x is None
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 = []

@our_settings
@given(st.integers())
@pytest.mark.trio
async def inner(_):
traces.append(await scheduler_trace())

# The pytest.mark.trio doesn't do it's magic thing to
# inner functions, so we invoke it explicitly here.
inner.hypothesis.inner_test = _trio_test_runner_factory(
None, inner.hypothesis.inner_test
)
inner() # Tada, now it's a sync function!

assert len(traces) >= 5
assert len(set(traces)) == 1

0 comments on commit 5e12d31

Please sign in to comment.