2
0

NestedRunTimeoutTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using Xunit.Abstractions;
  2. namespace ApplicationTests.Timeout;
  3. /// <summary>
  4. /// Tests for timeout behavior with nested Application.Run() calls.
  5. /// These tests verify that timeouts scheduled in a parent run loop continue to fire
  6. /// correctly when a nested modal dialog is shown via Application.Run().
  7. /// </summary>
  8. public class NestedRunTimeoutTests (ITestOutputHelper output)
  9. {
  10. [Fact]
  11. public void Multiple_Timeouts_Fire_In_Correct_Order_With_Nested_Run ()
  12. {
  13. // Arrange
  14. using IApplication? app = Application.Create ();
  15. app.Init ("FakeDriver");
  16. List<string> executionOrder = new ();
  17. var mainWindow = new Window { Title = "Main Window" };
  18. var dialog = new Dialog { Title = "Nested Dialog", Buttons = [new() { Text = "Ok" }] };
  19. var nestedRunCompleted = false;
  20. // Use iteration counter for safety instead of time-based timeout
  21. var iterations = 0;
  22. app.Iteration += IterationHandler;
  23. try
  24. {
  25. // Schedule multiple timeouts
  26. app.AddTimeout (
  27. TimeSpan.FromMilliseconds (100),
  28. () =>
  29. {
  30. executionOrder.Add ("Timeout1-100ms");
  31. output.WriteLine ("Timeout1 fired at 100ms");
  32. return false;
  33. }
  34. );
  35. app.AddTimeout (
  36. TimeSpan.FromMilliseconds (200),
  37. () =>
  38. {
  39. executionOrder.Add ("Timeout2-200ms-StartNestedRun");
  40. output.WriteLine ("Timeout2 fired at 200ms - Starting nested run");
  41. // Start nested run
  42. app.Run (dialog);
  43. executionOrder.Add ("Timeout2-NestedRunEnded");
  44. nestedRunCompleted = true;
  45. output.WriteLine ("Nested run ended");
  46. return false;
  47. }
  48. );
  49. app.AddTimeout (
  50. TimeSpan.FromMilliseconds (300),
  51. () =>
  52. {
  53. executionOrder.Add ("Timeout3-300ms-InNestedRun");
  54. output.WriteLine ($"Timeout3 fired at 300ms - TopRunnable: {app.TopRunnableView?.Title}");
  55. // This should fire while dialog is running
  56. Assert.Equal (dialog, app.TopRunnableView);
  57. return false;
  58. }
  59. );
  60. app.AddTimeout (
  61. TimeSpan.FromMilliseconds (400),
  62. () =>
  63. {
  64. executionOrder.Add ("Timeout4-400ms-CloseDialog");
  65. output.WriteLine ("Timeout4 fired at 400ms - Closing dialog");
  66. // Close the dialog
  67. app.RequestStop (dialog);
  68. return false;
  69. }
  70. );
  71. // Event-driven: Only stop main window AFTER nested run completes
  72. // Use a repeating timeout that checks the condition
  73. app.AddTimeout (
  74. TimeSpan.FromMilliseconds (50),
  75. () =>
  76. {
  77. // Keep checking until nested run completes
  78. if (nestedRunCompleted)
  79. {
  80. executionOrder.Add ("Timeout5-AfterNestedRun-StopMain");
  81. output.WriteLine ("Timeout5 fired after nested run completed - Stopping main window");
  82. app.RequestStop (mainWindow);
  83. return false; // Don't repeat
  84. }
  85. return true; // Keep checking
  86. }
  87. );
  88. // Act
  89. app.Run (mainWindow);
  90. // Assert - Verify all timeouts fired in the correct order
  91. output.WriteLine ($"Execution order: {string.Join (", ", executionOrder)}");
  92. Assert.Equal (6, executionOrder.Count); // 5 timeout events + 1 nested run end marker
  93. Assert.Equal ("Timeout1-100ms", executionOrder [0]);
  94. Assert.Equal ("Timeout2-200ms-StartNestedRun", executionOrder [1]);
  95. Assert.Equal ("Timeout3-300ms-InNestedRun", executionOrder [2]);
  96. Assert.Equal ("Timeout4-400ms-CloseDialog", executionOrder [3]);
  97. Assert.Equal ("Timeout2-NestedRunEnded", executionOrder [4]);
  98. Assert.Equal ("Timeout5-AfterNestedRun-StopMain", executionOrder [5]);
  99. }
  100. finally
  101. {
  102. app.Iteration -= IterationHandler;
  103. dialog.Dispose ();
  104. mainWindow.Dispose ();
  105. }
  106. return;
  107. void IterationHandler (object? s, EventArgs<IApplication?> e)
  108. {
  109. iterations++;
  110. // Safety limit - should never be hit with event-driven logic
  111. if (iterations > 2000)
  112. {
  113. output.WriteLine ($"SAFETY: Hit iteration limit. Execution order: {string.Join (", ", executionOrder)}");
  114. app.RequestStop ();
  115. }
  116. }
  117. }
  118. [Fact]
  119. public void Timeout_Fires_In_Nested_Run ()
  120. {
  121. // Arrange
  122. using IApplication? app = Application.Create ();
  123. app.Init ("FakeDriver");
  124. var timeoutFired = false;
  125. var nestedRunStarted = false;
  126. var nestedRunEnded = false;
  127. // Create a simple window for the main run loop
  128. var mainWindow = new Window { Title = "Main Window" };
  129. // Create a dialog for the nested run loop
  130. var dialog = new Dialog { Title = "Nested Dialog", Buttons = [new() { Text = "Ok" }] };
  131. // Schedule a safety timeout that will ensure the app quits if test hangs
  132. var requestStopTimeoutFired = false;
  133. app.AddTimeout (
  134. TimeSpan.FromMilliseconds (5000),
  135. () =>
  136. {
  137. output.WriteLine ("SAFETY: RequestStop Timeout fired - test took too long!");
  138. requestStopTimeoutFired = true;
  139. app.RequestStop ();
  140. return false;
  141. }
  142. );
  143. // Schedule a timeout that will fire AFTER the nested run starts and stop the dialog
  144. app.AddTimeout (
  145. TimeSpan.FromMilliseconds (200),
  146. () =>
  147. {
  148. output.WriteLine ($"DialogRequestStop Timeout fired! TopRunnable: {app.TopRunnableView?.Title ?? "null"}");
  149. timeoutFired = true;
  150. // Close the dialog when timeout fires
  151. if (app.TopRunnableView == dialog)
  152. {
  153. app.RequestStop (dialog);
  154. }
  155. return false;
  156. }
  157. );
  158. // After 100ms, start the nested run loop
  159. app.AddTimeout (
  160. TimeSpan.FromMilliseconds (100),
  161. () =>
  162. {
  163. output.WriteLine ("Starting nested run...");
  164. nestedRunStarted = true;
  165. // This blocks until the dialog is closed (by the timeout at 200ms)
  166. app.Run (dialog);
  167. output.WriteLine ("Nested run ended");
  168. nestedRunEnded = true;
  169. // Stop the main window after nested run completes
  170. app.RequestStop ();
  171. return false;
  172. }
  173. );
  174. // Act - Start the main run loop
  175. app.Run (mainWindow);
  176. // Assert
  177. Assert.True (nestedRunStarted, "Nested run should have started");
  178. Assert.True (timeoutFired, "Timeout should have fired during nested run");
  179. Assert.True (nestedRunEnded, "Nested run should have ended");
  180. Assert.False (requestStopTimeoutFired, "Safety timeout should NOT have fired");
  181. dialog.Dispose ();
  182. mainWindow.Dispose ();
  183. }
  184. [Fact]
  185. public void Timeout_Fires_With_Single_Session ()
  186. {
  187. // Arrange
  188. using IApplication? app = Application.Create ();
  189. app.Init ("FakeDriver");
  190. // Create a simple window for the main run loop
  191. var mainWindow = new Window { Title = "Main Window" };
  192. // Schedule a timeout that will ensure the app quits
  193. var requestStopTimeoutFired = false;
  194. app.AddTimeout (
  195. TimeSpan.FromMilliseconds (100),
  196. () =>
  197. {
  198. output.WriteLine ("RequestStop Timeout fired!");
  199. requestStopTimeoutFired = true;
  200. app.RequestStop ();
  201. return false;
  202. }
  203. );
  204. // Act - Start the main run loop
  205. app.Run (mainWindow);
  206. // Assert
  207. Assert.True (requestStopTimeoutFired, "RequestStop Timeout should have fired");
  208. mainWindow.Dispose ();
  209. }
  210. [Fact]
  211. public void Timeout_Queue_Persists_Across_Nested_Runs ()
  212. {
  213. // Verify that the timeout queue is not cleared when nested runs start/end
  214. // Arrange
  215. using IApplication? app = Application.Create ();
  216. app.Init ("FakeDriver");
  217. // Schedule a safety timeout that will ensure the app quits if test hangs
  218. var requestStopTimeoutFired = false;
  219. app.AddTimeout (
  220. TimeSpan.FromMilliseconds (10000),
  221. () =>
  222. {
  223. output.WriteLine ("SAFETY: RequestStop Timeout fired - test took too long!");
  224. requestStopTimeoutFired = true;
  225. app.RequestStop ();
  226. return false;
  227. }
  228. );
  229. var mainWindow = new Window { Title = "Main Window" };
  230. var dialog = new Dialog { Title = "Dialog", Buttons = [new() { Text = "Ok" }] };
  231. var initialTimeoutCount = 0;
  232. var timeoutCountDuringNestedRun = 0;
  233. var timeoutCountAfterNestedRun = 0;
  234. // Schedule 5 timeouts at different times with wider spacing
  235. for (var i = 0; i < 5; i++)
  236. {
  237. int capturedI = i;
  238. app.AddTimeout (
  239. TimeSpan.FromMilliseconds (150 * (i + 1)), // Increased spacing from 100ms to 150ms
  240. () =>
  241. {
  242. output.WriteLine ($"Timeout {capturedI} fired at {150 * (capturedI + 1)}ms");
  243. if (capturedI == 0)
  244. {
  245. initialTimeoutCount = app.TimedEvents!.Timeouts.Count;
  246. output.WriteLine ($"Initial timeout count: {initialTimeoutCount}");
  247. }
  248. if (capturedI == 1)
  249. {
  250. // Start nested run
  251. output.WriteLine ("Starting nested run");
  252. app.Run (dialog);
  253. output.WriteLine ("Nested run ended");
  254. timeoutCountAfterNestedRun = app.TimedEvents!.Timeouts.Count;
  255. output.WriteLine ($"Timeout count after nested run: {timeoutCountAfterNestedRun}");
  256. }
  257. if (capturedI == 2)
  258. {
  259. // This fires during nested run
  260. timeoutCountDuringNestedRun = app.TimedEvents!.Timeouts.Count;
  261. output.WriteLine ($"Timeout count during nested run: {timeoutCountDuringNestedRun}");
  262. // Close dialog
  263. app.RequestStop (dialog);
  264. }
  265. if (capturedI == 4)
  266. {
  267. // Stop main window
  268. app.RequestStop (mainWindow);
  269. }
  270. return false;
  271. }
  272. );
  273. }
  274. // Act
  275. app.Run (mainWindow);
  276. // Assert
  277. output.WriteLine ($"Final counts - Initial: {initialTimeoutCount}, During: {timeoutCountDuringNestedRun}, After: {timeoutCountAfterNestedRun}");
  278. // The timeout queue should have pending timeouts throughout
  279. Assert.True (initialTimeoutCount >= 0, "Should have timeouts in queue initially");
  280. Assert.True (timeoutCountDuringNestedRun >= 0, "Should have timeouts in queue during nested run");
  281. Assert.True (timeoutCountAfterNestedRun >= 0, "Should have timeouts in queue after nested run");
  282. Assert.False (requestStopTimeoutFired, "Safety timeout should NOT have fired");
  283. dialog.Dispose ();
  284. mainWindow.Dispose ();
  285. }
  286. [Fact]
  287. public void Timeout_Scheduled_Before_Nested_Run_Fires_During_Nested_Run ()
  288. {
  289. // This test specifically reproduces the ESC key issue scenario:
  290. // - Timeouts are scheduled upfront (like demo keys)
  291. // - A timeout fires and triggers a nested run (like Enter opening MessageBox)
  292. // - A subsequent timeout should still fire during the nested run (like ESC closing MessageBox)
  293. // Arrange
  294. using IApplication? app = Application.Create ();
  295. app.Init ("FakeDriver");
  296. var enterFired = false;
  297. var escFired = false;
  298. var messageBoxShown = false;
  299. var messageBoxClosed = false;
  300. var mainWindow = new Window { Title = "Login Window" };
  301. var messageBox = new Dialog { Title = "Success", Buttons = [new() { Text = "Ok" }] };
  302. // Schedule a safety timeout that will ensure the app quits if test hangs
  303. var requestStopTimeoutFired = false;
  304. app.AddTimeout (
  305. TimeSpan.FromMilliseconds (10000),
  306. () =>
  307. {
  308. output.WriteLine ("SAFETY: RequestStop Timeout fired - test took too long!");
  309. requestStopTimeoutFired = true;
  310. app.RequestStop ();
  311. return false;
  312. }
  313. );
  314. // Schedule "Enter" timeout at 100ms
  315. app.AddTimeout (
  316. TimeSpan.FromMilliseconds (100),
  317. () =>
  318. {
  319. output.WriteLine ("Enter timeout fired - showing MessageBox");
  320. enterFired = true;
  321. // Simulate Enter key opening MessageBox
  322. messageBoxShown = true;
  323. app.Run (messageBox);
  324. messageBoxClosed = true;
  325. output.WriteLine ("MessageBox closed");
  326. return false;
  327. }
  328. );
  329. // Schedule "ESC" timeout at 200ms (should fire while MessageBox is running)
  330. app.AddTimeout (
  331. TimeSpan.FromMilliseconds (200),
  332. () =>
  333. {
  334. output.WriteLine ($"ESC timeout fired - TopRunnable: {app.TopRunnableView?.Title}");
  335. escFired = true;
  336. // Simulate ESC key closing MessageBox
  337. if (app.TopRunnableView == messageBox)
  338. {
  339. output.WriteLine ("Closing MessageBox with ESC");
  340. app.RequestStop (messageBox);
  341. }
  342. return false;
  343. }
  344. );
  345. // Increased delay from 300ms to 500ms to ensure nested run completes before stopping main
  346. app.AddTimeout (
  347. TimeSpan.FromMilliseconds (500),
  348. () =>
  349. {
  350. output.WriteLine ("Stopping main window");
  351. app.RequestStop (mainWindow);
  352. return false;
  353. }
  354. );
  355. // Act
  356. app.Run (mainWindow);
  357. // Assert
  358. Assert.True (enterFired, "Enter timeout should have fired");
  359. Assert.True (messageBoxShown, "MessageBox should have been shown");
  360. Assert.True (escFired, "ESC timeout should have fired during MessageBox"); // THIS WAS THE BUG - NOW FIXED!
  361. Assert.True (messageBoxClosed, "MessageBox should have been closed");
  362. Assert.False (requestStopTimeoutFired, "Safety timeout should NOT have fired");
  363. messageBox.Dispose ();
  364. mainWindow.Dispose ();
  365. }
  366. }