RunnableIntegrationTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #nullable enable
  2. using Xunit.Abstractions;
  3. namespace ApplicationTests;
  4. /// <summary>
  5. /// Integration tests for IApplication's IRunnable support.
  6. /// Tests the full lifecycle of IRunnable instances through Application methods.
  7. /// </summary>
  8. public class ApplicationRunnableIntegrationTests (ITestOutputHelper output) : IDisposable
  9. {
  10. private readonly ITestOutputHelper _output = output;
  11. private IApplication? _app;
  12. public void Dispose ()
  13. {
  14. _app?.Dispose ();
  15. _app = null;
  16. }
  17. [Fact]
  18. public void Begin_AddsRunnableToStack ()
  19. {
  20. // Arrange
  21. IApplication app = GetApp ();
  22. Runnable<int> runnable = new ();
  23. int stackCountBefore = app.SessionStack?.Count ?? 0;
  24. // Act
  25. SessionToken? token = app.Begin (runnable);
  26. // Assert
  27. Assert.NotNull (token);
  28. Assert.NotNull (token.Runnable);
  29. Assert.Same (runnable, token.Runnable);
  30. Assert.Equal (stackCountBefore + 1, app.SessionStack?.Count ?? 0);
  31. // Cleanup
  32. app.End (token!);
  33. }
  34. [Fact]
  35. public void Begin_CanBeCanceled_ByIsRunningChanging ()
  36. {
  37. // Arrange
  38. IApplication app = GetApp ();
  39. CancelableRunnable runnable = new () { CancelStart = true };
  40. // Act
  41. SessionToken? token = app.Begin (runnable);
  42. // Assert - Should not be added to stack if canceled
  43. Assert.False (runnable.IsRunning);
  44. // Token not created
  45. Assert.Null (token);
  46. }
  47. [Fact]
  48. public void Begin_RaisesIsModalChangedEvent ()
  49. {
  50. // Arrange
  51. IApplication app = GetApp ();
  52. Runnable<int> runnable = new ();
  53. var isModalChangedRaised = false;
  54. bool? receivedValue = null;
  55. runnable.IsModalChanged += (s, e) =>
  56. {
  57. isModalChangedRaised = true;
  58. receivedValue = e.Value;
  59. };
  60. // Act
  61. SessionToken? token = app.Begin (runnable);
  62. // Assert
  63. Assert.True (isModalChangedRaised);
  64. Assert.True (receivedValue);
  65. // Cleanup
  66. app.End (token!);
  67. }
  68. [Fact]
  69. public void Begin_RaisesIsRunningChangedEvent ()
  70. {
  71. // Arrange
  72. IApplication app = GetApp ();
  73. Runnable<int> runnable = new ();
  74. var isRunningChangedRaised = false;
  75. bool? receivedValue = null;
  76. runnable.IsRunningChanged += (s, e) =>
  77. {
  78. isRunningChangedRaised = true;
  79. receivedValue = e.Value;
  80. };
  81. // Act
  82. SessionToken? token = app.Begin (runnable);
  83. // Assert
  84. Assert.True (isRunningChangedRaised);
  85. Assert.True (receivedValue);
  86. // Cleanup
  87. app.End (token!);
  88. }
  89. [Fact]
  90. public void Begin_RaisesIsRunningChangingEvent ()
  91. {
  92. // Arrange
  93. IApplication app = GetApp ();
  94. Runnable<int> runnable = new ();
  95. var isRunningChangingRaised = false;
  96. bool? oldValue = null;
  97. bool? newValue = null;
  98. runnable.IsRunningChanging += (s, e) =>
  99. {
  100. isRunningChangingRaised = true;
  101. oldValue = e.CurrentValue;
  102. newValue = e.NewValue;
  103. };
  104. // Act
  105. SessionToken? token = app.Begin (runnable);
  106. // Assert
  107. Assert.True (isRunningChangingRaised);
  108. Assert.False (oldValue);
  109. Assert.True (newValue);
  110. // Cleanup
  111. app.End (token!);
  112. }
  113. [Fact]
  114. public void Begin_SetsIsModalToTrue ()
  115. {
  116. // Arrange
  117. IApplication app = GetApp ();
  118. Runnable<int> runnable = new ();
  119. // Act
  120. SessionToken? token = app.Begin (runnable);
  121. // Assert
  122. Assert.True (runnable.IsModal);
  123. // Cleanup
  124. app.End (token!);
  125. }
  126. [Fact]
  127. public void Begin_SetsIsRunningToTrue ()
  128. {
  129. // Arrange
  130. IApplication app = GetApp ();
  131. Runnable<int> runnable = new ();
  132. // Act
  133. SessionToken? token = app.Begin (runnable);
  134. // Assert
  135. Assert.True (runnable.IsRunning);
  136. // Cleanup
  137. app.End (token!);
  138. }
  139. [Fact]
  140. public void Begin_ThrowsOnNullRunnable ()
  141. {
  142. // Arrange
  143. IApplication app = GetApp ();
  144. // Act & Assert
  145. Assert.Throws<ArgumentNullException> (() => app.Begin ((IRunnable)null!));
  146. }
  147. [Fact]
  148. public void End_CanBeCanceled_ByIsRunningChanging ()
  149. {
  150. // Arrange
  151. IApplication app = GetApp ();
  152. CancelableRunnable runnable = new () { CancelStop = true };
  153. SessionToken? token = app.Begin (runnable);
  154. runnable.CancelStop = true; // Enable cancellation
  155. // Act
  156. app.End (token!);
  157. // Assert - Should still be running if canceled
  158. Assert.True (runnable.IsRunning);
  159. // Force end by disabling cancellation
  160. runnable.CancelStop = false;
  161. app.End (token!);
  162. }
  163. [Fact]
  164. public void End_ClearsTokenRunnable ()
  165. {
  166. // Arrange
  167. IApplication app = GetApp ();
  168. Runnable<int> runnable = new ();
  169. SessionToken? token = app.Begin (runnable);
  170. // Act
  171. app.End (token!);
  172. // Assert
  173. Assert.Null (token!.Runnable);
  174. }
  175. [Fact]
  176. public void End_RaisesIsRunningChangedEvent ()
  177. {
  178. // Arrange
  179. IApplication app = GetApp ();
  180. Runnable<int> runnable = new ();
  181. SessionToken? token = app.Begin (runnable);
  182. var isRunningChangedRaised = false;
  183. bool? receivedValue = null;
  184. runnable.IsRunningChanged += (s, e) =>
  185. {
  186. isRunningChangedRaised = true;
  187. receivedValue = e.Value;
  188. };
  189. // Act
  190. app.End (token!);
  191. // Assert
  192. Assert.True (isRunningChangedRaised);
  193. Assert.False (receivedValue);
  194. }
  195. [Fact]
  196. public void End_RaisesIsRunningChangingEvent ()
  197. {
  198. // Arrange
  199. IApplication app = GetApp ();
  200. Runnable<int> runnable = new ();
  201. SessionToken? token = app.Begin (runnable);
  202. var isRunningChangingRaised = false;
  203. bool? oldValue = null;
  204. bool? newValue = null;
  205. runnable.IsRunningChanging += (s, e) =>
  206. {
  207. isRunningChangingRaised = true;
  208. oldValue = e.CurrentValue;
  209. newValue = e.NewValue;
  210. };
  211. // Act
  212. app.End (token!);
  213. // Assert
  214. Assert.True (isRunningChangingRaised);
  215. Assert.True (oldValue);
  216. Assert.False (newValue);
  217. }
  218. [Fact]
  219. public void End_RemovesRunnableFromStack ()
  220. {
  221. // Arrange
  222. IApplication app = GetApp ();
  223. Runnable<int> runnable = new ();
  224. SessionToken? token = app.Begin (runnable);
  225. int stackCountBefore = app.SessionStack?.Count ?? 0;
  226. // Act
  227. app.End (token!);
  228. // Assert
  229. Assert.Equal (stackCountBefore - 1, app.SessionStack?.Count ?? 0);
  230. }
  231. [Fact]
  232. public void End_SetsIsModalToFalse ()
  233. {
  234. // Arrange
  235. IApplication app = GetApp ();
  236. Runnable<int> runnable = new ();
  237. SessionToken? token = app.Begin (runnable);
  238. // Act
  239. app.End (token!);
  240. // Assert
  241. Assert.False (runnable.IsModal);
  242. }
  243. [Fact]
  244. public void End_SetsIsRunningToFalse ()
  245. {
  246. // Arrange
  247. IApplication app = GetApp ();
  248. Runnable<int> runnable = new ();
  249. SessionToken? token = app.Begin (runnable);
  250. // Act
  251. app.End (token!);
  252. // Assert
  253. Assert.False (runnable.IsRunning);
  254. }
  255. [Fact]
  256. public void End_ThrowsOnNullToken ()
  257. {
  258. // Arrange
  259. IApplication app = GetApp ();
  260. // Act & Assert
  261. Assert.Throws<ArgumentNullException> (() => app.End ((SessionToken)null!));
  262. }
  263. [Fact]
  264. public void MultipleRunnables_IndependentResults ()
  265. {
  266. // Arrange
  267. IApplication app = GetApp ();
  268. Runnable<int> runnable1 = new ();
  269. Runnable<string> runnable2 = new ();
  270. // Act
  271. runnable1.Result = 42;
  272. runnable2.Result = "test";
  273. // Assert
  274. Assert.Equal (42, runnable1.Result);
  275. Assert.Equal ("test", runnable2.Result);
  276. }
  277. [Fact]
  278. public void NestedBegin_MaintainsStackOrder ()
  279. {
  280. // Arrange
  281. IApplication app = GetApp ();
  282. Runnable<int> runnable1 = new () { Id = "1" };
  283. Runnable<int> runnable2 = new () { Id = "2" };
  284. // Act
  285. SessionToken token1 = app.Begin (runnable1)!;
  286. SessionToken token2 = app.Begin (runnable2)!;
  287. // Assert - runnable2 should be on top
  288. Assert.True (runnable2.IsModal);
  289. Assert.False (runnable1.IsModal);
  290. Assert.True (runnable1.IsRunning); // Still running, just not modal
  291. Assert.True (runnable2.IsRunning);
  292. // Cleanup
  293. app.End (token2);
  294. app.End (token1);
  295. }
  296. [Fact]
  297. public void NestedEnd_RestoresPreviousModal ()
  298. {
  299. // Arrange
  300. IApplication app = GetApp ();
  301. Runnable<int> runnable1 = new () { Id = "1" };
  302. Runnable<int> runnable2 = new () { Id = "2" };
  303. SessionToken token1 = app.Begin (runnable1)!;
  304. SessionToken token2 = app.Begin (runnable2)!;
  305. // Act - End the top runnable
  306. app.End (token2);
  307. // Assert - runnable1 should become modal again
  308. Assert.True (runnable1.IsModal);
  309. Assert.False (runnable2.IsModal);
  310. Assert.True (runnable1.IsRunning);
  311. Assert.False (runnable2.IsRunning);
  312. // Cleanup
  313. app.End (token1);
  314. }
  315. [Fact]
  316. public void RequestStop_WithIRunnable_WorksCorrectly ()
  317. {
  318. // Arrange
  319. IApplication app = GetApp ();
  320. StoppableRunnable runnable = new ();
  321. SessionToken? token = app.Begin (runnable);
  322. // Act
  323. app.RequestStop (runnable);
  324. // Assert - RequestStop should trigger End eventually
  325. // For now, just verify it doesn't throw
  326. Assert.NotNull (runnable);
  327. // Cleanup
  328. app.End (token!);
  329. }
  330. [Fact]
  331. public void RequestStop_WithNull_UsesTopRunnable ()
  332. {
  333. // Arrange
  334. IApplication app = GetApp ();
  335. StoppableRunnable runnable = new ();
  336. SessionToken? token = app.Begin (runnable);
  337. // Act
  338. app.RequestStop ((IRunnable?)null);
  339. // Assert - Should not throw
  340. Assert.NotNull (runnable);
  341. // Cleanup
  342. app.End (token!);
  343. }
  344. [Fact (Skip = "Run methods with main loop are not suitable for parallel tests - use non-parallel UnitTests instead")]
  345. public void RunGeneric_CreatesAndReturnsRunnable ()
  346. {
  347. // Arrange
  348. IApplication app = GetApp ();
  349. app.StopAfterFirstIteration = true;
  350. // Act - With fluent API, Run<T>() returns IApplication for chaining
  351. IApplication result = app.Run<TestRunnable> ();
  352. // Assert
  353. Assert.NotNull (result);
  354. Assert.Same (app, result); // Fluent API returns this
  355. // Note: Run blocks until stopped, but StopAfterFirstIteration makes it return immediately
  356. // The runnable is automatically disposed by Dispose()
  357. }
  358. [Fact (Skip = "Run methods with main loop are not suitable for parallel tests - use non-parallel UnitTests instead")]
  359. public void RunGeneric_ThrowsIfNotInitialized ()
  360. {
  361. // Arrange
  362. IApplication app = Application.Create ();
  363. // Don't call Init
  364. // Act & Assert
  365. Assert.Throws<NotInitializedException> (() => app.Run<TestRunnable> ());
  366. // Cleanup
  367. app.Dispose ();
  368. }
  369. private IApplication GetApp ()
  370. {
  371. if (_app is null)
  372. {
  373. _app = Application.Create ();
  374. _app.Init ("fake");
  375. }
  376. return _app;
  377. }
  378. /// <summary>
  379. /// Test runnable that can cancel lifecycle changes.
  380. /// </summary>
  381. private class CancelableRunnable : Runnable<int>
  382. {
  383. public bool CancelStart { get; set; }
  384. public bool CancelStop { get; set; }
  385. protected override bool OnIsRunningChanging (bool oldIsRunning, bool newIsRunning)
  386. {
  387. if (newIsRunning && CancelStart)
  388. {
  389. return true; // Cancel starting
  390. }
  391. if (!newIsRunning && CancelStop)
  392. {
  393. return true; // Cancel stopping
  394. }
  395. return base.OnIsRunningChanging (oldIsRunning, newIsRunning);
  396. }
  397. }
  398. /// <summary>
  399. /// Test runnable that can be stopped.
  400. /// </summary>
  401. private class StoppableRunnable : Runnable<int>
  402. {
  403. public override void RequestStop ()
  404. {
  405. WasStopRequested = true;
  406. base.RequestStop ();
  407. }
  408. public bool WasStopRequested { get; private set; }
  409. }
  410. /// <summary>
  411. /// Test runnable for generic Run tests.
  412. /// </summary>
  413. private class TestRunnable : Runnable<int>
  414. {
  415. public TestRunnable () { Id = "TestRunnable"; }
  416. }
  417. }