Skip to content

Commit 831fc85

Browse files
committed
made InterruptTest more robust
should resolve this failure: https://github.com/pythonnet/pythonnet/pull/1392/checks?check_run_id=1944113649 related to #1337
1 parent 707ef36 commit 831fc85

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/embed_tests/TestInterrupt.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using System;
3+
using System.Diagnostics;
34
using System.Threading;
45
using System.Threading.Tasks;
56

@@ -30,34 +31,35 @@ public void Dispose()
3031
[Test]
3132
public void InterruptTest()
3233
{
33-
int runSimpleStringReturnValue = int.MinValue;
34-
ulong pythonThreadID = ulong.MinValue;
35-
Task.Factory.StartNew(() =>
34+
long pythonThreadID = 0;
35+
var asyncCall = Task.Factory.StartNew(() =>
3636
{
3737
using (Py.GIL())
3838
{
39-
pythonThreadID = PythonEngine.GetPythonThreadID();
40-
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
39+
Interlocked.Exchange(ref pythonThreadID, (long)PythonEngine.GetPythonThreadID());
40+
return PythonEngine.RunSimpleString(@"
4141
import time
4242
4343
while True:
4444
time.sleep(0.2)");
4545
}
4646
});
4747

48-
Thread.Sleep(200);
49-
50-
Assert.AreNotEqual(ulong.MinValue, pythonThreadID);
48+
var timeout = Stopwatch.StartNew();
49+
while (Interlocked.Read(ref pythonThreadID) == 0)
50+
{
51+
Assert.Less(timeout.Elapsed, TimeSpan.FromSeconds(5), "thread ID was not assigned in time");
52+
}
5153

5254
using (Py.GIL())
5355
{
54-
int interruptReturnValue = PythonEngine.Interrupt(pythonThreadID);
56+
int interruptReturnValue = PythonEngine.Interrupt((ulong)Interlocked.Read(ref pythonThreadID));
5557
Assert.AreEqual(1, interruptReturnValue);
5658
}
5759

58-
Thread.Sleep(300);
60+
Assert.IsTrue(asyncCall.Wait(TimeSpan.FromSeconds(5)), "Async thread was not interrupted in time");
5961

60-
Assert.AreEqual(-1, runSimpleStringReturnValue);
62+
Assert.AreEqual(-1, asyncCall.Result);
6163
}
6264
}
6365
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy