Selaa lähdekoodia

Fixes #4164. FluentTests sometime causes unit tests failures locally and in CI (#4165)

BDisp 2 kuukautta sitten
vanhempi
commit
497ddb3b3e
1 muutettua tiedostoa jossa 43 lisäystä ja 39 poistoa
  1. 43 39
      Tests/TerminalGuiFluentTesting/GuiTestContext.cs

+ 43 - 39
Tests/TerminalGuiFluentTesting/GuiTestContext.cs

@@ -21,6 +21,7 @@ public class GuiTestContext : IDisposable
     private readonly StringBuilder _logsSb;
     private readonly V2TestDriver _driver;
     private bool _finished;
+    private readonly object _threadLock = new ();
 
     internal GuiTestContext (Func<Toplevel> topLevelBuilder, int width, int height, V2TestDriver driver)
     {
@@ -42,46 +43,49 @@ public class GuiTestContext : IDisposable
 
         var booting = new SemaphoreSlim (0, 1);
 
-        // Start the application in a background thread
-        _runTask = Task.Run (
-                             () =>
-                             {
-                                 try
-                                 {
-                                     ApplicationImpl.ChangeInstance (v2);
-
-                                     ILogger logger = LoggerFactory.Create (
-                                                                            builder =>
-                                                                                builder.SetMinimumLevel (LogLevel.Trace)
-                                                                                       .AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
-                                                                   .CreateLogger ("Test Logging");
-                                     Logging.Logger = logger;
-
-                                     v2.Init (null, GetDriverName ());
-
-                                     booting.Release ();
-
-                                     Toplevel t = topLevelBuilder ();
-                                     t.Closed += (s, e) => { _finished = true; };
-                                     Application.Run (t); // This will block, but it's on a background thread now
-
-                                     t.Dispose ();
-                                     Application.Shutdown ();
-                                 }
-                                 catch (OperationCanceledException)
-                                 { }
-                                 catch (Exception ex)
-                                 {
-                                     _ex = ex;
-                                 }
-                                 finally
+        lock (_threadLock)
+        {
+            // Start the application in a background thread
+            _runTask = Task.Run (
+                                 () =>
                                  {
-                                     ApplicationImpl.ChangeInstance (origApp);
-                                     Logging.Logger = origLogger;
-                                     _finished = true;
-                                 }
-                             },
-                             _cts.Token);
+                                     try
+                                     {
+                                         ApplicationImpl.ChangeInstance (v2);
+
+                                         ILogger logger = LoggerFactory.Create (
+                                                                                builder =>
+                                                                                    builder.SetMinimumLevel (LogLevel.Trace)
+                                                                                           .AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
+                                                                       .CreateLogger ("Test Logging");
+                                         Logging.Logger = logger;
+
+                                         v2.Init (null, GetDriverName ());
+
+                                         booting.Release ();
+
+                                         Toplevel t = topLevelBuilder ();
+                                         t.Closed += (s, e) => { _finished = true; };
+                                         Application.Run (t); // This will block, but it's on a background thread now
+
+                                         t.Dispose ();
+                                         Application.Shutdown ();
+                                     }
+                                     catch (OperationCanceledException)
+                                     { }
+                                     catch (Exception ex)
+                                     {
+                                         _ex = ex;
+                                     }
+                                     finally
+                                     {
+                                         ApplicationImpl.ChangeInstance (origApp);
+                                         Logging.Logger = origLogger;
+                                         _finished = true;
+                                     }
+                                 },
+                                 _cts.Token);
+        }
 
         // Wait for booting to complete with a timeout to avoid hangs
         if (!booting.WaitAsync (TimeSpan.FromSeconds (5)).Result)