22using BitFaster . Caching . Lru ;
33using System ;
44using System . Collections . Generic ;
5- using System . Text ;
65using System . Threading . Tasks ;
76using Xunit ;
87using System . Runtime . InteropServices ;
98
109namespace BitFaster . Caching . UnitTests . Lru
1110{
12- public class ConcurrentTLruTests
11+ public abstract class ConcurrentTLruTests
1312 {
1413 private readonly TimeSpan timeToLive = TimeSpan . FromMilliseconds ( 10 ) ;
1514 private readonly ICapacityPartition capacity = new EqualCapacityPartition ( 9 ) ;
16- private ConcurrentTLru < int , string > lru ;
15+ private ICache < int , string > lru ;
1716
1817 private ValueFactory valueFactory = new ValueFactory ( ) ;
1918
@@ -27,33 +26,11 @@ private void OnLruItemRemoved(object sender, ItemRemovedEventArgs<int, int> e)
2726 removedItems . Add ( e ) ;
2827 }
2928
30- public ConcurrentTLruTests ( )
31- {
32- lru = new ConcurrentTLru < int , string > ( 1 , capacity , EqualityComparer < int > . Default , timeToLive ) ;
33- }
34-
35- [ Fact ]
36- public void ConstructWithDefaultCtorReturnsCapacity ( )
37- {
38- var x = new ConcurrentTLru < int , int > ( 3 , TimeSpan . FromSeconds ( 1 ) ) ;
39-
40- x . Capacity . Should ( ) . Be ( 3 ) ;
41- }
42-
43- [ Fact ]
44- public void ConstructCapacityCtorReturnsCapacity ( )
45- {
46- var x = new ConcurrentTLru < int , int > ( 1 , 3 , EqualityComparer < int > . Default , TimeSpan . FromSeconds ( 1 ) ) ;
47-
48- x . Capacity . Should ( ) . Be ( 3 ) ;
49- }
29+ protected abstract ICache < K , V > CreateTLru < K , V > ( ICapacityPartition capacity , TimeSpan timeToLive ) ;
5030
51- [ Fact ]
52- public void ConstructPartitionCtorReturnsCapacity ( )
31+ public ConcurrentTLruTests ( )
5332 {
54- var x = new ConcurrentTLru < int , int > ( 1 , new EqualCapacityPartition ( 3 ) , EqualityComparer < int > . Default , TimeSpan . FromSeconds ( 1 ) ) ;
55-
56- x . Capacity . Should ( ) . Be ( 3 ) ;
33+ lru = CreateTLru < int , string > ( capacity , timeToLive ) ;
5734 }
5835
5936 [ Fact ]
@@ -101,7 +78,7 @@ public async Task WhenItemIsUpdatedTtlIsExtended()
10178 [ Fact ]
10279 public void WhenValueEvictedItemRemovedEventIsFired ( )
10380 {
104- var lruEvents = new ConcurrentTLru < int , int > ( 1 , new EqualCapacityPartition ( 6 ) , EqualityComparer < int > . Default , timeToLive ) ;
81+ var lruEvents = CreateTLru < int , int > ( new EqualCapacityPartition ( 6 ) , timeToLive ) ;
10582 lruEvents . Events . Value . ItemRemoved += OnLruItemRemoved ;
10683
10784 // First 6 adds
@@ -127,7 +104,7 @@ public void WhenValueEvictedItemRemovedEventIsFired()
127104 [ Fact ]
128105 public void WhenItemRemovedEventIsUnregisteredEventIsNotFired ( )
129106 {
130- var lruEvents = new ConcurrentTLru < int , int > ( 1 , new EqualCapacityPartition ( 6 ) , EqualityComparer < int > . Default , timeToLive ) ;
107+ var lruEvents = CreateTLru < int , int > ( new EqualCapacityPartition ( 6 ) , timeToLive ) ;
131108 lruEvents . Events . Value . ItemRemoved += OnLruItemRemoved ;
132109 lruEvents . Events . Value . ItemRemoved -= OnLruItemRemoved ;
133110
@@ -195,9 +172,49 @@ public async Task WhenItemsAreExpiredTrimRemovesExpiredItems()
195172
196173 await Task . Delay ( timeToLive * ttlWaitMlutiplier ) ;
197174
198- lru . Trim ( 1 ) ;
175+ lru . Policy . Eviction . Value . Trim ( 1 ) ;
199176
200177 lru . Count . Should ( ) . Be ( 0 ) ;
201178 }
202179 }
180+
181+ public class ConcurrentTLruDefaultClockTests : ConcurrentTLruTests
182+ {
183+ protected override ICache < K , V > CreateTLru < K , V > ( ICapacityPartition capacity , TimeSpan timeToLive )
184+ {
185+ return new ConcurrentTLru < K , V > ( 1 , capacity , EqualityComparer < K > . Default , timeToLive ) ;
186+ }
187+
188+ [ Fact ]
189+ public void ConstructWithDefaultCtorReturnsCapacity ( )
190+ {
191+ var x = new ConcurrentTLru < int , int > ( 3 , TimeSpan . FromSeconds ( 1 ) ) ;
192+
193+ x . Capacity . Should ( ) . Be ( 3 ) ;
194+ }
195+
196+ [ Fact ]
197+ public void ConstructCapacityCtorReturnsCapacity ( )
198+ {
199+ var x = new ConcurrentTLru < int , int > ( 1 , 3 , EqualityComparer < int > . Default , TimeSpan . FromSeconds ( 1 ) ) ;
200+
201+ x . Capacity . Should ( ) . Be ( 3 ) ;
202+ }
203+
204+ [ Fact ]
205+ public void ConstructPartitionCtorReturnsCapacity ( )
206+ {
207+ var x = new ConcurrentTLru < int , int > ( 1 , new EqualCapacityPartition ( 3 ) , EqualityComparer < int > . Default , TimeSpan . FromSeconds ( 1 ) ) ;
208+
209+ x . Capacity . Should ( ) . Be ( 3 ) ;
210+ }
211+ }
212+
213+ public class ConcurrentTLruHighResClockTests : ConcurrentTLruTests
214+ {
215+ protected override ICache < K , V > CreateTLru < K , V > ( ICapacityPartition capacity , TimeSpan timeToLive )
216+ {
217+ return new ConcurrentLruCore < K , V , LongTickCountLruItem < K , V > , TlruStopwatchPolicy < K , V > , TelemetryPolicy < K , V > > ( 1 , capacity , EqualityComparer < K > . Default , new TlruStopwatchPolicy < K , V > ( timeToLive ) , default ) ;
218+ }
219+ }
203220}
0 commit comments