From 8c7ccafb4057a1c7134f78d43f3cc41601615b0c Mon Sep 17 00:00:00 2001 From: "Nathaniel J. Smith" Date: Thu, 15 Feb 2018 04:13:49 -0800 Subject: [PATCH] Rework test_run_local_simultaneous_runs to be more reliable 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 gh-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. --- trio/_core/tests/test_local.py | 37 +++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/trio/_core/tests/test_local.py b/trio/_core/tests/test_local.py index 4dba087e92..9f4a356e45 100644 --- a/trio/_core/tests/test_local.py +++ b/trio/_core/tests/test_local.py @@ -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