@@ -28,52 +28,39 @@ public ConcurrentLfuSoakTests(ITestOutputHelper testOutputHelper)
2828 [ Repeat ( soakIterations ) ]
2929 public async Task WhenConcurrentGetCacheEndsInConsistentState ( int iteration )
3030 {
31- var scheduler = new BackgroundThreadScheduler ( ) ;
32- var lfu = new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
31+ var lfu = CreateWithBackgroundScheduler ( ) ;
3332
3433 await Threaded . Run ( threads , ( ) => {
3534 for ( int i = 0 ; i < loopIterations ; i ++ )
3635 {
3736 lfu . GetOrAdd ( i + 1 , i => i . ToString ( ) ) ;
3837 }
3938 } ) ;
40-
41- this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
42-
43- scheduler . Dispose ( ) ;
44- await scheduler . Completion ;
45-
46- RunIntegrityCheck ( lfu ) ;
39+
40+ await RunIntegrityCheckAsync ( lfu , iteration ) ;
4741 }
4842
4943 [ Theory ]
5044 [ Repeat ( soakIterations ) ]
5145 public async Task WhenConcurrentGetAsyncCacheEndsInConsistentState ( int iteration )
5246 {
53- var scheduler = new BackgroundThreadScheduler ( ) ;
54- var lfu = new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
47+ var lfu = CreateWithBackgroundScheduler ( ) ;
5548
5649 await Threaded . RunAsync ( threads , async ( ) => {
5750 for ( int i = 0 ; i < loopIterations ; i ++ )
5851 {
5952 await lfu . GetOrAddAsync ( i + 1 , i => Task . FromResult ( i . ToString ( ) ) ) ;
6053 }
61- } ) ;
62-
63- this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
64-
65- scheduler . Dispose ( ) ;
66- await scheduler . Completion ;
54+ } ) ;
6755
68- RunIntegrityCheck ( lfu ) ;
56+ await RunIntegrityCheckAsync ( lfu , iteration ) ;
6957 }
7058
7159 [ Theory ]
7260 [ Repeat ( soakIterations ) ]
7361 public async Task WhenConcurrentGetWithArgCacheEndsInConsistentState ( int iteration )
7462 {
75- var scheduler = new BackgroundThreadScheduler ( ) ;
76- var lfu = new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
63+ var lfu = CreateWithBackgroundScheduler ( ) ;
7764
7865 await Threaded . Run ( threads , ( ) => {
7966 for ( int i = 0 ; i < loopIterations ; i ++ )
@@ -83,20 +70,14 @@ await Threaded.Run(threads, () => {
8370 }
8471 } ) ;
8572
86- this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
87-
88- scheduler . Dispose ( ) ;
89- await scheduler . Completion ;
90-
91- RunIntegrityCheck ( lfu ) ;
73+ await RunIntegrityCheckAsync ( lfu , iteration ) ;
9274 }
9375
9476 [ Theory ]
9577 [ Repeat ( soakIterations ) ]
9678 public async Task WhenConcurrentGetAsyncWithArgCacheEndsInConsistentState ( int iteration )
9779 {
98- var scheduler = new BackgroundThreadScheduler ( ) ;
99- var lfu = new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
80+ var lfu = CreateWithBackgroundScheduler ( ) ;
10081
10182 await Threaded . RunAsync ( threads , async ( ) => {
10283 for ( int i = 0 ; i < loopIterations ; i ++ )
@@ -106,20 +87,14 @@ await Threaded.RunAsync(threads, async () => {
10687 }
10788 } ) ;
10889
109- this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
110-
111- scheduler . Dispose ( ) ;
112- await scheduler . Completion ;
113-
114- RunIntegrityCheck ( lfu ) ;
90+ await RunIntegrityCheckAsync ( lfu , iteration ) ;
11591 }
11692
11793 [ Theory ]
11894 [ Repeat ( soakIterations ) ]
11995 public async Task WhenConcurrentGetAndUpdateCacheEndsInConsistentState ( int iteration )
12096 {
121- var scheduler = new BackgroundThreadScheduler ( ) ;
122- var lfu = new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
97+ var lfu = CreateWithBackgroundScheduler ( ) ;
12398
12499 await Threaded . Run ( threads , ( ) => {
125100 for ( int i = 0 ; i < loopIterations ; i ++ )
@@ -129,20 +104,14 @@ await Threaded.Run(threads, () => {
129104 }
130105 } ) ;
131106
132- this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
133-
134- scheduler . Dispose ( ) ;
135- await scheduler . Completion ;
136-
137- RunIntegrityCheck ( lfu ) ;
107+ await RunIntegrityCheckAsync ( lfu , iteration ) ;
138108 }
139109
140110 [ Theory ]
141111 [ Repeat ( soakIterations ) ]
142112 public async Task WhenSoakConcurrentGetAndRemoveCacheEndsInConsistentState ( int iteration )
143113 {
144- var scheduler = new BackgroundThreadScheduler ( ) ;
145- var lfu = new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
114+ var lfu = CreateWithBackgroundScheduler ( ) ;
146115
147116 await Threaded . Run ( threads , ( ) => {
148117 for ( int i = 0 ; i < loopIterations ; i ++ )
@@ -152,20 +121,14 @@ await Threaded.Run(threads, () => {
152121 }
153122 } ) ;
154123
155- this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
156-
157- scheduler . Dispose ( ) ;
158- await scheduler . Completion ;
159-
160- RunIntegrityCheck ( lfu ) ;
124+ await RunIntegrityCheckAsync ( lfu , iteration ) ;
161125 }
162126
163127 [ Theory ]
164128 [ Repeat ( soakIterations ) ]
165129 public async Task WhenConcurrentGetAndRemoveKvpCacheEndsInConsistentState ( int iteration )
166130 {
167- var scheduler = new BackgroundThreadScheduler ( ) ;
168- var lfu = new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
131+ var lfu = CreateWithBackgroundScheduler ( ) ;
169132
170133 await Threaded . Run ( threads , ( ) => {
171134 for ( int i = 0 ; i < loopIterations ; i ++ )
@@ -175,23 +138,18 @@ await Threaded.Run(threads, () => {
175138 }
176139 } ) ;
177140
178- this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
179-
180- scheduler . Dispose ( ) ;
181- await scheduler . Completion ;
182-
183- RunIntegrityCheck ( lfu ) ;
141+ await RunIntegrityCheckAsync ( lfu , iteration ) ;
184142 }
185143
186144 [ Fact ]
187145 public async Task ThreadedVerifyMisses ( )
188146 {
189147 // buffer size is 1, this will cause dropped writes on some threads where the buffer is full
190- var cache = new ConcurrentLfu < int , int > ( 1 , 20 , new NullScheduler ( ) , EqualityComparer < int > . Default ) ;
148+ var cache = new ConcurrentLfu < int , string > ( 1 , 20 , new NullScheduler ( ) , EqualityComparer < int > . Default ) ;
191149
192150 await Threaded . Run ( threads , i =>
193151 {
194- Func < int , int > func = x => x ;
152+ Func < int , string > func = x => x . ToString ( ) ;
195153
196154 int start = i * loopIterations ;
197155
@@ -208,9 +166,27 @@ await Threaded.Run(threads, i =>
208166
209167 cache . Metrics . Value . Misses . Should ( ) . Be ( loopIterations * threads ) ;
210168 RunIntegrityCheck ( cache ) ;
211- }
169+ }
170+
171+ private ConcurrentLfu < int , string > CreateWithBackgroundScheduler ( )
172+ {
173+ var scheduler = new BackgroundThreadScheduler ( ) ;
174+ return new ConcurrentLfuBuilder < int , string > ( ) . WithCapacity ( 9 ) . WithScheduler ( scheduler ) . Build ( ) as ConcurrentLfu < int , string > ;
175+ }
176+
177+ private async Task RunIntegrityCheckAsync ( ConcurrentLfu < int , string > lfu , int iteration )
178+ {
179+ this . output . WriteLine ( $ "iteration { iteration } keys={ string . Join ( " " , lfu . Keys ) } ") ;
180+
181+ var scheduler = lfu . Scheduler as BackgroundThreadScheduler ;
182+ scheduler . Dispose ( ) ;
183+ await scheduler . Completion ;
184+
185+ RunIntegrityCheck ( lfu ) ;
186+ }
187+
212188
213- private void RunIntegrityCheck < K , V > ( ConcurrentLfu < K , V > cache )
189+ private static void RunIntegrityCheck < K , V > ( ConcurrentLfu < K , V > cache )
214190 {
215191 new ConcurrentLfuIntegrityChecker < K , V > ( cache ) . Validate ( ) ;
216192 }
0 commit comments