Skip to content

Commit 33321b7

Browse files
committed
Optimized CachingTestPlugin service replacement (#247)
1 parent 489d8d7 commit 33321b7

File tree

2 files changed

+35
-52
lines changed

2 files changed

+35
-52
lines changed
Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,52 @@
11
namespace MyTested.AspNetCore.Mvc.Plugins
22
{
33
using System;
4-
using System.Linq;
54
using Internal;
65
using Internal.Contracts;
76
using Microsoft.Extensions.Caching.Memory;
87
using Microsoft.Extensions.DependencyInjection;
98
using Internal.Services;
109
using Microsoft.Extensions.Caching.Distributed;
11-
using Utilities.Validators;
1210

1311
public class CachingTestPlugin : IServiceRegistrationPlugin
1412
{
15-
private readonly Type defaultCachingServiceType = typeof(IMemoryCache);
16-
private readonly Type defaultCachingImplementationType = typeof(MemoryCache);
13+
private readonly Type defaultMemoryCacheServiceType = typeof(IMemoryCache);
14+
private readonly Type defaultMemoryCacheImplementationType = typeof(MemoryCache);
1715

18-
private readonly Type defaultDistributedCachingServiceType = typeof(IDistributedCache);
19-
private readonly Type defaultDistributedCachingImplementationType = typeof(MemoryDistributedCache);
16+
private readonly Type defaultDistributedCacheServiceType = typeof(IDistributedCache);
17+
private readonly Type defaultDistributedCacheImplementationType = typeof(MemoryDistributedCache);
18+
19+
private bool replaceMemoryCache = false;
20+
private bool replaceDistributedCache = false;
2021

2122
public Func<ServiceDescriptor, bool> ServiceSelectorPredicate
2223
=> serviceDescriptor =>
2324
{
24-
var isSupportedService =
25-
serviceDescriptor.ServiceType == this.defaultCachingServiceType ||
26-
serviceDescriptor.ServiceType == this.defaultDistributedCachingServiceType;
25+
this.replaceMemoryCache = this.replaceMemoryCache ||
26+
(serviceDescriptor.ServiceType == this.defaultMemoryCacheServiceType &&
27+
serviceDescriptor.ImplementationType == this.defaultMemoryCacheImplementationType);
2728

28-
var isSupportedImplementation =
29-
serviceDescriptor.ImplementationType == this.defaultCachingImplementationType ||
30-
serviceDescriptor.ImplementationType == this.defaultDistributedCachingImplementationType;
29+
this.replaceDistributedCache = this.replaceDistributedCache ||
30+
(serviceDescriptor.ServiceType == this.defaultDistributedCacheServiceType &&
31+
serviceDescriptor.ImplementationType == this.defaultDistributedCacheImplementationType);
3132

32-
return isSupportedService && isSupportedImplementation;
33+
return replaceMemoryCache || replaceDistributedCache;
3334
};
3435

3536
public Action<IServiceCollection> ServiceRegistrationDelegate
36-
{
37-
get
37+
=> serviceCollection =>
3838
{
39-
return serviceCollection =>
39+
if (this.replaceMemoryCache)
4040
{
41-
CommonValidator.CheckForNullReference(serviceCollection);
42-
43-
if (this.ShouldReplace(serviceCollection, this.defaultCachingServiceType, this.defaultCachingImplementationType))
44-
{
45-
serviceCollection.ReplaceMemoryCache();
46-
TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IMemoryCache>()?.Dispose();
47-
}
48-
49-
if (this.ShouldReplace(serviceCollection, this.defaultDistributedCachingServiceType, this.defaultDistributedCachingImplementationType))
50-
{
51-
serviceCollection.ReplaceDistributedCache();
52-
TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IDistributedCacheMock>()?.Dispose();
53-
}
54-
};
55-
}
56-
}
41+
serviceCollection.ReplaceMemoryCache();
42+
TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IMemoryCache>()?.Dispose();
43+
}
5744

58-
private bool ShouldReplace(IServiceCollection serviceCollection, Type serviceType, Type implementationType)
59-
{
60-
var existingService = serviceCollection
61-
.FirstOrDefault(s => s.ServiceType == serviceType);
62-
63-
return existingService == null ||
64-
existingService.ImplementationType == implementationType;
65-
}
66-
45+
if (this.replaceDistributedCache)
46+
{
47+
serviceCollection.ReplaceDistributedCache();
48+
TestHelper.GlobalTestCleanup += () => TestServiceProvider.GetService<IDistributedCacheMock>()?.Dispose();
49+
}
50+
};
6751
}
6852
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
namespace MyTested.AspNetCore.Mvc.Test.PluginsTests
22
{
33
using System;
4+
using Microsoft.Extensions.Caching.Distributed;
45
using Microsoft.Extensions.Caching.Memory;
56
using Microsoft.Extensions.DependencyInjection;
67
using Plugins;
78
using Xunit;
89

910
public class CachingTestPluginTest
1011
{
11-
[Fact]
12-
public void ShouldThrowArgumentNullExceptionWithInvalidServiceCollection()
13-
{
14-
var testPlugin = new CachingTestPlugin();
15-
16-
Assert.Throws<NullReferenceException>(() => testPlugin.ServiceRegistrationDelegate(null));
17-
}
18-
1912
[Fact]
2013
public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection()
2114
{
2215
var testPlugin = new CachingTestPlugin();
2316
var serviceCollection = new ServiceCollection();
2417

18+
serviceCollection.AddMemoryCache();
19+
serviceCollection.AddDistributedMemoryCache();
20+
2521
testPlugin.ServiceRegistrationDelegate(serviceCollection);
2622

27-
var methodReturnType = testPlugin.ServiceRegistrationDelegate.Method.ReturnType.Name;
23+
Assert.Contains(
24+
serviceCollection,
25+
s => s.ServiceType == typeof(IMemoryCache) && s.ImplementationType == typeof(MemoryCache));
2826

29-
Assert.True(methodReturnType == "Void");
30-
Assert.Contains(serviceCollection, s => s.ServiceType == typeof(IMemoryCache));
27+
Assert.Contains(
28+
serviceCollection,
29+
s => s.ServiceType == typeof(IDistributedCache) && s.ImplementationType == typeof(MemoryDistributedCache));
3130
}
3231
}
3332
}

0 commit comments

Comments
 (0)