Skip to content

Commit

Permalink
#2931: Tighten up types to prevent accidentically calling AddOrGet wi…
Browse files Browse the repository at this point in the history
…th a ConcurrentDictionary (v2)
  • Loading branch information
bradwilson committed May 17, 2024
1 parent f466d81 commit be6db6f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/common/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

static class DictionaryExtensions
{
public static void Add<TKey, TValue>(this IDictionary<TKey, List<TValue>> dictionary, TKey key, TValue value)
public static void Add<TKey, TValue>(this Dictionary<TKey, List<TValue>> dictionary, TKey key, TValue value)
{
dictionary.AddOrGet(key).Add(value);
}

public static TValue AddOrGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
public static TValue AddOrGet<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
where TValue : new()
{
return dictionary.AddOrGet(key, () => new TValue());
}

public static TValue AddOrGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> newValue)
public static TValue AddOrGet<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TValue> newValue)
{
TValue result;

Expand All @@ -28,7 +28,11 @@ static class DictionaryExtensions
return result;
}

#if NET35
public static bool Contains<TKey, TValue>(this IDictionary<TKey, List<TValue>> dictionary, TKey key, TValue value, IEqualityComparer<TValue> valueComparer)
#else
public static bool Contains<TKey, TValue>(this IReadOnlyDictionary<TKey, List<TValue>> dictionary, TKey key, TValue value, IEqualityComparer<TValue> valueComparer)
#endif
{
List<TValue> values;

Expand Down
2 changes: 1 addition & 1 deletion src/xunit.execution/Sdk/ExtensibilityPointFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void Dispose()
/// <returns>The instance of the type.</returns>
public static TInterface Get<TInterface>(IMessageSink diagnosticMessageSink, Type type, object[] ctorArgs = null)
{
return (TInterface)instances.AddOrGet(Tuple.Create(type, diagnosticMessageSink), () => CreateInstance(diagnosticMessageSink, type, ctorArgs));
return (TInterface)instances.GetOrAdd(Tuple.Create(type, diagnosticMessageSink), _ => CreateInstance(diagnosticMessageSink, type, ctorArgs));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/xunit.execution/Sdk/Frameworks/XunitTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ protected override void Initialize()
}

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

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

static IEnumerable<IAttributeInfo> GetTraitAttributesData(ITestMethod testMethod)
{
Expand Down

0 comments on commit be6db6f

Please sign in to comment.