Skip to content

Commit 4e36e7a

Browse files
authored
Merge branch 'v2_develop' into v2_4050-Activate
2 parents dc3a67e + 497ddb3 commit 4e36e7a

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

Tests/TerminalGuiFluentTesting/GuiTestContext.cs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class GuiTestContext : IDisposable
2121
private readonly StringBuilder _logsSb;
2222
private readonly V2TestDriver _driver;
2323
private bool _finished;
24+
private readonly object _threadLock = new ();
2425

2526
internal GuiTestContext (Func<Toplevel> topLevelBuilder, int width, int height, V2TestDriver driver)
2627
{
@@ -42,46 +43,49 @@ internal GuiTestContext (Func<Toplevel> topLevelBuilder, int width, int height,
4243

4344
var booting = new SemaphoreSlim (0, 1);
4445

45-
// Start the application in a background thread
46-
_runTask = Task.Run (
47-
() =>
48-
{
49-
try
50-
{
51-
ApplicationImpl.ChangeInstance (v2);
52-
53-
ILogger logger = LoggerFactory.Create (
54-
builder =>
55-
builder.SetMinimumLevel (LogLevel.Trace)
56-
.AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
57-
.CreateLogger ("Test Logging");
58-
Logging.Logger = logger;
59-
60-
v2.Init (null, GetDriverName ());
61-
62-
booting.Release ();
63-
64-
Toplevel t = topLevelBuilder ();
65-
t.Closed += (s, e) => { _finished = true; };
66-
Application.Run (t); // This will block, but it's on a background thread now
67-
68-
t.Dispose ();
69-
Application.Shutdown ();
70-
}
71-
catch (OperationCanceledException)
72-
{ }
73-
catch (Exception ex)
74-
{
75-
_ex = ex;
76-
}
77-
finally
46+
lock (_threadLock)
47+
{
48+
// Start the application in a background thread
49+
_runTask = Task.Run (
50+
() =>
7851
{
79-
ApplicationImpl.ChangeInstance (origApp);
80-
Logging.Logger = origLogger;
81-
_finished = true;
82-
}
83-
},
84-
_cts.Token);
52+
try
53+
{
54+
ApplicationImpl.ChangeInstance (v2);
55+
56+
ILogger logger = LoggerFactory.Create (
57+
builder =>
58+
builder.SetMinimumLevel (LogLevel.Trace)
59+
.AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
60+
.CreateLogger ("Test Logging");
61+
Logging.Logger = logger;
62+
63+
v2.Init (null, GetDriverName ());
64+
65+
booting.Release ();
66+
67+
Toplevel t = topLevelBuilder ();
68+
t.Closed += (s, e) => { _finished = true; };
69+
Application.Run (t); // This will block, but it's on a background thread now
70+
71+
t.Dispose ();
72+
Application.Shutdown ();
73+
}
74+
catch (OperationCanceledException)
75+
{ }
76+
catch (Exception ex)
77+
{
78+
_ex = ex;
79+
}
80+
finally
81+
{
82+
ApplicationImpl.ChangeInstance (origApp);
83+
Logging.Logger = origLogger;
84+
_finished = true;
85+
}
86+
},
87+
_cts.Token);
88+
}
8589

8690
// Wait for booting to complete with a timeout to avoid hangs
8791
if (!booting.WaitAsync (TimeSpan.FromSeconds (5)).Result)

0 commit comments

Comments
 (0)