Skip to content
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 flakiness on test Log_WhenAsyncFlush_StreamWriterIsCalledOnlyWhenLogLevelAllowsIt #4394

Merged
merged 3 commits into from
Dec 19, 2024
Merged
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 @@ -197,30 +197,28 @@ public async Task Log_WhenSyncFlush_StreamWriterIsCalledOnlyWhenLogLevelAllowsIt

[TestMethod]
[DynamicData(nameof(LogTestHelpers.GetLogLevelCombinations), typeof(LogTestHelpers), DynamicDataSourceType.Method)]
public async Task Log_WhenAsyncFlush_StreamWriterIsCalledOnlyWhenLogLevelAllowsIt(LogLevel defaultLogLevel, LogLevel currentLogLevel)
public void Log_WhenAsyncFlush_StreamWriterIsCalledOnlyWhenLogLevelAllowsIt(LogLevel defaultLogLevel, LogLevel currentLogLevel)
{
_mockFileSystem.Setup(x => x.Exists(It.IsAny<string>())).Returns(false);
_mockFileStreamFactory
.Setup(x => x.Create(It.IsAny<string>(), FileMode.CreateNew, FileAccess.Write, FileShare.Read))
.Returns(_mockStream.Object);

using FileLogger fileLogger = new(
// Ensures that the async flush is completed before the file is read
using (FileLogger fileLogger = new(
new(LogFolder, LogPrefix, fileName: FileName, syncFlush: false),
defaultLogLevel,
_mockClock.Object,
new SystemTask(),
_mockConsole.Object,
_mockFileSystem.Object,
_mockFileStreamFactory.Object);
fileLogger.Log(currentLogLevel, Message, null, Formatter, Category);
_mockFileStreamFactory.Object))
{
fileLogger.Log(currentLogLevel, Message, null, Formatter, Category);
}

if (LogTestHelpers.IsLogEnabled(defaultLogLevel, currentLogLevel))
{
if (_memoryStream.Length == 0)
{
await Task.Delay(1000);
}

Assert.AreEqual($"0001-01-01T00:00:00.0000000+00:00 Test {currentLogLevel.ToString().ToUpperInvariant()} Message{Environment.NewLine}", Encoding.Default.GetString(_memoryStream.ToArray()));
}
else
Expand Down
Loading