RunnableIntegrationTests.cs 14 KB

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