From 45dec0dca4e5f982bcb1ffd7b289f9ffe3a916b0 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 7 Jun 2017 12:37:04 -0700 Subject: [PATCH] Fix #1407 null refs in VCTH tests Dictionary isn't thread safe YO. --- .../TestCompilation.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs b/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs index 8628904ddb..1943118015 100644 --- a/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs +++ b/test/Microsoft.AspNetCore.Razor.Test.Common/TestCompilation.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -14,8 +15,8 @@ namespace Microsoft.CodeAnalysis { public static class TestCompilation { - private static Dictionary> _assemblyMetadataReferences = - new Dictionary>(); + private static readonly ConcurrentDictionary> _referenceCache = + new ConcurrentDictionary>(); public static IEnumerable GetMetadataReferences(Assembly assembly) { @@ -40,10 +41,10 @@ namespace Microsoft.CodeAnalysis syntaxTrees = new[] { syntaxTree }; } - if (!_assemblyMetadataReferences.TryGetValue(assembly, out IEnumerable metadataReferences)) + if (!_referenceCache.TryGetValue(assembly, out IEnumerable metadataReferences)) { metadataReferences = GetMetadataReferences(assembly); - _assemblyMetadataReferences[assembly] = metadataReferences; + _referenceCache.TryAdd(assembly, metadataReferences); } var compilation = CSharpCompilation.Create(AssemblyName, syntaxTrees, metadataReferences);