MainLoopTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. // Alias Console to MockConsole so we don't accidentally use Console
  13. using Console = Terminal.Gui.FakeConsole;
  14. namespace Terminal.Gui.Core {
  15. /// <summary>
  16. /// Tests MainLoop using the FakeMainLoop.
  17. /// </summary>
  18. public class MainLoopTests {
  19. // TODO: Expand to test all the MainLoop implementations.
  20. [Fact]
  21. public void Constructor_Setups_Driver ()
  22. {
  23. var ml = new MainLoop (new FakeMainLoop ());
  24. Assert.NotNull (ml.Driver);
  25. }
  26. // Idle Handler tests
  27. [Fact]
  28. public void AddIdle_Adds_And_Removes ()
  29. {
  30. var ml = new MainLoop (new FakeMainLoop ());
  31. Func<bool> fnTrue = () => true;
  32. Func<bool> fnFalse = () => false;
  33. ml.AddIdle (fnTrue);
  34. ml.AddIdle (fnFalse);
  35. Assert.Equal (2, ml.IdleHandlers.Count);
  36. Assert.Equal (fnTrue, ml.IdleHandlers [0]);
  37. Assert.NotEqual (fnFalse, ml.IdleHandlers [0]);
  38. Assert.True (ml.RemoveIdle (fnTrue));
  39. Assert.Single (ml.IdleHandlers);
  40. // BUGBUG: This doesn't throw or indicate an error. Ideally RemoveIdle would either
  41. // throw an exception in this case, or return an error.
  42. // No. Only need to return a boolean.
  43. Assert.False (ml.RemoveIdle (fnTrue));
  44. Assert.True (ml.RemoveIdle (fnFalse));
  45. // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
  46. // throw an exception in this case, or return an error.
  47. // No. Only need to return a boolean.
  48. Assert.False (ml.RemoveIdle (fnFalse));
  49. // Add again, but with dupe
  50. ml.AddIdle (fnTrue);
  51. ml.AddIdle (fnTrue);
  52. Assert.Equal (2, ml.IdleHandlers.Count);
  53. Assert.Equal (fnTrue, ml.IdleHandlers [0]);
  54. Assert.True (ml.IdleHandlers [0] ());
  55. Assert.Equal (fnTrue, ml.IdleHandlers [1]);
  56. Assert.True (ml.IdleHandlers [1] ());
  57. Assert.True (ml.RemoveIdle (fnTrue));
  58. Assert.Single (ml.IdleHandlers);
  59. Assert.Equal (fnTrue, ml.IdleHandlers [0]);
  60. Assert.NotEqual (fnFalse, ml.IdleHandlers [0]);
  61. Assert.True (ml.RemoveIdle (fnTrue));
  62. Assert.Empty (ml.IdleHandlers);
  63. // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
  64. // throw an exception in this case, or return an error.
  65. // No. Only need to return a boolean.
  66. Assert.False (ml.RemoveIdle (fnTrue));
  67. }
  68. [Fact]
  69. public void AddIdle_Function_GetsCalled_OnIteration ()
  70. {
  71. var ml = new MainLoop (new FakeMainLoop ());
  72. var functionCalled = 0;
  73. Func<bool> fn = () => {
  74. functionCalled++;
  75. return true;
  76. };
  77. ml.AddIdle (fn);
  78. ml.MainIteration ();
  79. Assert.Equal (1, functionCalled);
  80. }
  81. [Fact]
  82. public void RemoveIdle_Function_NotCalled ()
  83. {
  84. var ml = new MainLoop (new FakeMainLoop ());
  85. var functionCalled = 0;
  86. Func<bool> fn = () => {
  87. functionCalled++;
  88. return true;
  89. };
  90. Assert.False (ml.RemoveIdle (fn));
  91. ml.MainIteration ();
  92. Assert.Equal (0, functionCalled);
  93. }
  94. [Fact]
  95. public void AddThenRemoveIdle_Function_NotCalled ()
  96. {
  97. var ml = new MainLoop (new FakeMainLoop ());
  98. var functionCalled = 0;
  99. Func<bool> fn = () => {
  100. functionCalled++;
  101. return true;
  102. };
  103. ml.AddIdle (fn);
  104. Assert.True (ml.RemoveIdle (fn));
  105. ml.MainIteration ();
  106. Assert.Equal (0, functionCalled);
  107. }
  108. [Fact]
  109. public void AddTwice_Function_CalledTwice ()
  110. {
  111. var ml = new MainLoop (new FakeMainLoop ());
  112. var functionCalled = 0;
  113. Func<bool> fn = () => {
  114. functionCalled++;
  115. return true;
  116. };
  117. ml.AddIdle (fn);
  118. ml.AddIdle (fn);
  119. ml.MainIteration ();
  120. Assert.Equal (2, functionCalled);
  121. Assert.Equal (2, ml.IdleHandlers.Count);
  122. functionCalled = 0;
  123. Assert.True (ml.RemoveIdle (fn));
  124. Assert.Single (ml.IdleHandlers);
  125. ml.MainIteration ();
  126. Assert.Equal (1, functionCalled);
  127. functionCalled = 0;
  128. Assert.True (ml.RemoveIdle (fn));
  129. Assert.Empty (ml.IdleHandlers);
  130. ml.MainIteration ();
  131. Assert.Equal (0, functionCalled);
  132. Assert.False (ml.RemoveIdle (fn));
  133. }
  134. [Fact]
  135. public void False_Idle_Stops_It_Being_Called_Again ()
  136. {
  137. var ml = new MainLoop (new FakeMainLoop ());
  138. var functionCalled = 0;
  139. Func<bool> fn1 = () => {
  140. functionCalled++;
  141. if (functionCalled == 10) {
  142. return false;
  143. }
  144. return true;
  145. };
  146. // Force stop if 20 iterations
  147. var stopCount = 0;
  148. Func<bool> fnStop = () => {
  149. stopCount++;
  150. if (stopCount == 20) {
  151. ml.Stop ();
  152. }
  153. return true;
  154. };
  155. ml.AddIdle (fnStop);
  156. ml.AddIdle (fn1);
  157. ml.Run ();
  158. Assert.True (ml.RemoveIdle (fnStop));
  159. Assert.False (ml.RemoveIdle (fn1));
  160. Assert.Equal (10, functionCalled);
  161. Assert.Equal (20, stopCount);
  162. }
  163. [Fact]
  164. public void AddIdle_Twice_Returns_False_Called_Twice ()
  165. {
  166. var ml = new MainLoop (new FakeMainLoop ());
  167. var functionCalled = 0;
  168. Func<bool> fn1 = () => {
  169. functionCalled++;
  170. return false;
  171. };
  172. // Force stop if 10 iterations
  173. var stopCount = 0;
  174. Func<bool> fnStop = () => {
  175. stopCount++;
  176. if (stopCount == 10) {
  177. ml.Stop ();
  178. }
  179. return true;
  180. };
  181. ml.AddIdle (fnStop);
  182. ml.AddIdle (fn1);
  183. ml.AddIdle (fn1);
  184. ml.Run ();
  185. Assert.True (ml.RemoveIdle (fnStop));
  186. Assert.False (ml.RemoveIdle (fn1));
  187. Assert.False (ml.RemoveIdle (fn1));
  188. Assert.Equal (2, functionCalled);
  189. }
  190. [Fact]
  191. public void Run_Runs_Idle_Stop_Stops_Idle ()
  192. {
  193. var ml = new MainLoop (new FakeMainLoop ());
  194. var functionCalled = 0;
  195. Func<bool> fn = () => {
  196. functionCalled++;
  197. if (functionCalled == 10) {
  198. ml.Stop ();
  199. }
  200. return true;
  201. };
  202. ml.AddIdle (fn);
  203. ml.Run ();
  204. Assert.True (ml.RemoveIdle (fn));
  205. Assert.Equal (10, functionCalled);
  206. }
  207. // Timeout Handler Tests
  208. [Fact]
  209. public void AddTimer_Adds_Removes_NoFaults ()
  210. {
  211. var ml = new MainLoop (new FakeMainLoop ());
  212. var ms = 100;
  213. var callbackCount = 0;
  214. Func<MainLoop, bool> callback = (MainLoop loop) => {
  215. callbackCount++;
  216. return true;
  217. };
  218. var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback);
  219. Assert.True (ml.RemoveTimeout (token));
  220. // BUGBUG: This should probably fault?
  221. // Must return a boolean.
  222. Assert.False (ml.RemoveTimeout (token));
  223. }
  224. [Fact]
  225. public void AddTimer_Run_Called ()
  226. {
  227. var ml = new MainLoop (new FakeMainLoop ());
  228. var ms = 100;
  229. var callbackCount = 0;
  230. Func<MainLoop, bool> callback = (MainLoop loop) => {
  231. callbackCount++;
  232. ml.Stop ();
  233. return true;
  234. };
  235. var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback);
  236. ml.Run ();
  237. Assert.True (ml.RemoveTimeout (token));
  238. Assert.Equal (1, callbackCount);
  239. }
  240. [Fact]
  241. public async Task AddTimer_Duplicate_Keys_Not_Allowed ()
  242. {
  243. var ml = new MainLoop (new FakeMainLoop ());
  244. const int ms = 100;
  245. object token1 = null, token2 = null;
  246. var callbackCount = 0;
  247. Func<MainLoop, bool> callback = (MainLoop loop) => {
  248. callbackCount++;
  249. if (callbackCount == 2) {
  250. ml.Stop ();
  251. }
  252. return true;
  253. };
  254. var task1 = new Task (() => token1 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback));
  255. var task2 = new Task (() => token2 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback));
  256. Assert.Null (token1);
  257. Assert.Null (token2);
  258. task1.Start ();
  259. task2.Start ();
  260. ml.Run ();
  261. Assert.NotNull (token1);
  262. Assert.NotNull (token2);
  263. await Task.WhenAll (task1, task2);
  264. Assert.True (ml.RemoveTimeout (token1));
  265. Assert.True (ml.RemoveTimeout (token2));
  266. Assert.Equal (2, callbackCount);
  267. }
  268. [Fact]
  269. public void AddTimer_In_Parallel_Wont_Throw ()
  270. {
  271. var ml = new MainLoop (new FakeMainLoop ());
  272. const int ms = 100;
  273. object token1 = null, token2 = null;
  274. var callbackCount = 0;
  275. Func<MainLoop, bool> callback = (MainLoop loop) => {
  276. callbackCount++;
  277. if (callbackCount == 2) {
  278. ml.Stop ();
  279. }
  280. return true;
  281. };
  282. Parallel.Invoke (
  283. () => token1 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback),
  284. () => token2 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback)
  285. );
  286. ml.Run ();
  287. Assert.NotNull (token1);
  288. Assert.NotNull (token2);
  289. Assert.True (ml.RemoveTimeout (token1));
  290. Assert.True (ml.RemoveTimeout (token2));
  291. Assert.Equal (2, callbackCount);
  292. }
  293. class MillisecondTolerance : IEqualityComparer<TimeSpan> {
  294. int _tolerance = 0;
  295. public MillisecondTolerance (int tolerance) { _tolerance = tolerance; }
  296. public bool Equals (TimeSpan x, TimeSpan y) => Math.Abs (x.Milliseconds - y.Milliseconds) <= _tolerance;
  297. public int GetHashCode (TimeSpan obj) => obj.GetHashCode ();
  298. }
  299. [Fact]
  300. public void AddTimer_Run_CalledAtApproximatelyRightTime ()
  301. {
  302. var ml = new MainLoop (new FakeMainLoop ());
  303. var ms = TimeSpan.FromMilliseconds (50);
  304. var watch = new System.Diagnostics.Stopwatch ();
  305. var callbackCount = 0;
  306. Func<MainLoop, bool> callback = (MainLoop loop) => {
  307. watch.Stop ();
  308. callbackCount++;
  309. ml.Stop ();
  310. return true;
  311. };
  312. var token = ml.AddTimeout (ms, callback);
  313. watch.Start ();
  314. ml.Run ();
  315. // +/- 100ms should be good enuf
  316. // https://github.com/xunit/assert.xunit/pull/25
  317. Assert.Equal<TimeSpan> (ms * callbackCount, watch.Elapsed, new MillisecondTolerance (100));
  318. Assert.True (ml.RemoveTimeout (token));
  319. Assert.Equal (1, callbackCount);
  320. }
  321. [Fact]
  322. public void AddTimer_Run_CalledTwiceApproximatelyRightTime ()
  323. {
  324. var ml = new MainLoop (new FakeMainLoop ());
  325. var ms = TimeSpan.FromMilliseconds (50);
  326. var watch = new System.Diagnostics.Stopwatch ();
  327. var callbackCount = 0;
  328. Func<MainLoop, bool> callback = (MainLoop loop) => {
  329. callbackCount++;
  330. if (callbackCount == 2) {
  331. watch.Stop ();
  332. ml.Stop ();
  333. }
  334. return true;
  335. };
  336. var token = ml.AddTimeout (ms, callback);
  337. watch.Start ();
  338. ml.Run ();
  339. // +/- 100ms should be good enuf
  340. // https://github.com/xunit/assert.xunit/pull/25
  341. Assert.Equal<TimeSpan> (ms * callbackCount, watch.Elapsed, new MillisecondTolerance (100));
  342. Assert.True (ml.RemoveTimeout (token));
  343. Assert.Equal (2, callbackCount);
  344. }
  345. [Fact]
  346. public void AddTimer_Remove_NotCalled ()
  347. {
  348. var ml = new MainLoop (new FakeMainLoop ());
  349. var ms = TimeSpan.FromMilliseconds (50);
  350. // Force stop if 10 iterations
  351. var stopCount = 0;
  352. Func<bool> fnStop = () => {
  353. stopCount++;
  354. if (stopCount == 10) {
  355. ml.Stop ();
  356. }
  357. return true;
  358. };
  359. ml.AddIdle (fnStop);
  360. var callbackCount = 0;
  361. Func<MainLoop, bool> callback = (MainLoop loop) => {
  362. callbackCount++;
  363. return true;
  364. };
  365. var token = ml.AddTimeout (ms, callback);
  366. Assert.True (ml.RemoveTimeout (token));
  367. ml.Run ();
  368. Assert.Equal (0, callbackCount);
  369. }
  370. [Fact]
  371. public void AddTimer_ReturnFalse_StopsBeingCalled ()
  372. {
  373. var ml = new MainLoop (new FakeMainLoop ());
  374. var ms = TimeSpan.FromMilliseconds (50);
  375. // Force stop if 10 iterations
  376. var stopCount = 0;
  377. Func<bool> fnStop = () => {
  378. Thread.Sleep (10); // Sleep to enable timer to fire
  379. stopCount++;
  380. if (stopCount == 10) {
  381. ml.Stop ();
  382. }
  383. return true;
  384. };
  385. ml.AddIdle (fnStop);
  386. var callbackCount = 0;
  387. Func<MainLoop, bool> callback = (MainLoop loop) => {
  388. callbackCount++;
  389. return false;
  390. };
  391. var token = ml.AddTimeout (ms, callback);
  392. ml.Run ();
  393. Assert.Equal (1, callbackCount);
  394. Assert.Equal (10, stopCount);
  395. Assert.False (ml.RemoveTimeout (token));
  396. }
  397. // Invoke Tests
  398. // TODO: Test with threading scenarios
  399. [Fact]
  400. public void Invoke_Adds_Idle ()
  401. {
  402. var ml = new MainLoop (new FakeMainLoop ());
  403. var actionCalled = 0;
  404. ml.Invoke (() => { actionCalled++; });
  405. ml.MainIteration ();
  406. Assert.Equal (1, actionCalled);
  407. }
  408. [Fact]
  409. public void Internal_Tests ()
  410. {
  411. var testMainloop = new TestMainloop ();
  412. var mainloop = new MainLoop (testMainloop);
  413. Assert.Empty (mainloop.timeouts);
  414. Assert.Empty (mainloop.idleHandlers);
  415. Assert.NotNull (new MainLoop.Timeout () {
  416. Span = new TimeSpan (),
  417. Callback = (_) => true
  418. });
  419. }
  420. private class TestMainloop : IMainLoopDriver {
  421. private MainLoop mainLoop;
  422. public bool EventsPending (bool wait)
  423. {
  424. throw new NotImplementedException ();
  425. }
  426. public void MainIteration ()
  427. {
  428. throw new NotImplementedException ();
  429. }
  430. public void Setup (MainLoop mainLoop)
  431. {
  432. this.mainLoop = mainLoop;
  433. }
  434. public void Wakeup ()
  435. {
  436. throw new NotImplementedException ();
  437. }
  438. }
  439. // TODO: EventsPending tests
  440. // - wait = true
  441. // - wait = false
  442. // TODO: Add IMainLoop tests
  443. volatile static int tbCounter = 0;
  444. static ManualResetEventSlim _wakeUp = new ManualResetEventSlim (false);
  445. private static void Launch (Random r, TextField tf, int target)
  446. {
  447. Task.Run (() => {
  448. Thread.Sleep (r.Next (2, 4));
  449. Application.MainLoop.Invoke (() => {
  450. tf.Text = $"index{r.Next ()}";
  451. Interlocked.Increment (ref tbCounter);
  452. if (target == tbCounter) {
  453. // On last increment wake up the check
  454. _wakeUp.Set ();
  455. }
  456. });
  457. });
  458. }
  459. private static void RunTest (Random r, TextField tf, int numPasses, int numIncrements, int pollMs)
  460. {
  461. for (int j = 0; j < numPasses; j++) {
  462. _wakeUp.Reset ();
  463. for (var i = 0; i < numIncrements; i++) {
  464. Launch (r, tf, (j + 1) * numIncrements);
  465. }
  466. while (tbCounter != (j + 1) * numIncrements) // Wait for tbCounter to reach expected value
  467. {
  468. var tbNow = tbCounter;
  469. _wakeUp.Wait (pollMs);
  470. if (tbCounter == tbNow) {
  471. // No change after wait: Idle handlers added via Application.MainLoop.Invoke have gone missing
  472. Application.MainLoop.Invoke (() => Application.RequestStop ());
  473. throw new TimeoutException (
  474. $"Timeout: Increment lost. tbCounter ({tbCounter}) didn't " +
  475. $"change after waiting {pollMs} ms. Failed to reach {(j + 1) * numIncrements} on pass {j + 1}");
  476. }
  477. };
  478. }
  479. Application.MainLoop.Invoke (() => Application.RequestStop ());
  480. }
  481. [Fact]
  482. [AutoInitShutdown]
  483. public async Task InvokeLeakTest ()
  484. {
  485. Random r = new ();
  486. TextField tf = new ();
  487. Application.Top.Add (tf);
  488. const int numPasses = 5;
  489. const int numIncrements = 5000;
  490. const int pollMs = 10000;
  491. var task = Task.Run (() => RunTest (r, tf, numPasses, numIncrements, pollMs));
  492. // blocks here until the RequestStop is processed at the end of the test
  493. Application.Run ();
  494. await task; // Propagate exception if any occurred
  495. Assert.Equal ((numIncrements * numPasses), tbCounter);
  496. }
  497. private static int total;
  498. private static Button btn;
  499. private static string clickMe;
  500. private static string cancel;
  501. private static string pewPew;
  502. private static int zero;
  503. private static int one;
  504. private static int two;
  505. private static int three;
  506. private static int four;
  507. private static bool taskCompleted;
  508. [Theory, AutoInitShutdown]
  509. [MemberData (nameof (TestAddIdle))]
  510. public void Mainloop_Invoke_Or_AddIdle_Can_Be_Used_For_Events_Or_Actions (Action action, string pclickMe, string pcancel, string ppewPew, int pzero, int pone, int ptwo, int pthree, int pfour)
  511. {
  512. total = 0;
  513. btn = null;
  514. clickMe = pclickMe;
  515. cancel = pcancel;
  516. pewPew = ppewPew;
  517. zero = pzero;
  518. one = pone;
  519. two = ptwo;
  520. three = pthree;
  521. four = pfour;
  522. taskCompleted = false;
  523. var btnLaunch = new Button ("Open Window");
  524. btnLaunch.Clicked += () => action ();
  525. Application.Top.Add (btnLaunch);
  526. var iterations = -1;
  527. Application.Iteration += () => {
  528. iterations++;
  529. if (iterations == 0) {
  530. Assert.Null (btn);
  531. Assert.Equal (zero, total);
  532. Assert.True (btnLaunch.ProcessKey (new KeyEvent (Key.Enter, null)));
  533. if (btn == null) {
  534. Assert.Null (btn);
  535. Assert.Equal (zero, total);
  536. } else {
  537. Assert.Equal (clickMe, btn.Text);
  538. Assert.Equal (four, total);
  539. }
  540. } else if (iterations == 1) {
  541. Assert.Equal (clickMe, btn.Text);
  542. Assert.Equal (zero, total);
  543. Assert.True (btn.ProcessKey (new KeyEvent (Key.Enter, null)));
  544. Assert.Equal (cancel, btn.Text);
  545. Assert.Equal (one, total);
  546. } else if (taskCompleted) {
  547. Application.RequestStop ();
  548. }
  549. };
  550. Application.Run ();
  551. Assert.True (taskCompleted);
  552. Assert.Equal (clickMe, btn.Text);
  553. Assert.Equal (four, total);
  554. }
  555. public static IEnumerable<object []> TestAddIdle {
  556. get {
  557. // Goes fine
  558. Action a1 = StartWindow;
  559. yield return new object [] { a1, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  560. // Also goes fine
  561. Action a2 = () => Application.MainLoop.Invoke (StartWindow);
  562. yield return new object [] { a2, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  563. }
  564. }
  565. private static void StartWindow ()
  566. {
  567. var startWindow = new Window {
  568. Modal = true
  569. };
  570. btn = new Button {
  571. Text = "Click Me"
  572. };
  573. btn.Clicked += RunAsyncTest;
  574. var totalbtn = new Button () {
  575. X = Pos.Right (btn),
  576. Text = "total"
  577. };
  578. totalbtn.Clicked += () => {
  579. MessageBox.Query ("Count", $"Count is {total}", "Ok");
  580. };
  581. startWindow.Add (btn);
  582. startWindow.Add (totalbtn);
  583. Application.Run (startWindow);
  584. Assert.Equal (clickMe, btn.Text);
  585. Assert.Equal (four, total);
  586. Application.RequestStop ();
  587. }
  588. private static async void RunAsyncTest ()
  589. {
  590. Assert.Equal (clickMe, btn.Text);
  591. Assert.Equal (zero, total);
  592. btn.Text = "Cancel";
  593. Interlocked.Increment (ref total);
  594. btn.SetNeedsDisplay ();
  595. await Task.Run (() => {
  596. try {
  597. Assert.Equal (cancel, btn.Text);
  598. Assert.Equal (one, total);
  599. RunSql ();
  600. } finally {
  601. SetReadyToRun ();
  602. }
  603. }).ContinueWith (async (s, e) => {
  604. await Task.Delay (1000);
  605. Assert.Equal (clickMe, btn.Text);
  606. Assert.Equal (three, total);
  607. Interlocked.Increment (ref total);
  608. Assert.Equal (clickMe, btn.Text);
  609. Assert.Equal (four, total);
  610. taskCompleted = true;
  611. }, TaskScheduler.FromCurrentSynchronizationContext ());
  612. }
  613. private static void RunSql ()
  614. {
  615. Thread.Sleep (100);
  616. Assert.Equal (cancel, btn.Text);
  617. Assert.Equal (one, total);
  618. Application.MainLoop.Invoke (() => {
  619. btn.Text = "Pew Pew";
  620. Interlocked.Increment (ref total);
  621. btn.SetNeedsDisplay ();
  622. });
  623. }
  624. private static void SetReadyToRun ()
  625. {
  626. Thread.Sleep (100);
  627. Assert.Equal (pewPew, btn.Text);
  628. Assert.Equal (two, total);
  629. Application.MainLoop.Invoke (() => {
  630. btn.Text = "Click Me";
  631. Interlocked.Increment (ref total);
  632. btn.SetNeedsDisplay ();
  633. });
  634. }
  635. }
  636. }