Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit831fc85

Browse files
committed
made InterruptTest more robust
should resolve this failure:https://github.com/pythonnet/pythonnet/pull/1392/checks?check_run_id=1944113649related to#1337
1 parent707ef36 commit831fc85

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
usingSystem;
3+
usingSystem.Diagnostics;
34
usingSystem.Threading;
45
usingSystem.Threading.Tasks;
56

@@ -30,34 +31,35 @@ public void Dispose()
3031
[Test]
3132
publicvoidInterruptTest()
3233
{
33-
intrunSimpleStringReturnValue=int.MinValue;
34-
ulongpythonThreadID=ulong.MinValue;
35-
Task.Factory.StartNew(()=>
34+
longpythonThreadID=0;
35+
varasyncCall=Task.Factory.StartNew(()=>
3636
{
3737
using(Py.GIL())
3838
{
39-
pythonThreadID=PythonEngine.GetPythonThreadID();
40-
runSimpleStringReturnValue=PythonEngine.RunSimpleString(@"
39+
Interlocked.Exchange(refpythonThreadID,(long)PythonEngine.GetPythonThreadID());
40+
returnPythonEngine.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+
vartimeout=Stopwatch.StartNew();
49+
while(Interlocked.Read(refpythonThreadID)==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-
intinterruptReturnValue=PythonEngine.Interrupt(pythonThreadID);
56+
intinterruptReturnValue=PythonEngine.Interrupt((ulong)Interlocked.Read(refpythonThreadID));
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)

[8]ページ先頭

©2009-2025 Movatter.jp