Skip to content

Commit

Permalink
Rework test_run_local_simultaneous_runs to be more reliable
Browse files Browse the repository at this point in the history
We used to use a single queue to send messages to and from the
threads, which of course is unreliable because the main thread could
end up reading back its own message. In particular, on PyPy this
happened regularly, and occasionally it meant that the test
deadlocked. So this fixed python-triogh-379.

This patch also updates the test harness to actually pull errors back
from the child threads, so that if the test does fail then we can
detect it.
  • Loading branch information
njsmith committed Feb 15, 2018
1 parent a72e471 commit 8c7ccaf
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions trio/_core/tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,43 @@ async def main(x):
def test_run_local_simultaneous_runs():
r = _core.RunLocal()

async def main(x, q):
q.get()
result_q = queue.Queue()

async def main(x, in_q, out_q):
in_q.get()
assert not hasattr(r, "attr")
r.attr = x
assert hasattr(r, "attr")
assert r.attr == x
q.put(None)
q.get()
out_q.put(None)
in_q.get()
assert r.attr == x

q1 = queue.Queue()
t1 = threading.Thread(target=_core.run, args=(main, 1, q1))
def harness(x, in_q, out_q):
result_q.put(_core.Result.capture(_core.run, main, x, in_q, out_q))

in_q1 = queue.Queue()
out_q1 = queue.Queue()
t1 = threading.Thread(target=harness, args=(1, in_q1, out_q1))
t1.start()

q2 = queue.Queue()
t2 = threading.Thread(target=_core.run, args=(main, 2, q2))
in_q2 = queue.Queue()
out_q2 = queue.Queue()
t2 = threading.Thread(target=harness, args=(2, in_q2, out_q2))
t2.start()

q1.put(None)
q1.get()
in_q1.put(None)
out_q1.get()

q2.put(None)
q2.get()
in_q2.put(None)
out_q2.get()

q1.put(None)
q2.put(None)
in_q1.put(None)
in_q2.put(None)
t1.join()
t2.join()
result_q.get().unwrap()
result_q.get().unwrap()

with pytest.raises(RuntimeError):
r.attr
Expand Down

0 comments on commit 8c7ccaf

Please sign in to comment.