1+ using FluentAssertions ;
2+ using LantanaGroup . Link . Shared . Application . Enums ;
3+ using LantanaGroup . Link . Shared . Application . Extensions ;
4+ using LantanaGroup . Link . Shared . Application . Interfaces ;
5+ using LantanaGroup . Link . Shared . Application . Models . Configs ;
6+ using Medallion . Threading ;
7+ using Microsoft . Extensions . Configuration ;
8+ using Microsoft . Extensions . DependencyInjection ;
9+ using StackExchange . Redis . Extensions . Core . Abstractions ;
10+
11+ namespace UnitTests . Shared . ResourceCache ;
12+
13+ [ Trait ( "Category" , "UnitTests" ) ]
14+ public class ResourceCacheExtensionsTests
15+ {
16+ [ Fact ]
17+ public void AddResourceCache_UsesExistingRedisConnection_ForHybridCache ( )
18+ {
19+ var services = new ServiceCollection ( ) ;
20+ var configuration = CreateConfiguration ( ResourceCacheType . Hybrid , includeResourceCacheRedisSettings : false ) ;
21+
22+ DistributedLockSettingsExtensions . DistributedLockBuildAndAddToDI ( services , configuration , "Redis" ) ;
23+ services . AddResourceCache ( configuration , useExistingRedisConnection : true ) ;
24+
25+ services . Count ( descriptor => descriptor . ServiceType == typeof ( IRedisDatabase ) ) . Should ( ) . Be ( 1 ) ;
26+ services . Should ( ) . ContainSingle ( descriptor => descriptor . ServiceType == typeof ( IDistributedSemaphoreProvider ) ) ;
27+ services . Should ( ) . ContainSingle ( descriptor =>
28+ descriptor . ServiceType == typeof ( IResourceCache ) && descriptor . ServiceKey == null ) ;
29+ services . Should ( ) . ContainSingle ( descriptor =>
30+ descriptor . ServiceType == typeof ( IResourceCache ) && Equals ( descriptor . ServiceKey , ResourceCacheType . Redis ) ) ;
31+ services . Should ( ) . ContainSingle ( descriptor =>
32+ descriptor . ServiceType == typeof ( IResourceCache ) && Equals ( descriptor . ServiceKey , ResourceCacheType . ABS ) ) ;
33+ }
34+
35+ [ Fact ]
36+ public void AddResourceCache_RegistersRedisConnection_ForStandaloneCache ( )
37+ {
38+ var services = new ServiceCollection ( ) ;
39+ var configuration = CreateConfiguration ( ResourceCacheType . Redis ) ;
40+
41+ services . AddResourceCache ( configuration ) ;
42+
43+ services . Count ( descriptor => descriptor . ServiceType == typeof ( IRedisDatabase ) ) . Should ( ) . Be ( 1 ) ;
44+ services . Should ( ) . ContainSingle ( descriptor => descriptor . ServiceType == typeof ( IResourceCache ) ) ;
45+ }
46+
47+ [ Fact ]
48+ public void AddResourceCache_Throws_WhenExistingRedisConnectionIsMissing ( )
49+ {
50+ var services = new ServiceCollection ( ) ;
51+ var configuration = CreateConfiguration ( ResourceCacheType . Redis , includeResourceCacheRedisSettings : false ) ;
52+
53+ var action = ( ) => services . AddResourceCache ( configuration , useExistingRedisConnection : true ) ;
54+
55+ action . Should ( ) . Throw < InvalidOperationException > ( )
56+ . WithMessage ( "*IRedisDatabase registration is required*" ) ;
57+ }
58+
59+ [ Fact ]
60+ public void AddResourceCache_DoesNotRegisterAnotherRedisConnection_ForAbsCache ( )
61+ {
62+ var services = new ServiceCollection ( ) ;
63+ var configuration = CreateConfiguration ( ResourceCacheType . ABS , includeResourceCacheRedisSettings : false ) ;
64+
65+ DistributedLockSettingsExtensions . DistributedLockBuildAndAddToDI ( services , configuration , "Redis" ) ;
66+ services . AddResourceCache ( configuration , useExistingRedisConnection : true ) ;
67+
68+ services . Count ( descriptor => descriptor . ServiceType == typeof ( IRedisDatabase ) ) . Should ( ) . Be ( 1 ) ;
69+ services . Should ( ) . ContainSingle ( descriptor => descriptor . ServiceType == typeof ( IDistributedSemaphoreProvider ) ) ;
70+ services . Should ( ) . ContainSingle ( descriptor => descriptor . ServiceType == typeof ( IResourceCache ) ) ;
71+ }
72+
73+ private static IConfiguration CreateConfiguration (
74+ ResourceCacheType cacheImplementation ,
75+ bool includeResourceCacheRedisSettings = true )
76+ {
77+ var settings = new Dictionary < string , string ? >
78+ {
79+ [ "ConnectionStrings:Redis" ] = "localhost:6379" ,
80+ [ "Redis:Password" ] = "redis-password" ,
81+ [ "DistributedLockSettings:PoolSize" ] = "3" ,
82+ [ "ResourceCache:CacheImplementation" ] = cacheImplementation . ToString ( ) ,
83+ [ "ResourceCache:BlobStorage:ConnectionString" ] = "UseDevelopmentStorage=true"
84+ } ;
85+
86+ if ( includeResourceCacheRedisSettings )
87+ {
88+ settings [ "ResourceCache:Redis:ConnectionString" ] = "localhost:6379" ;
89+ settings [ "ResourceCache:Redis:Password" ] = "redis-password" ;
90+ }
91+
92+ return new ConfigurationBuilder ( )
93+ . AddInMemoryCollection ( settings )
94+ . Build ( ) ;
95+ }
96+ }
0 commit comments