ApplicationStressTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace StressTests;
  4. public class ApplicationStressTests : TestsAllViews
  5. {
  6. public ApplicationStressTests (ITestOutputHelper output)
  7. {
  8. ConsoleDriver.RunningUnitTests = true;
  9. }
  10. private static volatile int _tbCounter;
  11. #pragma warning disable IDE1006 // Naming Styles
  12. private static readonly ManualResetEventSlim _wakeUp = new (false);
  13. #pragma warning restore IDE1006 // Naming Styles
  14. private const int NUM_PASSES = 50;
  15. private const int NUM_INCREMENTS = 500;
  16. private const int POLL_MS = 100;
  17. [Theory]
  18. [InlineData (typeof (FakeDriver))]
  19. //[InlineData (typeof (DotNetDriver), Skip = "System.IO.IOException: The handle is invalid")]
  20. //[InlineData (typeof (ANSIDriver))]
  21. //[InlineData (typeof (WindowsDriver))]
  22. //[InlineData (typeof (UnixDriver), Skip = "Unable to load DLL 'libc' or one of its dependencies: The specified module could not be found. (0x8007007E)")]
  23. public async Task InvokeLeakTest (Type driverType)
  24. {
  25. Application.Init (driverName: driverType.Name);
  26. Random r = new ();
  27. TextField tf = new ();
  28. var top = new Toplevel ();
  29. top.Add (tf);
  30. _tbCounter = 0;
  31. Task task = Task.Run (() => RunTest (r, tf, NUM_PASSES, NUM_INCREMENTS, POLL_MS));
  32. // blocks here until the RequestStop is processed at the end of the test
  33. Application.Run (top);
  34. await task; // Propagate exception if any occurred
  35. Assert.Equal (NUM_INCREMENTS * NUM_PASSES, _tbCounter);
  36. top.Dispose ();
  37. Application.Shutdown ();
  38. return;
  39. static void RunTest (Random r, TextField tf, int numPasses, int numIncrements, int pollMs)
  40. {
  41. for (var j = 0; j < numPasses; j++)
  42. {
  43. _wakeUp.Reset ();
  44. for (var i = 0; i < numIncrements; i++)
  45. {
  46. Launch (r, tf, (j + 1) * numIncrements);
  47. }
  48. while (_tbCounter != (j + 1) * numIncrements) // Wait for tbCounter to reach expected value
  49. {
  50. int tbNow = _tbCounter;
  51. _wakeUp.Wait (pollMs);
  52. if (_tbCounter != tbNow)
  53. {
  54. continue;
  55. }
  56. // No change after wait: Idle handlers added via Application.Invoke have gone missing
  57. Application.Invoke (() => Application.RequestStop ());
  58. throw new TimeoutException (
  59. $"Timeout: Increment lost. _tbCounter ({_tbCounter}) didn't "
  60. + $"change after waiting {pollMs} ms. Failed to reach {(j + 1) * numIncrements} on pass {j + 1}"
  61. );
  62. }
  63. ;
  64. }
  65. Application.Invoke (() => Application.RequestStop ());
  66. }
  67. static void Launch (Random r, TextField tf, int target)
  68. {
  69. Task.Run (
  70. () =>
  71. {
  72. Thread.Sleep (r.Next (2, 4));
  73. Application.Invoke (
  74. () =>
  75. {
  76. tf.Text = $"index{r.Next ()}";
  77. Interlocked.Increment (ref _tbCounter);
  78. if (target == _tbCounter)
  79. {
  80. // On last increment wake up the check
  81. _wakeUp.Set ();
  82. }
  83. }
  84. );
  85. }
  86. );
  87. }
  88. }
  89. }