MainLoopTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.ComTypes;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Terminal.Gui;
  10. using Xunit;
  11. using Xunit.Sdk;
  12. // Alais Console to MockConsole so we don't accidentally use Console
  13. using Console = Terminal.Gui.FakeConsole;
  14. namespace Terminal.Gui.Core {
  15. public class MainLoopTests {
  16. [Fact]
  17. public void Constructor_Setups_Driver ()
  18. {
  19. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  20. Assert.NotNull (ml.Driver);
  21. }
  22. // Idle Handler tests
  23. [Fact]
  24. public void AddIdle_Adds_And_Removes ()
  25. {
  26. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  27. Func<bool> fnTrue = () => { return true; };
  28. Func<bool> fnFalse = () => { return false; };
  29. ml.AddIdle (fnTrue);
  30. ml.AddIdle (fnFalse);
  31. Assert.True (ml.RemoveIdle (fnTrue));
  32. // BUGBUG: This doesn't throw or indicate an error. Ideally RemoveIdle would either
  33. // throw an exception in this case, or return an error.
  34. // No. Only need to return a boolean.
  35. Assert.False (ml.RemoveIdle (fnTrue));
  36. Assert.True (ml.RemoveIdle (fnFalse));
  37. // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
  38. // throw an exception in this case, or return an error.
  39. // No. Only need to return a boolean.
  40. Assert.False (ml.RemoveIdle (fnFalse));
  41. // Add again, but with dupe
  42. ml.AddIdle (fnTrue);
  43. ml.AddIdle (fnTrue);
  44. Assert.True (ml.RemoveIdle (fnTrue));
  45. Assert.True (ml.RemoveIdle (fnTrue));
  46. // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
  47. // throw an exception in this case, or return an error.
  48. // No. Only need to return a boolean.
  49. Assert.False (ml.RemoveIdle (fnTrue));
  50. }
  51. [Fact]
  52. public void AddIdle_Function_GetsCalled_OnIteration ()
  53. {
  54. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  55. var functionCalled = 0;
  56. Func<bool> fn = () => {
  57. functionCalled++;
  58. return true;
  59. };
  60. ml.AddIdle (fn);
  61. ml.MainIteration ();
  62. Assert.Equal (1, functionCalled);
  63. }
  64. [Fact]
  65. public void RemoveIdle_Function_NotCalled ()
  66. {
  67. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  68. var functionCalled = 0;
  69. Func<bool> fn = () => {
  70. functionCalled++;
  71. return true;
  72. };
  73. Assert.False (ml.RemoveIdle (fn));
  74. ml.MainIteration ();
  75. Assert.Equal (0, functionCalled);
  76. }
  77. [Fact]
  78. public void AddThenRemoveIdle_Function_NotCalled ()
  79. {
  80. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  81. var functionCalled = 0;
  82. Func<bool> fn = () => {
  83. functionCalled++;
  84. return true;
  85. };
  86. ml.AddIdle (fn);
  87. Assert.True (ml.RemoveIdle (fn));
  88. ml.MainIteration ();
  89. Assert.Equal (0, functionCalled);
  90. }
  91. [Fact]
  92. public void AddTwice_Function_CalledTwice ()
  93. {
  94. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  95. var functionCalled = 0;
  96. Func<bool> fn = () => {
  97. functionCalled++;
  98. return true;
  99. };
  100. ml.AddIdle (fn);
  101. ml.AddIdle (fn);
  102. ml.MainIteration ();
  103. Assert.Equal (2, functionCalled);
  104. functionCalled = 0;
  105. Assert.True (ml.RemoveIdle (fn));
  106. ml.MainIteration ();
  107. Assert.Equal (1, functionCalled);
  108. functionCalled = 0;
  109. Assert.True (ml.RemoveIdle (fn));
  110. ml.MainIteration ();
  111. Assert.Equal (0, functionCalled);
  112. Assert.False (ml.RemoveIdle (fn));
  113. }
  114. [Fact]
  115. public void False_Idle_Stops_It_Being_Called_Again ()
  116. {
  117. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  118. var functionCalled = 0;
  119. Func<bool> fn1 = () => {
  120. functionCalled++;
  121. if (functionCalled == 10) {
  122. return false;
  123. }
  124. return true;
  125. };
  126. // Force stop if 20 iterations
  127. var stopCount = 0;
  128. Func<bool> fnStop = () => {
  129. stopCount++;
  130. if (stopCount == 20) {
  131. ml.Stop ();
  132. }
  133. return true;
  134. };
  135. ml.AddIdle (fnStop);
  136. ml.AddIdle (fn1);
  137. ml.Run ();
  138. Assert.True (ml.RemoveIdle (fnStop));
  139. Assert.False (ml.RemoveIdle (fn1));
  140. Assert.Equal (10, functionCalled);
  141. Assert.Equal (20, stopCount);
  142. }
  143. [Fact]
  144. public void AddIdle_Twice_Returns_False_Called_Twice ()
  145. {
  146. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  147. var functionCalled = 0;
  148. Func<bool> fn1 = () => {
  149. functionCalled++;
  150. return false;
  151. };
  152. // Force stop if 10 iterations
  153. var stopCount = 0;
  154. Func<bool> fnStop = () => {
  155. stopCount++;
  156. if (stopCount == 10) {
  157. ml.Stop ();
  158. }
  159. return true;
  160. };
  161. ml.AddIdle (fnStop);
  162. ml.AddIdle (fn1);
  163. ml.AddIdle (fn1);
  164. ml.Run ();
  165. Assert.True (ml.RemoveIdle (fnStop));
  166. Assert.False (ml.RemoveIdle (fn1));
  167. Assert.False (ml.RemoveIdle (fn1));
  168. Assert.Equal (2, functionCalled);
  169. }
  170. [Fact]
  171. public void Run_Runs_Idle_Stop_Stops_Idle ()
  172. {
  173. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  174. var functionCalled = 0;
  175. Func<bool> fn = () => {
  176. functionCalled++;
  177. if (functionCalled == 10) {
  178. ml.Stop ();
  179. }
  180. return true;
  181. };
  182. ml.AddIdle (fn);
  183. ml.Run ();
  184. Assert.True (ml.RemoveIdle (fn));
  185. Assert.Equal (10, functionCalled);
  186. }
  187. // Timeout Handler Tests
  188. [Fact]
  189. public void AddTimer_Adds_Removes_NoFaults ()
  190. {
  191. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  192. var ms = 100;
  193. var callbackCount = 0;
  194. Func<MainLoop, bool> callback = (MainLoop loop) => {
  195. callbackCount++;
  196. return true;
  197. };
  198. var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback);
  199. Assert.True (ml.RemoveTimeout (token));
  200. // BUGBUG: This should probably fault?
  201. // Must return a boolean.
  202. Assert.False (ml.RemoveTimeout (token));
  203. }
  204. [Fact]
  205. public void AddTimer_Run_Called ()
  206. {
  207. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  208. var ms = 100;
  209. var callbackCount = 0;
  210. Func<MainLoop, bool> callback = (MainLoop loop) => {
  211. callbackCount++;
  212. ml.Stop ();
  213. return true;
  214. };
  215. var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback);
  216. ml.Run ();
  217. Assert.True (ml.RemoveTimeout (token));
  218. Assert.Equal (1, callbackCount);
  219. }
  220. [Fact]
  221. public async Task AddTimer_Duplicate_Keys_Not_Allowed ()
  222. {
  223. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  224. const int ms = 100;
  225. object token1 = null, token2 = null;
  226. var callbackCount = 0;
  227. Func<MainLoop, bool> callback = (MainLoop loop) => {
  228. callbackCount++;
  229. if (callbackCount == 2) {
  230. ml.Stop ();
  231. }
  232. return true;
  233. };
  234. var task1 = new Task (() => token1 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback));
  235. var task2 = new Task (() => token2 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback));
  236. Assert.Null (token1);
  237. Assert.Null (token2);
  238. task1.Start ();
  239. task2.Start ();
  240. ml.Run ();
  241. Assert.NotNull (token1);
  242. Assert.NotNull (token2);
  243. await Task.WhenAll (task1, task2);
  244. Assert.True (ml.RemoveTimeout (token1));
  245. Assert.True (ml.RemoveTimeout (token2));
  246. Assert.Equal (2, callbackCount);
  247. }
  248. [Fact]
  249. public void AddTimer_In_Parallel_Wont_Throw ()
  250. {
  251. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  252. const int ms = 100;
  253. object token1 = null, token2 = null;
  254. var callbackCount = 0;
  255. Func<MainLoop, bool> callback = (MainLoop loop) => {
  256. callbackCount++;
  257. if (callbackCount == 2) {
  258. ml.Stop ();
  259. }
  260. return true;
  261. };
  262. Parallel.Invoke (
  263. () => token1 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback),
  264. () => token2 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback)
  265. );
  266. ml.Run ();
  267. Assert.NotNull (token1);
  268. Assert.NotNull (token2);
  269. Assert.True (ml.RemoveTimeout (token1));
  270. Assert.True (ml.RemoveTimeout (token2));
  271. Assert.Equal (2, callbackCount);
  272. }
  273. class MillisecondTolerance : IEqualityComparer<TimeSpan> {
  274. int _tolerance = 0;
  275. public MillisecondTolerance (int tolerance) { _tolerance = tolerance; }
  276. public bool Equals (TimeSpan x, TimeSpan y) => Math.Abs (x.Milliseconds - y.Milliseconds) <= _tolerance;
  277. public int GetHashCode (TimeSpan obj) => obj.GetHashCode ();
  278. }
  279. [Fact]
  280. public void AddTimer_Run_CalledAtApproximatelyRightTime ()
  281. {
  282. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  283. var ms = TimeSpan.FromMilliseconds (50);
  284. var watch = new System.Diagnostics.Stopwatch ();
  285. var callbackCount = 0;
  286. Func<MainLoop, bool> callback = (MainLoop loop) => {
  287. watch.Stop ();
  288. callbackCount++;
  289. ml.Stop ();
  290. return true;
  291. };
  292. var token = ml.AddTimeout (ms, callback);
  293. watch.Start ();
  294. ml.Run ();
  295. // +/- 100ms should be good enuf
  296. // https://github.com/xunit/assert.xunit/pull/25
  297. Assert.Equal<TimeSpan> (ms * callbackCount, watch.Elapsed, new MillisecondTolerance (100));
  298. Assert.True (ml.RemoveTimeout (token));
  299. Assert.Equal (1, callbackCount);
  300. }
  301. [Fact]
  302. public void AddTimer_Run_CalledTwiceApproximatelyRightTime ()
  303. {
  304. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  305. var ms = TimeSpan.FromMilliseconds (50);
  306. var watch = new System.Diagnostics.Stopwatch ();
  307. var callbackCount = 0;
  308. Func<MainLoop, bool> callback = (MainLoop loop) => {
  309. callbackCount++;
  310. if (callbackCount == 2) {
  311. watch.Stop ();
  312. ml.Stop ();
  313. }
  314. return true;
  315. };
  316. var token = ml.AddTimeout (ms, callback);
  317. watch.Start ();
  318. ml.Run ();
  319. // +/- 100ms should be good enuf
  320. // https://github.com/xunit/assert.xunit/pull/25
  321. Assert.Equal<TimeSpan> (ms * callbackCount, watch.Elapsed, new MillisecondTolerance (100));
  322. Assert.True (ml.RemoveTimeout (token));
  323. Assert.Equal (2, callbackCount);
  324. }
  325. [Fact]
  326. public void AddTimer_Remove_NotCalled ()
  327. {
  328. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  329. var ms = TimeSpan.FromMilliseconds (50);
  330. // Force stop if 10 iterations
  331. var stopCount = 0;
  332. Func<bool> fnStop = () => {
  333. stopCount++;
  334. if (stopCount == 10) {
  335. ml.Stop ();
  336. }
  337. return true;
  338. };
  339. ml.AddIdle (fnStop);
  340. var callbackCount = 0;
  341. Func<MainLoop, bool> callback = (MainLoop loop) => {
  342. callbackCount++;
  343. return true;
  344. };
  345. var token = ml.AddTimeout (ms, callback);
  346. Assert.True (ml.RemoveTimeout (token));
  347. ml.Run ();
  348. Assert.Equal (0, callbackCount);
  349. }
  350. [Fact]
  351. public void AddTimer_ReturnFalse_StopsBeingCalled ()
  352. {
  353. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  354. var ms = TimeSpan.FromMilliseconds (50);
  355. // Force stop if 10 iterations
  356. var stopCount = 0;
  357. Func<bool> fnStop = () => {
  358. Thread.Sleep (10); // Sleep to enable timer to fire
  359. stopCount++;
  360. if (stopCount == 10) {
  361. ml.Stop ();
  362. }
  363. return true;
  364. };
  365. ml.AddIdle (fnStop);
  366. var callbackCount = 0;
  367. Func<MainLoop, bool> callback = (MainLoop loop) => {
  368. callbackCount++;
  369. return false;
  370. };
  371. var token = ml.AddTimeout (ms, callback);
  372. ml.Run ();
  373. Assert.Equal (1, callbackCount);
  374. Assert.Equal (10, stopCount);
  375. Assert.False (ml.RemoveTimeout (token));
  376. }
  377. // Invoke Tests
  378. // TODO: Test with threading scenarios
  379. [Fact]
  380. public void Invoke_Adds_Idle ()
  381. {
  382. var ml = new MainLoop (new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  383. var actionCalled = 0;
  384. ml.Invoke (() => { actionCalled++; });
  385. ml.MainIteration ();
  386. Assert.Equal (1, actionCalled);
  387. }
  388. // TODO: EventsPending tests
  389. // - wait = true
  390. // - wait = false
  391. // TODO: Add IMainLoop tests
  392. }
  393. }