44using System . Text ;
55using System . Threading . Tasks ;
66using BitFaster . Caching . Lru ;
7+ using BitFaster . Caching . Synchronized ;
78using FluentAssertions ;
89using Xunit ;
910
@@ -14,7 +15,7 @@ public class ConcurrentLruBuilderTests
1415 [ Fact ]
1516 public void TestFastLru ( )
1617 {
17- var lru = new ConcurrentLruBuilder < int , int > ( )
18+ ICache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
1819 . Build ( ) ;
1920
2021 lru . Should ( ) . BeOfType < FastConcurrentLru < int , int > > ( ) ;
@@ -23,7 +24,7 @@ public void TestFastLru()
2324 [ Fact ]
2425 public void TestMetricsLru ( )
2526 {
26- var lru = new ConcurrentLruBuilder < int , int > ( )
27+ ICache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
2728 . WithMetrics ( )
2829 . Build ( ) ;
2930
@@ -33,7 +34,7 @@ public void TestMetricsLru()
3334 [ Fact ]
3435 public void TestFastTLru ( )
3536 {
36- var lru = new ConcurrentLruBuilder < int , int > ( )
37+ ICache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
3738 . WithExpireAfterWrite ( TimeSpan . FromSeconds ( 1 ) )
3839 . Build ( ) ;
3940
@@ -43,7 +44,7 @@ public void TestFastTLru()
4344 [ Fact ]
4445 public void TestMetricsTLru ( )
4546 {
46- var lru = new ConcurrentLruBuilder < int , int > ( )
47+ ICache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
4748 . WithExpireAfterWrite ( TimeSpan . FromSeconds ( 1 ) )
4849 . WithMetrics ( )
4950 . Build ( ) ;
@@ -53,22 +54,55 @@ public void TestMetricsTLru()
5354 }
5455
5556 [ Fact ]
56- public void TestScoped ( )
57+ public void AsAsyncTestFastLru ( )
5758 {
58- var lru = new ConcurrentLruBuilder < int , Disposable > ( )
59- . WithScopedValues ( )
60- . WithCapacity ( 3 )
61- . WithExpireAfterWrite ( TimeSpan . FromMinutes ( 1 ) )
59+ IAsyncCache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
60+ . AsAsyncCache ( )
6261 . Build ( ) ;
6362
64- lru . Should ( ) . BeOfType < ScopedCache < int , Disposable > > ( ) ;
65- lru . Capacity . Should ( ) . Be ( 3 ) ;
63+ lru . Should ( ) . BeOfType < FastConcurrentLru < int , int > > ( ) ;
64+ }
65+
66+ [ Fact ]
67+ public void AsAsyncTestMetricsLru ( )
68+ {
69+ IAsyncCache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
70+ . WithMetrics ( )
71+ . AsAsyncCache ( )
72+ . Build ( ) ;
73+
74+ lru . Should ( ) . BeOfType < ConcurrentLru < int , int > > ( ) ;
75+ }
76+
77+ [ Fact ]
78+ public void AsAsyncTestFastTLru ( )
79+ {
80+ IAsyncCache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
81+ . WithExpireAfterWrite ( TimeSpan . FromSeconds ( 1 ) )
82+ . AsAsyncCache ( )
83+ . Build ( ) ;
84+
85+ lru . Should ( ) . BeOfType < FastConcurrentTLru < int , int > > ( ) ;
6686 }
6787
88+ [ Fact ]
89+ public void AsAsyncTestMetricsTLru ( )
90+ {
91+ IAsyncCache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
92+ . WithExpireAfterWrite ( TimeSpan . FromSeconds ( 1 ) )
93+ . WithMetrics ( )
94+ . AsAsyncCache ( )
95+ . Build ( ) ;
96+
97+ lru . Should ( ) . BeOfType < ConcurrentTLru < int , int > > ( ) ;
98+ lru . Capacity . Should ( ) . Be ( 128 ) ;
99+ }
100+
101+
68102 [ Fact ]
69103 public void TestComparer ( )
70104 {
71- var fastLru = new ConcurrentLruBuilder < string , int > ( )
105+ ICache < string , int > fastLru = new ConcurrentLruBuilder < string , int > ( )
72106 . WithKeyComparer ( StringComparer . OrdinalIgnoreCase )
73107 . Build ( ) ;
74108
@@ -90,7 +124,7 @@ public void TestConcurrencyLevel()
90124 [ Fact ]
91125 public void TestIntCapacity ( )
92126 {
93- var lru = new ConcurrentLruBuilder < int , Disposable > ( )
127+ ICache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
94128 . WithCapacity ( 3 )
95129 . Build ( ) ;
96130
@@ -100,11 +134,277 @@ public void TestIntCapacity()
100134 [ Fact ]
101135 public void TestPartitionCapacity ( )
102136 {
103- var lru = new ConcurrentLruBuilder < int , Disposable > ( )
137+ ICache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
104138 . WithCapacity ( new FavorFrequencyPartition ( 6 ) )
105139 . Build ( ) ;
106140
107141 lru . Capacity . Should ( ) . Be ( 6 ) ;
108142 }
143+
144+ // There are 15 combinations to test:
145+ // -----------------------------
146+ //1 WithAtomic
147+ //2 WithScoped
148+ //3 AsAsync
149+ //
150+ // -----------------------------
151+ //4 WithAtomic
152+ // WithScoped
153+ //
154+ //5 WithScoped
155+ // WithAtomic
156+ //
157+ //6 AsAsync
158+ // WithScoped
159+ //
160+ //7 WithScoped
161+ // AsAsync
162+ //
163+ //8 WithAtomic
164+ // AsAsync
165+ //
166+ //9 AsAsync
167+ // WithAtomic
168+ //
169+ // -----------------------------
170+ //10 WithAtomic
171+ // WithScoped
172+ // AsAsync
173+ //
174+ //11 WithAtomic
175+ // AsAsync
176+ // WithScoped
177+ //
178+ //12 WithScoped
179+ // WithAtomic
180+ // AsAsync
181+ //
182+ //13 WithScoped
183+ // AsAsync
184+ // WithAtomic
185+ //
186+ //14 AsAsync
187+ // WithScoped
188+ // WithAtomic
189+ //
190+ //15 AsAsync
191+ // WithAtomic
192+ // WithScoped
193+
194+ // 1
195+ [ Fact ]
196+ public void WithScopedValues ( )
197+ {
198+ IScopedCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
199+ . WithScopedValues ( )
200+ . WithCapacity ( 3 )
201+ . Build ( ) ;
202+
203+ lru . Should ( ) . BeOfType < ScopedCache < int , Disposable > > ( ) ;
204+ lru . Capacity . Should ( ) . Be ( 3 ) ;
205+ }
206+
207+ // 2
208+ [ Fact ]
209+ public void WithAtomicFactory ( )
210+ {
211+ ICache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
212+ . WithAtomicCreate ( )
213+ . WithCapacity ( 3 )
214+ . Build ( ) ;
215+
216+ lru . Should ( ) . BeOfType < AtomicFactoryCache < int , int > > ( ) ;
217+ }
218+
219+ // 3
220+ [ Fact ]
221+ public void AsAsync ( )
222+ {
223+ IAsyncCache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
224+ . AsAsyncCache ( )
225+ . WithCapacity ( 3 )
226+ . Build ( ) ;
227+
228+ lru . Should ( ) . BeAssignableTo < IAsyncCache < int , int > > ( ) ;
229+ }
230+
231+ // 4
232+ [ Fact ]
233+ public void WithAtomicWithScope ( )
234+ {
235+ IScopedCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
236+ . WithAtomicCreate ( )
237+ . WithScopedValues ( )
238+ . WithCapacity ( 3 )
239+ . Build ( ) ;
240+
241+ lru . Should ( ) . BeOfType < AtomicFactoryScopedCache < int , Disposable > > ( ) ;
242+ lru . Capacity . Should ( ) . Be ( 3 ) ;
243+ }
244+
245+ // 5
246+ [ Fact ]
247+ public void WithScopedWithAtomic ( )
248+ {
249+ IScopedCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
250+ . WithScopedValues ( )
251+ . WithAtomicCreate ( )
252+ . WithCapacity ( 3 )
253+ . Build ( ) ;
254+
255+ lru . Should ( ) . BeOfType < AtomicFactoryScopedCache < int , Disposable > > ( ) ;
256+ lru . Capacity . Should ( ) . Be ( 3 ) ;
257+ }
258+
259+ // 6
260+ [ Fact ]
261+ public void AsAsyncWithScoped ( )
262+ {
263+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
264+ . AsAsyncCache ( )
265+ . WithScopedValues ( )
266+ . WithCapacity ( 3 )
267+ . Build ( ) ;
268+
269+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
270+
271+ lru . Capacity . Should ( ) . Be ( 3 ) ;
272+ }
273+
274+ // 7
275+ [ Fact ]
276+ public void WithScopedAsAsync ( )
277+ {
278+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
279+ . WithScopedValues ( )
280+ . AsAsyncCache ( )
281+ . WithCapacity ( 3 )
282+ . Build ( ) ;
283+
284+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
285+ lru . Capacity . Should ( ) . Be ( 3 ) ;
286+ }
287+
288+ // 8
289+ [ Fact ]
290+ public void WithAtomicAsAsync ( )
291+ {
292+ IAsyncCache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
293+ . WithAtomicCreate ( )
294+ . AsAsyncCache ( )
295+ . WithCapacity ( 3 )
296+ . Build ( ) ;
297+
298+ lru . Should ( ) . BeAssignableTo < IAsyncCache < int , int > > ( ) ;
299+ }
300+
301+ // 9
302+ [ Fact ]
303+ public void AsAsyncWithAtomic ( )
304+ {
305+ IAsyncCache < int , int > lru = new ConcurrentLruBuilder < int , int > ( )
306+ . AsAsyncCache ( )
307+ . WithAtomicCreate ( )
308+ . WithCapacity ( 3 )
309+ . Build ( ) ;
310+
311+ lru . Should ( ) . BeAssignableTo < IAsyncCache < int , int > > ( ) ;
312+ }
313+
314+ // 10
315+ [ Fact ]
316+ public void WithAtomicWithScopedAsAsync ( )
317+ {
318+ // TODO: this will not resolve a TLru
319+
320+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
321+ . WithAtomicCreate ( )
322+ . WithScopedValues ( )
323+ . AsAsyncCache ( )
324+ . WithCapacity ( 3 )
325+ . Build ( ) ;
326+
327+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
328+ }
329+
330+ // 11
331+ [ Fact ]
332+ public void WithAtomicAsAsyncWithScoped ( )
333+ {
334+ // TODO: this will not resolve a TLru
335+
336+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
337+ . WithAtomicCreate ( )
338+ . AsAsyncCache ( )
339+ . WithScopedValues ( )
340+ . WithCapacity ( 3 )
341+ . Build ( ) ;
342+
343+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
344+ }
345+
346+ // 12
347+ [ Fact ]
348+ public void WithScopedWithAtomicAsAsync ( )
349+ {
350+ // TODO: this will not resolve a TLru
351+
352+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
353+ . WithScopedValues ( )
354+ . WithAtomicCreate ( )
355+ . AsAsyncCache ( )
356+ . WithCapacity ( 3 )
357+ . Build ( ) ;
358+
359+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
360+ }
361+
362+ // 13
363+ [ Fact ]
364+ public void WithScopedAsAsyncWithAtomic ( )
365+ {
366+ // TODO: this will not resolve a TLru
367+
368+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
369+ . WithScopedValues ( )
370+ . AsAsyncCache ( )
371+ . WithAtomicCreate ( )
372+ . WithCapacity ( 3 )
373+ . Build ( ) ;
374+
375+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
376+ }
377+
378+ // 14
379+ [ Fact ]
380+ public void AsAsyncWithScopedWithAtomic ( )
381+ {
382+ // TODO: this will not resolve a TLru
383+
384+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
385+ . AsAsyncCache ( )
386+ . WithScopedValues ( )
387+ . WithAtomicCreate ( )
388+ . WithCapacity ( 3 )
389+ . Build ( ) ;
390+
391+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
392+ }
393+
394+ // 15
395+ [ Fact ]
396+ public void AsAsyncWithAtomicWithScoped ( )
397+ {
398+ // TODO: this will not resolve a TLru
399+
400+ IScopedAsyncCache < int , Disposable > lru = new ConcurrentLruBuilder < int , Disposable > ( )
401+ . AsAsyncCache ( )
402+ . WithAtomicCreate ( )
403+ . WithScopedValues ( )
404+ . WithCapacity ( 3 )
405+ . Build ( ) ;
406+
407+ lru . Should ( ) . BeAssignableTo < IScopedAsyncCache < int , Disposable > > ( ) ;
408+ }
109409 }
110410}
0 commit comments