Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making the cache dictionary thread safe #153

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Moq.AutoMock/AutoMocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public AutoMocker(MockBehavior mockBehavior, DefaultValue defaultValue, DefaultV
/// The keys are the types used when resolving services.
/// </summary>
public IReadOnlyDictionary<Type, object?> ResolvedObjects
//NB: NonBlocking.ConcurrentDictionary GetEnumerator method returns a snapshot enumerator which is thread-safe
=> TypeMap?.ToDictionary(kvp => kvp.Key, kvp =>
{
return kvp.Value switch
Expand All @@ -123,7 +124,7 @@ public AutoMocker(MockBehavior mockBehavior, DefaultValue defaultValue, DefaultV
};
}) ?? new Dictionary<Type, object?>();

private Dictionary<Type, IInstance>? TypeMap
private NonBlocking.ConcurrentDictionary<Type, IInstance>? TypeMap
=> Resolvers.OfType<CacheResolver>().FirstOrDefault()?.TypeMap;

private bool TryResolve(Type serviceType,
Expand Down Expand Up @@ -1054,7 +1055,7 @@ internal void CacheInstances(IEnumerable<(Type, IInstance)> instances)
});
}

private void WithTypeMap(Action<Dictionary<Type, IInstance>> onTypeMap)
private void WithTypeMap(Action<NonBlocking.ConcurrentDictionary<Type, IInstance>> onTypeMap)
{
if (TypeMap is { } typeMap)
{
Expand Down
1 change: 1 addition & 0 deletions Moq.AutoMock/Moq.AutoMock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NonBlocking" Version="2.1.0" />
<ProjectReference Include="../Generators/Generators.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
Expand Down
36 changes: 18 additions & 18 deletions Moq.AutoMock/Resolvers/CacheResolver.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
namespace Moq.AutoMock.Resolvers;

/// <summary>
/// Provides the cache used by AutoMocker.
/// </summary>
public class CacheResolver : IMockResolver
{
internal Dictionary<Type, IInstance> TypeMap { get; } = new();

/// <inheritdoc />
public void Resolve(MockResolutionContext context)
{
if (TypeMap.TryGetValue(context.RequestType, out IInstance instance))
{
context.Value = instance;
}
}
}
namespace Moq.AutoMock.Resolvers;
/// <summary>
/// Provides the cache used by AutoMocker.
/// </summary>
public class CacheResolver : IMockResolver
{
internal NonBlocking.ConcurrentDictionary<Type, IInstance> TypeMap { get; } = new();
/// <inheritdoc />
public void Resolve(MockResolutionContext context)
{
if (TypeMap.TryGetValue(context.RequestType, out IInstance instance))
{
context.Value = instance;
}
}
}