CancellationTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using Lua.Standard;
  2. using System.Diagnostics;
  3. namespace Lua.Tests;
  4. public class CancellationTest
  5. {
  6. LuaState state = default!;
  7. [SetUp]
  8. public void SetUp()
  9. {
  10. state = LuaState.Create();
  11. state.OpenStandardLibraries();
  12. state.Environment["assert"] = new LuaFunction("assert_with_wait",
  13. async (context, ct) =>
  14. {
  15. await Task.Delay(1, ct);
  16. var arg0 = context.GetArgument(0);
  17. if (!arg0.ToBoolean())
  18. {
  19. var message = "assertion failed!";
  20. if (context.HasArgument(1))
  21. {
  22. message = context.GetArgument<string>(1);
  23. }
  24. throw new LuaAssertionException(context.Thread, message);
  25. }
  26. return (context.Return(context.Arguments));
  27. });
  28. state.Environment["sleep"] = new LuaFunction("sleep",
  29. (context, _) =>
  30. {
  31. Thread.Sleep(context.GetArgument<int>(0));
  32. return new(context.Return());
  33. });
  34. state.Environment["wait"] = new LuaFunction("wait",
  35. async (context, ct) =>
  36. {
  37. await Task.Delay(context.GetArgument<int>(0), ct);
  38. return context.Return();
  39. });
  40. }
  41. [Test]
  42. public async Task PCall_WaitTest()
  43. {
  44. var source = """
  45. local function f(millisec)
  46. wait(millisec)
  47. end
  48. pcall(f, 500)
  49. """;
  50. var cancellationTokenSource = new CancellationTokenSource();
  51. cancellationTokenSource.CancelAfter(200);
  52. try
  53. {
  54. await state.DoStringAsync(source, "@test.lua", cancellationTokenSource.Token);
  55. Assert.Fail("Expected TaskCanceledException was not thrown.");
  56. }
  57. catch (Exception e)
  58. {
  59. Assert.That(e, Is.TypeOf<LuaCanceledException>());
  60. var luaCancelledException = (LuaCanceledException)e;
  61. Assert.That(luaCancelledException.InnerException, Is.TypeOf<TaskCanceledException>());
  62. var luaStackTrace = luaCancelledException.LuaTraceback!.ToString();
  63. Console.WriteLine(luaStackTrace);
  64. Assert.That(luaStackTrace, Contains.Substring("'wait'"));
  65. Assert.That(luaStackTrace, Contains.Substring("'pcall'"));
  66. }
  67. }
  68. [Test]
  69. public async Task PCall_SleepTest()
  70. {
  71. var source = """
  72. local function f(millisec)
  73. sleep(millisec)
  74. end
  75. pcall(f, 500)
  76. """;
  77. var cancellationTokenSource = new CancellationTokenSource();
  78. cancellationTokenSource.CancelAfter(250);
  79. try
  80. {
  81. await state.DoStringAsync(source, "@test.lua", cancellationTokenSource.Token);
  82. Assert.Fail("Expected TaskCanceledException was not thrown.");
  83. }
  84. catch (Exception e)
  85. {
  86. Assert.That(e, Is.TypeOf<LuaCanceledException>());
  87. var luaCancelledException = (LuaCanceledException)e;
  88. Assert.That(luaCancelledException.InnerException, Is.Null);
  89. var luaStackTrace = luaCancelledException.LuaTraceback!.ToString();
  90. Console.WriteLine(luaStackTrace);
  91. Assert.That(luaStackTrace, Contains.Substring("'sleep'"));
  92. Assert.That(luaStackTrace, Contains.Substring("'pcall'"));
  93. }
  94. }
  95. [Test]
  96. public async Task ForLoopTest()
  97. {
  98. var source = """
  99. local ret = 0
  100. for i = 1, 1000000000 do
  101. ret = ret + i
  102. end
  103. return ret
  104. """;
  105. var cancellationTokenSource = new CancellationTokenSource();
  106. cancellationTokenSource.CancelAfter(100);
  107. cancellationTokenSource.Token.Register(() =>
  108. {
  109. Console.WriteLine("Cancellation requested");
  110. });
  111. try
  112. {
  113. var r = await state.DoStringAsync(source, "@test.lua", cancellationTokenSource.Token);
  114. Console.WriteLine(r[0]);
  115. Assert.Fail("Expected TaskCanceledException was not thrown.");
  116. }
  117. catch (Exception e)
  118. {
  119. Assert.That(e, Is.TypeOf<LuaCanceledException>());
  120. Console.WriteLine(e.StackTrace);
  121. var luaCancelledException = (LuaCanceledException)e;
  122. Assert.That(luaCancelledException.InnerException, Is.Null);
  123. var traceback = luaCancelledException.LuaTraceback;
  124. if (traceback != null)
  125. {
  126. var luaStackTrace = traceback.ToString();
  127. Console.WriteLine(luaStackTrace);
  128. }
  129. }
  130. }
  131. [Test]
  132. public async Task GoToLoopTest()
  133. {
  134. var source = """
  135. local ret = 0
  136. ::loop::
  137. ret = ret + 1
  138. goto loop
  139. return ret
  140. """;
  141. var cancellationTokenSource = new CancellationTokenSource();
  142. cancellationTokenSource.CancelAfter(100);
  143. cancellationTokenSource.Token.Register(() =>
  144. {
  145. Console.WriteLine("Cancellation requested");
  146. });
  147. try
  148. {
  149. var r = await state.DoStringAsync(source, "@test.lua", cancellationTokenSource.Token);
  150. Console.WriteLine(r[0]);
  151. Assert.Fail("Expected TaskCanceledException was not thrown.");
  152. }
  153. catch (Exception e)
  154. {
  155. Assert.That(e, Is.TypeOf<LuaCanceledException>());
  156. Console.WriteLine(e.StackTrace);
  157. var luaCancelledException = (LuaCanceledException)e;
  158. Assert.That(luaCancelledException.InnerException, Is.Null);
  159. var traceback = luaCancelledException.LuaTraceback;
  160. if (traceback != null)
  161. {
  162. var luaStackTrace = traceback.ToString();
  163. Console.WriteLine(luaStackTrace);
  164. }
  165. }
  166. }
  167. [Test]
  168. public async Task CancelByHookTest()
  169. {
  170. var source = """
  171. local ret = 0
  172. ::loop::
  173. ret = ret + 1
  174. goto loop
  175. return ret
  176. """;
  177. var cancellationTokenSource = new CancellationTokenSource();
  178. var sw = Stopwatch.StartNew();
  179. state.MainThread.SetHook(new LuaFunction("timeout",async (context, cancellationToken) =>
  180. {
  181. if (sw.ElapsedMilliseconds > 100)
  182. {
  183. await Task.Delay(1,cancellationToken);
  184. cancellationTokenSource.Cancel();
  185. cancellationToken.ThrowIfCancellationRequested();
  186. }
  187. return context.Return();
  188. }),"",10000);
  189. cancellationTokenSource.Token.Register(() =>
  190. {
  191. Console.WriteLine("Cancellation requested");
  192. });
  193. try
  194. {
  195. var r = await state.DoStringAsync(source, "@test.lua", cancellationTokenSource.Token);
  196. Console.WriteLine(r[0]);
  197. Assert.Fail("Expected TaskCanceledException was not thrown.");
  198. }
  199. catch (Exception e)
  200. {
  201. Assert.That(e, Is.TypeOf<LuaCanceledException>());
  202. Console.WriteLine(e.StackTrace);
  203. var luaCancelledException = (LuaCanceledException)e;
  204. Assert.That(luaCancelledException.InnerException, Is.TypeOf<OperationCanceledException>());
  205. var traceback = luaCancelledException.LuaTraceback;
  206. if (traceback != null)
  207. {
  208. var luaStackTrace = traceback.ToString();
  209. Console.WriteLine(luaStackTrace);
  210. }
  211. }
  212. }
  213. }