Skip to content

Avoid keeping trx stream open in unit tests #4642

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

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testAp
_isCopyingFileAllowed = isCopyingFileAllowed;
}

public async Task<string> GenerateReportAsync(string testHostCrashInfo = "", bool isTestHostCrashed = false, bool keepReportFileStreamOpen = false)
public async Task<string> GenerateReportAsync(string testHostCrashInfo = "", bool isTestHostCrashed = false)
=> await RetryWhenIOExceptionAsync(async () =>
{
string testAppModule = _testApplicationModuleInfo.GetCurrentTestApplicationFullPath();
Expand Down Expand Up @@ -189,23 +189,12 @@ public async Task<string> GenerateReportAsync(string testHostCrashInfo = "", boo

string outputDirectory = _configuration.GetTestResultDirectory(); // add var for this
string finalFileName = Path.Combine(outputDirectory, trxFileName);
Stream stream = _fileSystem.NewFileStream(finalFileName, FileMode.CreateNew).Stream;
try
{
await document.SaveAsync(stream, SaveOptions.None, _cancellationToken);
return finalFileName;
}
finally
{
if (!keepReportFileStreamOpen)
{
#if NET
await stream.DisposeAsync();
#else
stream.Dispose();
#endif
}
}

// Note that we need to dispose the IFileStream, not the inner stream.
// IFileStream implementations will be responsible to dispose their inner stream.
using IFileStream stream = _fileSystem.NewFileStream(finalFileName, FileMode.CreateNew);
await document.SaveAsync(stream.Stream, SaveOptions.None, _cancellationToken);
return finalFileName;
});

private async Task<string> RetryWhenIOExceptionAsync(Func<Task<string>> func)
Expand Down
105 changes: 65 additions & 40 deletions test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ public async Task TrxReportEngine_GenerateReportAsyncWithNullAdapterSupportTrxCa
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string trxContent = xml.ToString();
Assert.IsFalse(trxContent.Contains(@"className="));
Expand All @@ -58,11 +59,12 @@ public async Task TrxReportEngine_GenerateReportAsyncWithNotExecutedTests_TrxExe
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream, notExecutedTestsCount: 1);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string trxContent = xml.ToString();
Assert.IsTrue(trxContent.Contains(@"notExecuted=""1"""));
Expand All @@ -77,11 +79,12 @@ public async Task TrxReportEngine_GenerateReportAsyncWithTimeoutTests_TrxTimeout
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream, timeoutTestsCount: 1);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string trxContent = xml.ToString();
Assert.IsTrue(trxContent.Contains(@"timeout=""1"""));
Expand All @@ -98,11 +101,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithArgumentTrxReportFileN
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
Assert.IsTrue(fileName.Equals("argumentTrxReportFileName", StringComparison.OrdinalIgnoreCase));
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
}

Expand All @@ -117,11 +121,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithInvalidArgumentValueFo
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
Assert.IsTrue(fileName.Equals("_NUL", StringComparison.OrdinalIgnoreCase));
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
}

Expand All @@ -134,11 +139,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithTestHostCrash_ResultSu
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(isTestHostCrashed: true, keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync(isTestHostCrashed: true);

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Failed");
}

Expand All @@ -151,11 +157,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithTestSkipped_ResultSumm
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(0, 0, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
}

Expand All @@ -170,11 +177,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithTestFailed_WithStandar
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(0, 1, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Failed");

XElement? testRun = xml.Root;
Expand Down Expand Up @@ -203,11 +211,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithTestFailed_WithoutStan
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(0, 1, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Failed");
string trxContent = xml.ToString();
string trxContentsPattern = @"
Expand All @@ -231,11 +240,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithTestFailed_WithoutStan
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(0, 1, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Failed");
string trxContent = xml.ToString();
string trxContentsPattern = @"
Expand All @@ -262,11 +272,12 @@ public async Task TrxReportEngine_GenerateReportAsync_PassedTestWithTestCategory
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(1, 0, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string trxContent = xml.ToString();
string trxContentsPattern = @"
Expand All @@ -289,11 +300,12 @@ public async Task TrxReportEngine_GenerateReportAsync_FailedTestWithTestCategory
TrxReportEngine trxReportEngine = GenerateTrxReportEngine(0, 1, propertyBag, memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Failed");
string trxContent = xml.ToString();
string trxContentsPattern = @"
Expand All @@ -317,11 +329,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithAdapterSupportTrxCapab
propertyBag, memoryStream, true);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string trxContent = xml.ToString();
Assert.IsTrue(trxContent.Contains(@"className=""TrxFullyQualifiedTypeName"), trxContent);
Expand All @@ -337,11 +350,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithArtifactsByTestNode_Tr
new(new PassedTestNodeStateProperty()), memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string trxContent = xml.ToString();
string trxContentsPattern = @"
Expand All @@ -366,11 +380,12 @@ public async Task TrxReportEngine_GenerateReportAsync_WithArtifactsByExtension_T
new(new PassedTestNodeStateProperty()), memoryStream);

// Act
string fileName = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
string fileName = await trxReportEngine.GenerateReportAsync();

// Assert
AssertExpectedTrxFileName(fileName);
XDocument xml = GetTrxContent(memoryStream);
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string trxContent = xml.ToString();
string trxContentsPattern = @"
Expand Down Expand Up @@ -413,19 +428,12 @@ public async Task TrxReportEngine_GenerateReportAsync_FileAlreadyExists_WillRetr
isCopyingFileAllowed: false);

// Act
_ = await trxReportEngine.GenerateReportAsync(keepReportFileStreamOpen: true);
_ = await trxReportEngine.GenerateReportAsync();

// Assert
Assert.AreEqual(4, retryCount);
}

private static XDocument GetTrxContent(MemoryFileStream memoryStream)
{
Assert.IsNotNull(memoryStream);
_ = memoryStream.Stream.Seek(0, SeekOrigin.Begin);
return XDocument.Load(memoryStream.Stream);
}

private static void AssertTrxOutcome(XDocument xml, string expectedOutcome)
{
Assert.IsNotNull(xml);
Expand Down Expand Up @@ -473,16 +481,33 @@ private sealed class MemoryFileStream : IFileStream

public MemoryStream Stream { get; }

public XDocument? TrxContent { get; private set; }

Stream IFileStream.Stream => Stream;

string IFileStream.Name => string.Empty;

private void SetTrxContent()
{
if (TrxContent is null)
{
_ = Stream.Seek(0, SeekOrigin.Begin);
TrxContent = XDocument.Load(Stream);
}
}

void IDisposable.Dispose()
=> Stream.Dispose();
{
SetTrxContent();
Stream.Dispose();
}

#if NETCOREAPP
ValueTask IAsyncDisposable.DisposeAsync()
=> Stream.DisposeAsync();
{
SetTrxContent();
return Stream.DisposeAsync();
}
#endif
}
}
Loading