Skip to content

Commit 852c6b6

Browse files
mdh1418Copilot
andauthored
[Test] Fix collect command test race condition with status printer (dotnet#5787)
The CollectCommandFunctionalTests substitute a MemoryStream for the output file stream, so the trace file is never created on disk. The interactive status-printing path is non-deterministic in these tests because the MemoryStream-backed session completes near-instantly, and whether the status line fires before the loop exits depends on thread scheduling. When it does fire, FileInfo.Length throws because the file does not exist. Set IsOutputRedirected=true on MockConsole to disable the status path. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 455cd12 commit 852c6b6

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ internal async Task<int> Collect(CancellationToken ct, CommandLineConfiguration
385385
}
386386
}
387387

388-
FileInfo fileInfo = new(output.FullName);
389388
bool wroteStatus = false;
390389
Action printStatus = () => {
391390
if (printStatusOverTime && rewriter.IsRewriteConsoleLineSupported)
@@ -399,8 +398,7 @@ internal async Task<int> Collect(CancellationToken ct, CommandLineConfiguration
399398
// First time writing status, so don't rewrite console yet.
400399
wroteStatus = true;
401400
}
402-
fileInfo.Refresh();
403-
ConsoleWriteLine($"[{stopwatch.Elapsed:dd\\:hh\\:mm\\:ss}]\tRecording trace {GetSize(fileInfo.Length)}");
401+
ConsoleWriteLine($"[{stopwatch.Elapsed:dd\\:hh\\:mm\\:ss}]\tRecording trace {GetSize(eventStream.Length)}");
404402
ConsoleWriteLine("Press <Enter> or <Ctrl+C> to exit...");
405403
}
406404

src/tests/dotnet-trace/CollectCommandFunctionalTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public async Task CollectCommand_InvalidProcessSpecifierConfigurations(CollectAr
9191

9292
private static async Task<int> RunAsync(CollectArgs config, MockConsole console, bool hasChildProcess = false)
9393
{
94+
// Disable the interactive status-printing path. It is currently non-deterministic in these tests because the
95+
// MemoryStream-backed session completes near-instantly, and whether the status line fires before the loop exits
96+
// depends on thread scheduling. The MemoryStream substitution also means no trace file exists on disk, so
97+
// FileInfo.Length in the status printer would throw.
98+
console.IsOutputRedirected = true;
99+
94100
var handler = new CollectCommandHandler(console);
95101
handler.StartTraceSessionAsync = (client, cfg, ct) => Task.FromResult<CollectCommandHandler.ICollectSession>(new TestCollectSession());
96102
handler.ResumeRuntimeAsync = (client, ct) => Task.CompletedTask;

0 commit comments

Comments
 (0)