From 28bc23cd49a6235a6f84f6b6e08914fbe2b2d03b Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 27 Jun 2026 09:33:33 +0900 Subject: [PATCH] =?UTF-8?q?JIT=E7=84=A1=E9=99=90=E3=83=AB=E3=83=BC?= =?UTF-8?q?=E3=83=97=E3=81=AE=E3=83=8F=E3=83=BC=E3=83=89=E3=82=AD=E3=83=AB?= =?UTF-8?q?=E3=83=BB=E3=82=BF=E3=82=A4=E3=83=A0=E3=82=A2=E3=82=A6=E3=83=88?= =?UTF-8?q?=E6=A4=9C=E8=A8=BC=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/NativeWorkerTimeoutTests.cs | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 LuaScript.Tests/NativeWorkerTimeoutTests.cs diff --git a/LuaScript.Tests/NativeWorkerTimeoutTests.cs b/LuaScript.Tests/NativeWorkerTimeoutTests.cs new file mode 100644 index 0000000..00d21a7 --- /dev/null +++ b/LuaScript.Tests/NativeWorkerTimeoutTests.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics; +using System.IO; + +namespace LuaScript.Tests +{ + public class NativeWorkerTimeoutTests + { + private static string LuaJit => Path.Combine(AppContext.BaseDirectory, "native", "luajit.exe"); + + private static Process Launch(string inlineScript) + { + var psi = new ProcessStartInfo + { + FileName = LuaJit, + WorkingDirectory = Path.GetDirectoryName(LuaJit)!, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + }; + psi.ArgumentList.Add("-e"); + psi.ArgumentList.Add(inlineScript); + return Process.Start(psi) ?? throw new InvalidOperationException("failed to start luajit"); + } + + [Fact] + public void JitCompiledInfiniteLoop_IsHardKilledWithinTimeout_AndHostRecovers() + { + Assert.True(File.Exists(LuaJit), "native/luajit.exe must be present"); + + const int timeoutMs = 1500; + + using (var hung = Launch("local x=0 for i=1,1e9 do for j=1,1e9 do x=x+1 end end")) + { + bool exitedOnItsOwn = hung.WaitForExit(timeoutMs); + Assert.False(exitedOnItsOwn, "the hot loop should still be running at the timeout"); + + var sw = Stopwatch.StartNew(); + hung.Kill(entireProcessTree: true); + Assert.True(hung.WaitForExit(2000), "the process must terminate promptly after Kill"); + sw.Stop(); + Assert.True(sw.ElapsedMilliseconds < 2000, "kill should take effect quickly"); + } + + using var ok = Launch("io.write('recovered')"); + string output = ok.StandardOutput.ReadToEnd(); + Assert.True(ok.WaitForExit(5000), "a fresh worker must run after the previous was killed"); + Assert.Equal(0, ok.ExitCode); + Assert.Equal("recovered", output); + } + } +}