2
0

MainLoopDriverTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using Xunit.Abstractions;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. namespace UnitTests_Parallelizable.DriverTests;
  4. public class MainLoopDriverTests : UnitTests.Parallelizable.ParallelizableBase
  5. {
  6. public MainLoopDriverTests (ITestOutputHelper output) { ConsoleDriver.RunningUnitTests = true; }
  7. [Theory]
  8. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  9. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  10. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  11. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  12. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  13. public void MainLoop_AddTimeout_ValidIdleHandler_ReturnsToken (Type driverType, Type mainLoopDriverType)
  14. {
  15. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  16. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  17. var mainLoop = new MainLoop (mainLoopDriver);
  18. var idleHandlerInvoked = false;
  19. bool IdleHandler ()
  20. {
  21. idleHandlerInvoked = true;
  22. return false;
  23. }
  24. var token = mainLoop.TimedEvents.Add(TimeSpan.Zero, IdleHandler);
  25. Assert.NotNull (token);
  26. Assert.False (idleHandlerInvoked); // Idle handler should not be invoked immediately
  27. mainLoop.RunIteration (); // Run an iteration to process the idle handler
  28. Assert.True (idleHandlerInvoked); // Idle handler should be invoked after processing
  29. mainLoop.Dispose ();
  30. }
  31. [Theory]
  32. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  33. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  34. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  35. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  36. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  37. public void MainLoop_AddTimeout_ValidParameters_ReturnsToken (Type driverType, Type mainLoopDriverType)
  38. {
  39. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  40. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  41. var mainLoop = new MainLoop (mainLoopDriver);
  42. var callbackInvoked = false;
  43. object token = mainLoop.TimedEvents.Add (
  44. TimeSpan.FromMilliseconds (100),
  45. () =>
  46. {
  47. callbackInvoked = true;
  48. return false;
  49. }
  50. );
  51. Assert.NotNull (token);
  52. mainLoop.RunIteration (); // Run an iteration to process the timeout
  53. Assert.False (callbackInvoked); // Callback should not be invoked immediately
  54. Thread.Sleep (200); // Wait for the timeout
  55. mainLoop.RunIteration (); // Run an iteration to process the timeout
  56. Assert.True (callbackInvoked); // Callback should be invoked after the timeout
  57. mainLoop.Dispose ();
  58. }
  59. [Theory]
  60. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  61. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  62. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  63. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  64. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  65. public void MainLoop_CheckTimersAndIdleHandlers_IdleHandlersActive_ReturnsTrue (
  66. Type driverType,
  67. Type mainLoopDriverType
  68. )
  69. {
  70. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  71. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  72. var mainLoop = new MainLoop (mainLoopDriver);
  73. mainLoop.TimedEvents.Add (TimeSpan.Zero, () => false);
  74. bool result = mainLoop.TimedEvents.CheckTimers (out int waitTimeout);
  75. Assert.True (result);
  76. Assert.Equal (0, waitTimeout);
  77. mainLoop.Dispose ();
  78. }
  79. [Theory]
  80. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  81. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  82. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  83. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  84. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  85. public void MainLoop_CheckTimers_NoTimersOrIdleHandlers_ReturnsFalse (
  86. Type driverType,
  87. Type mainLoopDriverType
  88. )
  89. {
  90. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  91. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  92. var mainLoop = new MainLoop (mainLoopDriver);
  93. bool result = mainLoop.TimedEvents.CheckTimers (out int waitTimeout);
  94. Assert.False (result);
  95. Assert.Equal (-1, waitTimeout);
  96. mainLoop.Dispose ();
  97. }
  98. [Theory]
  99. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  100. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  101. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  102. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  103. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  104. public void MainLoop_CheckTimersAndIdleHandlers_TimersActive_ReturnsTrue (
  105. Type driverType,
  106. Type mainLoopDriverType
  107. )
  108. {
  109. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  110. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  111. var mainLoop = new MainLoop (mainLoopDriver);
  112. mainLoop.TimedEvents.Add (TimeSpan.FromMilliseconds (100), () => false);
  113. bool result = mainLoop.TimedEvents.CheckTimers(out int waitTimeout);
  114. Assert.True (result);
  115. Assert.True (waitTimeout >= 0);
  116. mainLoop.Dispose ();
  117. }
  118. [Theory]
  119. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  120. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  121. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  122. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  123. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  124. public void MainLoop_Constructs_Disposes (Type driverType, Type mainLoopDriverType)
  125. {
  126. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  127. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  128. var mainLoop = new MainLoop (mainLoopDriver);
  129. // Check default values
  130. Assert.NotNull (mainLoop);
  131. Assert.Equal (mainLoopDriver, mainLoop.MainLoopDriver);
  132. Assert.Empty (mainLoop.TimedEvents.Timeouts);
  133. Assert.False (mainLoop.Running);
  134. // Clean up
  135. mainLoop.Dispose ();
  136. // TODO: It'd be nice if we could really verify IMainLoopDriver.TearDown was called
  137. // and that it was actually cleaned up.
  138. Assert.Null (mainLoop.MainLoopDriver);
  139. Assert.Empty (mainLoop.TimedEvents.Timeouts);
  140. Assert.False (mainLoop.Running);
  141. }
  142. [Theory]
  143. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  144. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  145. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  146. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  147. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  148. public void MainLoop_RemoveIdle_InvalidToken_ReturnsFalse (Type driverType, Type mainLoopDriverType)
  149. {
  150. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  151. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  152. var mainLoop = new MainLoop (mainLoopDriver);
  153. bool result = mainLoop.TimedEvents.Remove("flibble");
  154. Assert.False (result);
  155. mainLoop.Dispose ();
  156. }
  157. [Theory]
  158. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  159. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  160. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  161. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  162. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  163. public void MainLoop_RemoveIdle_ValidToken_ReturnsTrue (Type driverType, Type mainLoopDriverType)
  164. {
  165. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  166. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  167. var mainLoop = new MainLoop (mainLoopDriver);
  168. bool IdleHandler () { return false; }
  169. var token = mainLoop.TimedEvents.Add (TimeSpan.Zero, IdleHandler);
  170. bool result = mainLoop.TimedEvents.Remove (token);
  171. Assert.True (result);
  172. mainLoop.Dispose ();
  173. }
  174. [Theory]
  175. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  176. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  177. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  178. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  179. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  180. public void MainLoop_RemoveTimeout_InvalidToken_ReturnsFalse (Type driverType, Type mainLoopDriverType)
  181. {
  182. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  183. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  184. var mainLoop = new MainLoop (mainLoopDriver);
  185. bool result = mainLoop.TimedEvents.Remove (new object ());
  186. Assert.False (result);
  187. }
  188. [Theory]
  189. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  190. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  191. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  192. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  193. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  194. public void MainLoop_RemoveTimeout_ValidToken_ReturnsTrue (Type driverType, Type mainLoopDriverType)
  195. {
  196. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  197. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  198. var mainLoop = new MainLoop (mainLoopDriver);
  199. object token = mainLoop.TimedEvents.Add (TimeSpan.FromMilliseconds (100), () => false);
  200. bool result = mainLoop.TimedEvents.Remove (token);
  201. Assert.True (result);
  202. mainLoop.Dispose ();
  203. }
  204. [Theory]
  205. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  206. //[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  207. //[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  208. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  209. //[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
  210. public void MainLoop_RunIteration_ValidIdleHandler_CallsIdleHandler (Type driverType, Type mainLoopDriverType)
  211. {
  212. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  213. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, driver);
  214. var mainLoop = new MainLoop (mainLoopDriver);
  215. var idleHandlerInvoked = false;
  216. Func<bool> idleHandler = () =>
  217. {
  218. idleHandlerInvoked = true;
  219. return false;
  220. };
  221. mainLoop.TimedEvents.Add (TimeSpan.Zero, idleHandler);
  222. mainLoop.RunIteration (); // Run an iteration to process the idle handler
  223. Assert.True (idleHandlerInvoked);
  224. mainLoop.Dispose ();
  225. }
  226. //[Theory]
  227. //[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  228. ////[InlineData (typeof (DotNetDriver), typeof (NetMainLoop))]
  229. ////[InlineData (typeof (UnixDriver), typeof (UnixMainLoop))]
  230. ////[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  231. //public void MainLoop_Invoke_ValidAction_RunsAction (Type driverType, Type mainLoopDriverType)
  232. //{
  233. // var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  234. // var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  235. // var mainLoop = new MainLoop (mainLoopDriver);
  236. // var actionInvoked = false;
  237. // mainLoop.Invoke (() => { actionInvoked = true; });
  238. // mainLoop.RunIteration (); // Run an iteration to process the action.
  239. // Assert.True (actionInvoked);
  240. // mainLoop.Dispose ();
  241. //}
  242. }