|
1 |
| - |
2 | 1 | using System;
|
| 2 | +using System.Diagnostics; |
3 | 3 | using System.Threading;
|
4 | 4 | using System.Threading.Tasks;
|
5 | 5 |
|
@@ -30,34 +30,35 @@ public void Dispose()
|
30 | 30 | [Test]
|
31 | 31 | public void InterruptTest()
|
32 | 32 | {
|
33 |
| - int runSimpleStringReturnValue = int.MinValue; |
34 |
| - ulong pythonThreadID = ulong.MinValue; |
35 |
| - Task.Factory.StartNew(() => |
| 33 | + long pythonThreadID = 0; |
| 34 | + var asyncCall = Task.Factory.StartNew(() => |
36 | 35 | {
|
37 | 36 | using (Py.GIL())
|
38 | 37 | {
|
39 |
| - pythonThreadID = PythonEngine.GetPythonThreadID(); |
40 |
| - runSimpleStringReturnValue = PythonEngine.RunSimpleString(@" |
| 38 | + Interlocked.Exchange(ref pythonThreadID, (long)PythonEngine.GetPythonThreadID()); |
| 39 | + return PythonEngine.RunSimpleString(@" |
41 | 40 | import time
|
42 | 41 |
|
43 | 42 | while True:
|
44 | 43 | time.sleep(0.2)");
|
45 | 44 | }
|
46 | 45 | });
|
47 | 46 |
|
48 |
| - Thread.Sleep(200); |
49 |
| - |
50 |
| - Assert.AreNotEqual(ulong.MinValue, pythonThreadID); |
| 47 | + var timeout = Stopwatch.StartNew(); |
| 48 | + while (Interlocked.Read(ref pythonThreadID) == 0) |
| 49 | + { |
| 50 | + Assert.Less(timeout.Elapsed, TimeSpan.FromSeconds(5), "thread ID was not assigned in time"); |
| 51 | + } |
51 | 52 |
|
52 | 53 | using (Py.GIL())
|
53 | 54 | {
|
54 |
| - int interruptReturnValue = PythonEngine.Interrupt(pythonThreadID); |
| 55 | + int interruptReturnValue = PythonEngine.Interrupt((ulong)Interlocked.Read(ref pythonThreadID)); |
55 | 56 | Assert.AreEqual(1, interruptReturnValue);
|
56 | 57 | }
|
57 | 58 |
|
58 |
| - Thread.Sleep(300); |
| 59 | + Assert.IsTrue(asyncCall.Wait(TimeSpan.FromSeconds(5)), "Async thread was not interrupted in time"); |
59 | 60 |
|
60 |
| - Assert.AreEqual(-1, runSimpleStringReturnValue); |
| 61 | + Assert.AreEqual(-1, asyncCall.Result); |
61 | 62 | }
|
62 | 63 | }
|
63 | 64 | }
|
0 commit comments