Skip to content

Commit

Permalink
Fix flakiness on test Log_WhenAsyncFlush_StreamWriterIsCalledOnlyWhen…
Browse files Browse the repository at this point in the history
…LogLevelAllowsIt (#4394)
  • Loading branch information
Evangelink authored Dec 19, 2024
1 parent 5bdcacd commit ddf39b4
Showing 1 changed file with 7 additions and 9 deletions.
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

0 comments on commit ddf39b4

Please sign in to comment.