Skip to content

Made InterruptTest more robust #1393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/embed_tests/TestInterrupt.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -30,34 +30,35 @@ public void Dispose()
[Test]
public void InterruptTest()
{
int runSimpleStringReturnValue = int.MinValue;
ulong pythonThreadID = ulong.MinValue;
Task.Factory.StartNew(() =>
long pythonThreadID = 0;
var asyncCall = Task.Factory.StartNew(() =>
{
using (Py.GIL())
{
pythonThreadID = PythonEngine.GetPythonThreadID();
runSimpleStringReturnValue = PythonEngine.RunSimpleString(@"
Interlocked.Exchange(ref pythonThreadID, (long)PythonEngine.GetPythonThreadID());
return PythonEngine.RunSimpleString(@"
import time

while True:
time.sleep(0.2)");
}
});

Thread.Sleep(200);

Assert.AreNotEqual(ulong.MinValue, pythonThreadID);
var timeout = Stopwatch.StartNew();
while (Interlocked.Read(ref pythonThreadID) == 0)
{
Assert.Less(timeout.Elapsed, TimeSpan.FromSeconds(5), "thread ID was not assigned in time");
}

using (Py.GIL())
{
int interruptReturnValue = PythonEngine.Interrupt(pythonThreadID);
int interruptReturnValue = PythonEngine.Interrupt((ulong)Interlocked.Read(ref pythonThreadID));
Assert.AreEqual(1, interruptReturnValue);
}

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

Assert.AreEqual(-1, runSimpleStringReturnValue);
Assert.AreEqual(-1, asyncCall.Result);
}
}
}