Skip to content

Commit

Permalink
StopUpdate in Finally Block (#4147)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst authored Nov 25, 2024
1 parent e3900ac commit 7d0cd29
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;

using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Resources;

namespace Microsoft.Testing.Platform.OutputDevice.Terminal;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ internal sealed partial class TestProgressStateAwareTerminal : IDisposable
/// <summary>
/// Protects access to state shared between the logger callbacks and the rendering thread.
/// </summary>
#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<bool?> _showProgress;
private readonly bool _writeProgressImmediatelyAfterOutput;
private readonly int _updateEvery;
private TestProgressState?[] _progressItems = Array.Empty<TestProgressState>();
private TestProgressState?[] _progressItems = [];
private bool? _showProgressCached;

/// <summary>
Expand Down Expand Up @@ -122,24 +126,35 @@ internal void WriteToTerminal(Action<ITerminal> 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();
}
}
}
}
Expand Down

0 comments on commit 7d0cd29

Please sign in to comment.