Skip to content

Commit be6db6f

Browse files
committed
#2931: Tighten up types to prevent accidentically calling AddOrGet with a ConcurrentDictionary (v2)
1 parent f466d81 commit be6db6f

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/common/DictionaryExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
static class DictionaryExtensions
66
{
7-
public static void Add<TKey, TValue>(this IDictionary<TKey, List<TValue>> dictionary, TKey key, TValue value)
7+
public static void Add<TKey, TValue>(this Dictionary<TKey, List<TValue>> dictionary, TKey key, TValue value)
88
{
99
dictionary.AddOrGet(key).Add(value);
1010
}
1111

12-
public static TValue AddOrGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
12+
public static TValue AddOrGet<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
1313
where TValue : new()
1414
{
1515
return dictionary.AddOrGet(key, () => new TValue());
1616
}
1717

18-
public static TValue AddOrGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> newValue)
18+
public static TValue AddOrGet<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TValue> newValue)
1919
{
2020
TValue result;
2121

@@ -28,7 +28,11 @@ public static TValue AddOrGet<TKey, TValue>(this IDictionary<TKey, TValue> dicti
2828
return result;
2929
}
3030

31+
#if NET35
3132
public static bool Contains<TKey, TValue>(this IDictionary<TKey, List<TValue>> dictionary, TKey key, TValue value, IEqualityComparer<TValue> valueComparer)
33+
#else
34+
public static bool Contains<TKey, TValue>(this IReadOnlyDictionary<TKey, List<TValue>> dictionary, TKey key, TValue value, IEqualityComparer<TValue> valueComparer)
35+
#endif
3236
{
3337
List<TValue> values;
3438

src/xunit.execution/Sdk/ExtensibilityPointFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void Dispose()
7373
/// <returns>The instance of the type.</returns>
7474
public static TInterface Get<TInterface>(IMessageSink diagnosticMessageSink, Type type, object[] ctorArgs = null)
7575
{
76-
return (TInterface)instances.AddOrGet(Tuple.Create(type, diagnosticMessageSink), () => CreateInstance(diagnosticMessageSink, type, ctorArgs));
76+
return (TInterface)instances.GetOrAdd(Tuple.Create(type, diagnosticMessageSink), _ => CreateInstance(diagnosticMessageSink, type, ctorArgs));
7777
}
7878

7979
/// <summary>

src/xunit.execution/Sdk/Frameworks/XunitTestCase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ protected override void Initialize()
149149
}
150150

151151
static IEnumerable<IAttributeInfo> GetCachedTraitAttributes(IAssemblyInfo assembly)
152-
=> assemblyTraitAttributeCache.AddOrGet(assembly.Name, () => assembly.GetCustomAttributes(typeof(ITraitAttribute)));
152+
=> assemblyTraitAttributeCache.GetOrAdd(assembly.Name, _ => assembly.GetCustomAttributes(typeof(ITraitAttribute)));
153153

154154
static IEnumerable<IAttributeInfo> GetCachedTraitAttributes(ITypeInfo type)
155-
=> typeTraitAttributeCache.AddOrGet(type.Name, () => type.GetCustomAttributes(typeof(ITraitAttribute)));
155+
=> typeTraitAttributeCache.GetOrAdd(type.Name, _ => type.GetCustomAttributes(typeof(ITraitAttribute)));
156156

157157
static IEnumerable<IAttributeInfo> GetTraitAttributesData(ITestMethod testMethod)
158158
{

0 commit comments

Comments
 (0)