-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix flaky WhenSpecificDataSourceIsIgnoredViaIgnoreMessageProperty
#4553
Merged
+74
−112
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b05ffaa
Fix flaky `WhenSpecificDataSourceIsIgnoredViaIgnoreMessageProperty`
Youssef1313 47fb3a3
Fix batching to not let other Console.Write calls interfere with it
Youssef1313 69fb609
Fix Console.Out may be changing
Youssef1313 4c57aad
doc
Youssef1313 214c895
Doc
Youssef1313 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ internal sealed class NonAnsiTerminal : ITerminal | |
{ | ||
private readonly IConsole _console; | ||
private readonly ConsoleColor _defaultForegroundColor; | ||
private readonly StringBuilder _stringBuilder = new(); | ||
private bool _isBatching; | ||
|
||
public NonAnsiTerminal(IConsole console) | ||
|
@@ -28,52 +27,16 @@ public NonAnsiTerminal(IConsole console) | |
public int Height => _console.IsOutputRedirected ? int.MaxValue : _console.BufferHeight; | ||
|
||
public void Append(char value) | ||
{ | ||
if (_isBatching) | ||
{ | ||
_stringBuilder.Append(value); | ||
} | ||
else | ||
{ | ||
_console.Write(value); | ||
} | ||
} | ||
=> _console.Write(value); | ||
|
||
public void Append(string value) | ||
{ | ||
if (_isBatching) | ||
{ | ||
_stringBuilder.Append(value); | ||
} | ||
else | ||
{ | ||
_console.Write(value); | ||
} | ||
} | ||
=> _console.Write(value); | ||
|
||
public void AppendLine() | ||
{ | ||
if (_isBatching) | ||
{ | ||
_stringBuilder.AppendLine(); | ||
} | ||
else | ||
{ | ||
_console.WriteLine(); | ||
} | ||
} | ||
=> _console.WriteLine(); | ||
|
||
public void AppendLine(string value) | ||
{ | ||
if (_isBatching) | ||
{ | ||
_stringBuilder.AppendLine(value); | ||
} | ||
else | ||
{ | ||
_console.WriteLine(value); | ||
} | ||
} | ||
=> _console.WriteLine(value); | ||
|
||
public void AppendLink(string path, int? lineNumber) | ||
{ | ||
|
@@ -85,26 +48,10 @@ public void AppendLink(string path, int? lineNumber) | |
} | ||
|
||
public void SetColor(TerminalColor color) | ||
{ | ||
if (_isBatching) | ||
{ | ||
_console.Write(_stringBuilder.ToString()); | ||
_stringBuilder.Clear(); | ||
} | ||
|
||
_console.SetForegroundColor(ToConsoleColor(color)); | ||
} | ||
=> _console.SetForegroundColor(ToConsoleColor(color)); | ||
|
||
public void ResetColor() | ||
{ | ||
if (_isBatching) | ||
{ | ||
_console.Write(_stringBuilder.ToString()); | ||
_stringBuilder.Clear(); | ||
} | ||
|
||
_console.SetForegroundColor(_defaultForegroundColor); | ||
} | ||
=> _console.SetForegroundColor(_defaultForegroundColor); | ||
|
||
public void ShowCursor() | ||
{ | ||
|
@@ -116,20 +63,48 @@ public void HideCursor() | |
// nop | ||
} | ||
|
||
// TODO: Refactor NonAnsiTerminal and AnsiTerminal such that we don't need StartUpdate/StopUpdate. | ||
// It's much better if we use lock C# keyword instead of manually calling Monitor.Enter/Exit | ||
// Using lock also ensures we don't accidentally have `await`s in between that could cause Exit to be on a different thread. | ||
public void StartUpdate() | ||
{ | ||
if (_isBatching) | ||
{ | ||
throw new InvalidOperationException(PlatformResources.ConsoleIsAlreadyInBatchingMode); | ||
} | ||
|
||
_stringBuilder.Clear(); | ||
bool lockTaken = false; | ||
// SystemConsole.ConsoleOut is set only once in static ctor. | ||
// So we are sure we will be doing Monitor.Exit on the same instance. | ||
// Note that we need to lock on System.Out for batching to work correctly. | ||
// Consider the following scenario: | ||
// 1. We call StartUpdate | ||
// 2. We call a Write("A") | ||
// 3. User calls Console.Write("B") from another thread. | ||
// 4. We call a Write("C"). | ||
// 5. We call StopUpdate. | ||
// The expectation is that we see either ACB, or BAC, but not ABC. | ||
// Basically, when doing batching, we want to ensure that everything we write is | ||
// written continuously, without anything in-between. | ||
// One option (and we used to do it), is that we append to a StringBuilder while batching | ||
// Then at StopUpdate, we write the whole string at once. | ||
// This works to some extent, but we cannot get it to work when SetColor kicks in. | ||
// Console methods will internally lock on Console.Out, so we are locking on the same thing. | ||
// This locking is the easiest way to get coloring to work correctly while preventing | ||
// interleaving with user's calls to Console.Write methods. | ||
Monitor.Enter(SystemConsole.ConsoleOut, ref lockTaken); | ||
if (!lockTaken) | ||
{ | ||
// Can this happen? :/ | ||
throw new InvalidOperationException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rarely but I think yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am fine with throwing the |
||
} | ||
|
||
_isBatching = true; | ||
} | ||
|
||
public void StopUpdate() | ||
{ | ||
_console.Write(_stringBuilder.ToString()); | ||
Monitor.Exit(SystemConsole.ConsoleOut); | ||
_isBatching = false; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I need to doc this more, and mention that it's to avoid interleaving with user's calls to
Console.WriteLine
, which will internally lock on Console.Out.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge this as-is for now to unblock main CI that is currently blocked so we unblock DotNet sdk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already extended the comment so this comment wasn't valid anymore actually :)