RunnableIntegrationTests.cs 14 KB

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