1- using FluentAssertions ;
1+ using Faker ;
2+ using FluentAssertions ;
3+ using Microsoft . Extensions . Caching . Memory ;
24using Microsoft . Extensions . DependencyInjection ;
35using Microsoft . Extensions . Options ;
46using OLT . Core ;
57using OLT . Extensions . Caching . Tests . Assets ;
68using StackExchange . Redis ;
9+ using StackExchange . Redis . Extensions . Core . Abstractions ;
710using StackExchange . Redis . Extensions . Core . Configuration ;
811using System ;
12+ using System . Collections . Generic ;
913using System . Threading . Tasks ;
1014using Xunit ;
1115
@@ -104,5 +108,92 @@ public async Task AsyncConfigTests()
104108 cacheService . Remove ( cacheKey ) ;
105109
106110 }
111+
112+
113+
114+ [ Fact ]
115+ public void FlushTests ( )
116+ {
117+
118+ var cacheKeys = new Dictionary < string , OltPersonName > ( ) ;
119+ var services = new ServiceCollection ( ) ;
120+ var config = new RedisConfiguration
121+ {
122+ ConnectionString = $ "{ _config . RedisCacheConnectionString . Replace ( "unit-test" , "unit-test-flush" ) } ,allowAdmin=true,defaultDatabase=4"
123+ } ;
124+
125+ var provider = TestHelper . BuildRedisProvider ( config , TimeSpan . FromMinutes ( 2 ) , "flush-tests" ) ;
126+
127+ var cacheService = provider . GetRequiredService < IOltCacheService > ( ) ;
128+
129+ for ( var idx = 0 ; idx < 10 ; idx ++ )
130+ {
131+ var model = TestHelper . CreateModel ( ) ;
132+ var cacheKey = $ "cache-person-{ Guid . NewGuid ( ) } ";
133+ cacheKeys . Add ( cacheKey , model ) ;
134+ cacheService . Get ( cacheKey , ( ) => TestHelper . CloneModel ( model ) ) . Should ( ) . BeEquivalentTo ( model ) ;
135+ }
136+
137+ foreach ( var item in cacheKeys )
138+ {
139+ var newModel = TestHelper . CreateModel ( ) ;
140+ var cacheKey = item . Key ;
141+ var expected = item . Value ;
142+ cacheService . Get ( cacheKey , ( ) => TestHelper . CloneModel ( newModel ) ) . Should ( ) . BeEquivalentTo ( expected ) ;
143+ }
144+
145+ cacheService . Flush ( ) ;
146+
147+ foreach ( var item in cacheKeys )
148+ {
149+ var newModel = TestHelper . CreateModel ( ) ;
150+ var cacheKey = item . Key ;
151+ var expected = item . Value ;
152+ cacheService . Get ( cacheKey , ( ) => TestHelper . CloneModel ( newModel ) ) . Should ( ) . BeEquivalentTo ( newModel ) ;
153+ }
154+ }
155+
156+ [ Fact ]
157+ public async Task FlushAsyncTests ( )
158+ {
159+
160+ var cacheKeys = new Dictionary < string , OltPersonName > ( ) ;
161+ var services = new ServiceCollection ( ) ;
162+
163+ var config = new RedisConfiguration
164+ {
165+ ConnectionString = $ "{ _config . RedisCacheConnectionString . Replace ( "unit-test" , "unit-test-flush-async" ) } ,allowAdmin=true,defaultDatabase=3"
166+ } ;
167+
168+ var provider = TestHelper . BuildRedisProvider ( config , TimeSpan . FromMinutes ( 2 ) , "async-flush-tests" ) ;
169+
170+ var cacheService = provider . GetRequiredService < IOltCacheService > ( ) ;
171+
172+ for ( var idx = 0 ; idx < 10 ; idx ++ )
173+ {
174+ var model = TestHelper . CreateModel ( ) ;
175+ var cacheKey = $ "cache-person-{ Guid . NewGuid ( ) } ";
176+ cacheKeys . Add ( cacheKey , model ) ;
177+ ( await cacheService . GetAsync ( cacheKey , async ( ) => await TestHelper . FakeAsync ( model ) ) ) . Should ( ) . BeEquivalentTo ( model ) ;
178+ }
179+
180+ foreach ( var item in cacheKeys )
181+ {
182+ var newModel = TestHelper . CreateModel ( ) ;
183+ var cacheKey = item . Key ;
184+ var expected = item . Value ;
185+ ( await cacheService . GetAsync ( cacheKey , async ( ) => await TestHelper . FakeAsync ( newModel ) ) ) . Should ( ) . BeEquivalentTo ( expected ) ;
186+ }
187+
188+ await cacheService . FlushAsync ( ) ;
189+
190+ foreach ( var item in cacheKeys )
191+ {
192+ var newModel = TestHelper . CreateModel ( ) ;
193+ var cacheKey = item . Key ;
194+ var expected = item . Value ;
195+ ( await cacheService . GetAsync ( cacheKey , async ( ) => await TestHelper . FakeAsync ( newModel ) ) ) . Should ( ) . BeEquivalentTo ( newModel ) ;
196+ }
197+ }
107198 }
108199}
0 commit comments