@@ -70,15 +70,11 @@ public async Task DefaultEvictionBehavior_RemovesAbsoluteExpiredItem_LeavesNonEx
7070 {
7171 {
7272 notExpiredKey , new CacheEntity < string , IAsyncDisposable > ( notExpiredKey , ( ) => Task . FromResult ( notExpiredCacheObject ) , AsyncLazyFlags . None )
73- {
74- AbsoluteExpiration = DateTimeOffset . UtcNow . AddDays ( 2 )
75- }
73+ . WithAbsoluteExpiration ( DateTimeOffset . UtcNow . AddDays ( 2 ) )
7674 } ,
7775 {
7876 expiredKey , new CacheEntity < string , IAsyncDisposable > ( expiredKey , ( ) => Task . FromResult ( expiredCacheObject ) , AsyncLazyFlags . None )
79- {
80- AbsoluteExpiration = DateTimeOffset . UtcNow . AddMinutes ( - 1 )
81- }
77+ . WithAbsoluteExpiration ( DateTimeOffset . UtcNow . AddMinutes ( - 1 ) )
8278 }
8379 } ;
8480
@@ -98,15 +94,11 @@ public async Task DefaultEvictionBehavior_RemovesSlidingExpiredItem_LeavesNonExp
9894 {
9995 {
10096 notExpiredKey , new CacheEntity < string , IAsyncDisposable > ( notExpiredKey , ( ) => Task . FromResult ( notExpiredCacheObject ) , AsyncLazyFlags . None )
101- {
102- SlidingExpiration = TimeSpan . FromDays ( 2 )
103- }
97+ . WithSlidingExpiration ( TimeSpan . FromDays ( 2 ) )
10498 } ,
10599 {
106100 expiredKey , new CacheEntity < string , IAsyncDisposable > ( expiredKey , ( ) => Task . FromResult ( expiredCacheObject ) , AsyncLazyFlags . None )
107- {
108- SlidingExpiration = TimeSpan . FromTicks ( 1 )
109- }
101+ . WithSlidingExpiration ( TimeSpan . FromTicks ( 1 ) )
110102 }
111103 } ;
112104
@@ -198,9 +190,7 @@ public async Task ExpiredCacheItem_AlwaysDisposedIfRefCountBelowZero()
198190 . Returns (
199191 [
200192 new CacheEntity < string , IAsyncDisposable > ( expiredKey , ( ) => Task . FromResult ( expiredCacheObject ) , AsyncLazyFlags . None )
201- {
202- AbsoluteExpiration = DateTimeOffset . UtcNow
203- }
193+ . WithAbsoluteExpiration ( DateTimeOffset . UtcNow )
204194 ] )
205195 . AndDoes ( _ => resetEvent . Set ( ) ) ;
206196
@@ -233,9 +223,7 @@ public async Task ExpiredCacheItem_NeverDisposedIfRefCountAboveZero()
233223 {
234224 {
235225 expiredKey , new CacheEntity < string , IAsyncDisposable > ( expiredKey , ( ) => Task . FromResult ( expiredCacheObject ) , AsyncLazyFlags . None )
236- {
237- AbsoluteExpiration = DateTimeOffset . UtcNow
238- }
226+ . WithAbsoluteExpiration ( DateTimeOffset . UtcNow )
239227 }
240228 } ;
241229
@@ -258,4 +246,35 @@ public async Task ExpiredCacheItem_NeverDisposedIfRefCountAboveZero()
258246
259247 await expiredCacheObject . DidNotReceive ( ) . DisposeAsync ( ) ;
260248 }
249+
250+ [ Fact ]
251+ public async Task CacheItemWithoutExpirationStrategy_NeverDisposed ( )
252+ {
253+ var expiredCacheObject = Substitute . For < IAsyncDisposable > ( ) ;
254+ const string expiredKey = "expired" ;
255+ var cacheBackingStore = new Dictionary < string , CacheEntity < string , IAsyncDisposable > >
256+ {
257+ {
258+ // Not setting any expiration strategy
259+ expiredKey , new CacheEntity < string , IAsyncDisposable > ( expiredKey , ( ) => Task . FromResult ( expiredCacheObject ) , AsyncLazyFlags . None )
260+ }
261+ } ;
262+
263+ var resetEvent = new ManualResetEvent ( false ) ;
264+ var config = Substitute . For < IAsyncMemoryCacheConfiguration < string , IAsyncDisposable > > ( ) ;
265+ _ = config . CacheBackingStore
266+ . Returns ( cacheBackingStore )
267+ . AndDoes ( _ => resetEvent . Set ( ) ) ;
268+
269+ var timeProvider = new FakeTimeProvider ( DateTime . UtcNow ) ;
270+ var target = new DefaultEvictionBehavior ( timeProvider ) ;
271+ target . Start ( config , _logger ) ;
272+
273+ timeProvider . Advance ( TimeSpan . FromDays ( 30 ) ) ;
274+
275+ _ = resetEvent . WaitOne ( ) ;
276+ await target . DisposeAsync ( ) ;
277+
278+ await expiredCacheObject . DidNotReceive ( ) . DisposeAsync ( ) ;
279+ }
261280}
0 commit comments