2
2
using BitFaster . Caching . Lru ;
3
3
using System ;
4
4
using System . Collections . Generic ;
5
- using System . Text ;
6
5
using System . Threading . Tasks ;
7
6
using Xunit ;
8
7
using System . Runtime . InteropServices ;
9
8
10
9
namespace BitFaster . Caching . UnitTests . Lru
11
10
{
12
- public class ConcurrentTLruTests
11
+ public abstract class ConcurrentTLruTests
13
12
{
14
13
private readonly TimeSpan timeToLive = TimeSpan . FromMilliseconds ( 10 ) ;
15
14
private readonly ICapacityPartition capacity = new EqualCapacityPartition ( 9 ) ;
16
- private ConcurrentTLru < int , string > lru ;
15
+ private ICache < int , string > lru ;
17
16
18
17
private ValueFactory valueFactory = new ValueFactory ( ) ;
19
18
@@ -27,33 +26,11 @@ private void OnLruItemRemoved(object sender, ItemRemovedEventArgs<int, int> e)
27
26
removedItems . Add ( e ) ;
28
27
}
29
28
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 ) ;
50
30
51
- [ Fact ]
52
- public void ConstructPartitionCtorReturnsCapacity ( )
31
+ public ConcurrentTLruTests ( )
53
32
{
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 ) ;
57
34
}
58
35
59
36
[ Fact ]
@@ -101,7 +78,7 @@ public async Task WhenItemIsUpdatedTtlIsExtended()
101
78
[ Fact ]
102
79
public void WhenValueEvictedItemRemovedEventIsFired ( )
103
80
{
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 ) ;
105
82
lruEvents . Events . Value . ItemRemoved += OnLruItemRemoved ;
106
83
107
84
// First 6 adds
@@ -127,7 +104,7 @@ public void WhenValueEvictedItemRemovedEventIsFired()
127
104
[ Fact ]
128
105
public void WhenItemRemovedEventIsUnregisteredEventIsNotFired ( )
129
106
{
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 ) ;
131
108
lruEvents . Events . Value . ItemRemoved += OnLruItemRemoved ;
132
109
lruEvents . Events . Value . ItemRemoved -= OnLruItemRemoved ;
133
110
@@ -195,9 +172,49 @@ public async Task WhenItemsAreExpiredTrimRemovesExpiredItems()
195
172
196
173
await Task . Delay ( timeToLive * ttlWaitMlutiplier ) ;
197
174
198
- lru . Trim ( 1 ) ;
175
+ lru . Policy . Eviction . Value . Trim ( 1 ) ;
199
176
200
177
lru . Count . Should ( ) . Be ( 0 ) ;
201
178
}
202
179
}
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
+ }
203
220
}
0 commit comments