Skip to content

Commit 83cdd05

Browse files
committed
Fix test flake for FunctionLogging_Succeeds
The call to GetAllLogMessages sometimes ends up enumerating the loggers while they are modified. This should resolve.
1 parent 395b0c9 commit 83cdd05

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

test/WebJobs.Script.Tests.Shared/TestLoggerProvider.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
67
using System.Linq;
78
using Microsoft.Azure.WebJobs.Script.Tests;
@@ -18,19 +19,13 @@ public TestLoggerProvider(Func<string, LogLevel, bool> filter = null)
1819
_filter = filter;
1920
}
2021

21-
private Dictionary<string, TestLogger> LoggerCache { get; } = new Dictionary<string, TestLogger>();
22+
private ConcurrentDictionary<string, TestLogger> LoggerCache { get; } = new ConcurrentDictionary<string, TestLogger>();
2223

2324
public IEnumerable<TestLogger> CreatedLoggers => LoggerCache.Values;
2425

2526
public ILogger CreateLogger(string categoryName)
2627
{
27-
if (!LoggerCache.TryGetValue(categoryName, out TestLogger logger))
28-
{
29-
logger = new TestLogger(categoryName, _filter);
30-
LoggerCache.Add(categoryName, logger);
31-
}
32-
33-
return logger;
28+
return LoggerCache.GetOrAdd(categoryName, (key) => new TestLogger(key, _filter));
3429
}
3530

3631
public IList<LogMessage> GetAllLogMessages() => CreatedLoggers.SelectMany(l => l.GetLogMessages()).OrderBy(p => p.Timestamp).ToList();

0 commit comments

Comments
 (0)