Skip to content

Commit 854f20b

Browse files
committed
internally time limit TestThread
1 parent ab5b896 commit 854f20b

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/embed_tests/Modules.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -339,26 +339,35 @@ public void TestThread()
339339
);
340340
}
341341
int th_cnt = 100;
342+
int started = 0, locked = 0, unlocked = 0;
342343
for (int i = 0; i < th_cnt; i++)
343344
{
344345
ThreadPool.QueueUserWorkItem(_ =>
345346
{
347+
Interlocked.Increment(ref started);
346348
using (Py.GIL())
347349
{
350+
Interlocked.Decrement(ref locked);
348351
_ps.update();
349352
}
353+
Interlocked.Increment(ref unlocked);
350354
});
351355
}
352356
//equivalent to Thread.Join, make the main thread join the GIL competition
353357
int cnt = 0;
354-
while (cnt != th_cnt)
358+
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
359+
while (cnt != th_cnt && stopwatch.ElapsedMilliseconds < 15000)
355360
{
356361
using (Py.GIL())
357362
{
358363
cnt = ps.Get<int>("th_cnt");
359364
}
360365
Thread.Yield();
361366
}
367+
368+
if (stopwatch.ElapsedMilliseconds > 15000)
369+
Assert.Fail($"started: {started} locked: {locked} unlocked: {unlocked} cnt: {cnt}");
370+
362371
using (Py.GIL())
363372
{
364373
var result = ps.Get<int>("res");

0 commit comments

Comments
 (0)