From e01777bdcc9cb235e9aaacbbcb3e2b8ddb931fd6 Mon Sep 17 00:00:00 2001
From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com>
Date: Mon, 25 Nov 2024 14:31:47 +0000
Subject: [PATCH 1/2] StopUpdate in Finally Block
---
.../TestProgressStateAwareTerminal.cs | 37 +++++++++++++------
1 file changed, 26 insertions(+), 11 deletions(-)
diff --git a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs
index f1b1f32c62f..c462d8e67e8 100644
--- a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs
+++ b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs
@@ -16,13 +16,17 @@ internal sealed partial class TestProgressStateAwareTerminal : IDisposable
///
/// Protects access to state shared between the logger callbacks and the rendering thread.
///
+#if NET9_0_OR_GREATER
+ private readonly System.Threading.Lock _lock = new();
+#else
private readonly object _lock = new();
+#endif
private readonly ITerminal _terminal;
private readonly Func _showProgress;
private readonly bool _writeProgressImmediatelyAfterOutput;
private readonly int _updateEvery;
- private TestProgressState?[] _progressItems = Array.Empty();
+ private TestProgressState?[] _progressItems = [];
private bool? _showProgressCached;
///
@@ -122,24 +126,35 @@ internal void WriteToTerminal(Action write)
{
lock (_lock)
{
- _terminal.StartUpdate();
- _terminal.EraseProgress();
- write(_terminal);
- if (_writeProgressImmediatelyAfterOutput)
+ try
{
- _terminal.RenderProgress(_progressItems);
+ _terminal.StartUpdate();
+ _terminal.EraseProgress();
+ write(_terminal);
+ if (_writeProgressImmediatelyAfterOutput)
+ {
+ _terminal.RenderProgress(_progressItems);
+ }
+ }
+ finally
+ {
+ _terminal.StopUpdate();
}
-
- _terminal.StopUpdate();
}
}
else
{
lock (_lock)
{
- _terminal.StartUpdate();
- write(_terminal);
- _terminal.StopUpdate();
+ try
+ {
+ _terminal.StartUpdate();
+ write(_terminal);
+ }
+ finally
+ {
+ _terminal.StopUpdate();
+ }
}
}
}
From 34224fc4ab8053f7031bb4c970cc510f23d74966 Mon Sep 17 00:00:00 2001
From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com>
Date: Mon, 25 Nov 2024 14:45:58 +0000
Subject: [PATCH 2/2] Use language resource
---
.../OutputDevice/Terminal/NonAnsiTerminal.cs | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs
index e0af85e4fc4..8c1f5457201 100644
--- a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs
+++ b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs
@@ -5,6 +5,7 @@
using System.Text;
using Microsoft.Testing.Platform.Helpers;
+using Microsoft.Testing.Platform.Resources;
namespace Microsoft.Testing.Platform.OutputDevice.Terminal;
@@ -122,7 +123,7 @@ public void StartUpdate()
{
if (_isBatching)
{
- throw new InvalidOperationException("Console is already in batching mode.");
+ throw new InvalidOperationException(PlatformResources.ConsoleIsAlreadyInBatchingMode);
}
_stringBuilder.Clear();