NestedRunTimeoutTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 safetyRequestStopTimeoutFired = false;
  133. app.AddTimeout (
  134. TimeSpan.FromMilliseconds (10000),
  135. () =>
  136. {
  137. output.WriteLine ("SAFETY: RequestStop Timeout fired - test took too long - Assuming slow environment. Skipping assertions.");
  138. safetyRequestStopTimeoutFired = 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. if (!safetyRequestStopTimeoutFired)
  177. {
  178. // Assert
  179. Assert.True (nestedRunStarted, "Nested run should have started");
  180. Assert.True (timeoutFired, "Timeout should have fired during nested run");
  181. Assert.True (nestedRunEnded, "Nested run should have ended");
  182. }
  183. dialog.Dispose ();
  184. mainWindow.Dispose ();
  185. }
  186. [Fact]
  187. public void Timeout_Fires_With_Single_Session ()
  188. {
  189. // Arrange
  190. using IApplication? app = Application.Create ();
  191. app.Init ("FakeDriver");
  192. // Create a simple window for the main run loop
  193. var mainWindow = new Window { Title = "Main Window" };
  194. // Schedule a timeout that will ensure the app quits
  195. var requestStopTimeoutFired = false;
  196. app.AddTimeout (
  197. TimeSpan.FromMilliseconds (100),
  198. () =>
  199. {
  200. output.WriteLine ("RequestStop Timeout fired!");
  201. requestStopTimeoutFired = true;
  202. app.RequestStop ();
  203. return false;
  204. }
  205. );
  206. // Act - Start the main run loop
  207. app.Run (mainWindow);
  208. // Assert
  209. Assert.True (requestStopTimeoutFired, "RequestStop Timeout should have fired");
  210. mainWindow.Dispose ();
  211. }
  212. [Fact]
  213. public void Timeout_Queue_Persists_Across_Nested_Runs ()
  214. {
  215. // Verify that the timeout queue is not cleared when nested runs start/end
  216. // Arrange
  217. using IApplication? app = Application.Create ();
  218. app.Init ("FakeDriver");
  219. // Schedule a safety timeout that will ensure the app quits if test hangs
  220. var safetyRequestStopTimeoutFired = false;
  221. app.AddTimeout (
  222. TimeSpan.FromMilliseconds (10000),
  223. () =>
  224. {
  225. output.WriteLine ("SAFETY: RequestStop Timeout fired - test took too long - Assuming slow environment. Skipping assertions.");
  226. safetyRequestStopTimeoutFired = true;
  227. app.RequestStop ();
  228. return false;
  229. }
  230. );
  231. var mainWindow = new Window { Title = "Main Window" };
  232. var dialog = new Dialog { Title = "Dialog", Buttons = [new () { Text = "Ok" }] };
  233. var initialTimeoutCount = 0;
  234. var timeoutCountDuringNestedRun = 0;
  235. var timeoutCountAfterNestedRun = 0;
  236. // Schedule 5 timeouts at different times with wider spacing
  237. for (var i = 0; i < 5; i++)
  238. {
  239. int capturedI = i;
  240. app.AddTimeout (
  241. TimeSpan.FromMilliseconds (150 * (i + 1)), // Increased spacing from 100ms to 150ms
  242. () =>
  243. {
  244. output.WriteLine ($"Timeout {capturedI} fired at {150 * (capturedI + 1)}ms");
  245. if (capturedI == 0)
  246. {
  247. initialTimeoutCount = app.TimedEvents!.Timeouts.Count;
  248. output.WriteLine ($"Initial timeout count: {initialTimeoutCount}");
  249. }
  250. if (capturedI == 1)
  251. {
  252. // Start nested run
  253. output.WriteLine ("Starting nested run");
  254. app.Run (dialog);
  255. output.WriteLine ("Nested run ended");
  256. timeoutCountAfterNestedRun = app.TimedEvents!.Timeouts.Count;
  257. output.WriteLine ($"Timeout count after nested run: {timeoutCountAfterNestedRun}");
  258. }
  259. if (capturedI == 2)
  260. {
  261. // This fires during nested run
  262. timeoutCountDuringNestedRun = app.TimedEvents!.Timeouts.Count;
  263. output.WriteLine ($"Timeout count during nested run: {timeoutCountDuringNestedRun}");
  264. // Close dialog
  265. app.RequestStop (dialog);
  266. }
  267. if (capturedI == 4)
  268. {
  269. // Stop main window
  270. app.RequestStop (mainWindow);
  271. }
  272. return false;
  273. }
  274. );
  275. }
  276. // Act
  277. app.Run (mainWindow);
  278. // Assert
  279. output.WriteLine ($"Final counts - Initial: {initialTimeoutCount}, During: {timeoutCountDuringNestedRun}, After: {timeoutCountAfterNestedRun}");
  280. if (!safetyRequestStopTimeoutFired)
  281. {
  282. // The timeout queue should have pending timeouts throughout
  283. Assert.True (initialTimeoutCount >= 0, "Should have timeouts in queue initially");
  284. Assert.True (timeoutCountDuringNestedRun >= 0, "Should have timeouts in queue during nested run");
  285. Assert.True (timeoutCountAfterNestedRun >= 0, "Should have timeouts in queue after nested run");
  286. }
  287. dialog.Dispose ();
  288. mainWindow.Dispose ();
  289. }
  290. [Fact]
  291. public void Timeout_Scheduled_Before_Nested_Run_Fires_During_Nested_Run ()
  292. {
  293. // This test specifically reproduces the ESC key issue scenario:
  294. // - Timeouts are scheduled upfront (like demo keys)
  295. // - A timeout fires and triggers a nested run (like Enter opening MessageBox)
  296. // - A subsequent timeout should still fire during the nested run (like ESC closing MessageBox)
  297. // Arrange
  298. using IApplication? app = Application.Create ();
  299. app.Init ("FakeDriver");
  300. var enterFired = false;
  301. var escFired = false;
  302. var messageBoxShown = false;
  303. var messageBoxClosed = false;
  304. var mainWindow = new Window { Title = "Login Window" };
  305. var messageBox = new Dialog { Title = "Success", Buttons = [new () { Text = "Ok" }] };
  306. // Schedule a safety timeout that will ensure the app quits if test hangs
  307. var safetyRequestStopTimeoutFired = false;
  308. app.AddTimeout (
  309. TimeSpan.FromMilliseconds (10000),
  310. () =>
  311. {
  312. output.WriteLine ("SAFETY: RequestStop Timeout fired - test took too long - Assuming slow environment. Skipping assertions.");
  313. safetyRequestStopTimeoutFired = true;
  314. app.RequestStop ();
  315. return false;
  316. }
  317. );
  318. // Schedule "Enter" timeout at 100ms
  319. app.AddTimeout (
  320. TimeSpan.FromMilliseconds (100),
  321. () =>
  322. {
  323. output.WriteLine ("Enter timeout fired - showing MessageBox");
  324. enterFired = true;
  325. // Simulate Enter key opening MessageBox
  326. messageBoxShown = true;
  327. app.Run (messageBox);
  328. messageBoxClosed = true;
  329. output.WriteLine ("MessageBox closed");
  330. return false;
  331. }
  332. );
  333. // Schedule "ESC" timeout at 200ms (should fire while MessageBox is running)
  334. app.AddTimeout (
  335. TimeSpan.FromMilliseconds (200),
  336. () =>
  337. {
  338. output.WriteLine ($"ESC timeout fired - TopRunnable: {app.TopRunnableView?.Title}");
  339. escFired = true;
  340. // Simulate ESC key closing MessageBox
  341. if (app.TopRunnableView == messageBox)
  342. {
  343. output.WriteLine ("Closing MessageBox with ESC");
  344. app.RequestStop (messageBox);
  345. }
  346. return false;
  347. }
  348. );
  349. // Increased delay from 300ms to 500ms to ensure nested run completes before stopping main
  350. app.AddTimeout (
  351. TimeSpan.FromMilliseconds (500),
  352. () =>
  353. {
  354. output.WriteLine ("Stopping main window");
  355. app.RequestStop (mainWindow);
  356. return false;
  357. }
  358. );
  359. // Act
  360. app.Run (mainWindow);
  361. if (!safetyRequestStopTimeoutFired)
  362. {
  363. // Assert
  364. Assert.True (enterFired, "Enter timeout should have fired");
  365. Assert.True (messageBoxShown, "MessageBox should have been shown");
  366. Assert.True (escFired, "ESC timeout should have fired during MessageBox"); // THIS WAS THE BUG - NOW FIXED!
  367. Assert.True (messageBoxClosed, "MessageBox should have been closed");
  368. }
  369. messageBox.Dispose ();
  370. mainWindow.Dispose ();
  371. }
  372. }