1111using Xtensive . Core ;
1212using Xtensive . Orm . Tests ;
1313
14- #pragma warning disable IDE0058
15-
1614namespace Xtensive . Orm . Tests . Core . Caching
1715{
1816 [ TestFixture ]
1917 public class FastConcurrentLruCacheTest
2018 {
21- private FastConcurrentLruCache < string , TestClass > globalCache ;
22- private readonly Random random = RandomManager . CreateRandom ( ( int ) DateTime . Now . Ticks ) ;
23-
24- private class BadTestClass :
25- IIdentified < string > ,
26- IHasSize
19+ private class BadTestClass : IIdentified < string > , IHasSize
2720 {
28- object IIdentified . Identifier
29- {
30- get { return Identifier ; }
31- }
21+ object IIdentified . Identifier => Identifier ;
22+ public string Identifier => null ;
23+ public long Size => 1 ;
24+ }
3225
33- public string Identifier
34- {
35- get { return null ; }
36- }
26+ private const int TestCacheCapacity = 6 ; // divides by 3
3727
38- public long Size
39- {
40- get { return 1 ; }
41- }
42- }
28+ private FastConcurrentLruCache < string , TestClass > globalCache ;
29+ private readonly Random random = RandomManager . CreateRandom ( ( int ) DateTime . Now . Ticks ) ;
4330
4431 [ Test ]
4532 public void ConstructorsTest ( )
4633 {
47- var cache = new FastConcurrentLruCache < string , TestClass > (
48- 1000 ,
49- value => value . Text ) ;
34+ const int maxCount = 1000 ;
5035
51- var cache1 = new FastConcurrentLruCache < string , TestClass > (
52- 1000 ,
53- ( value ) => value . Text
54- ) ;
36+ var cache = CreateCacheInstance ( maxCount ) ;
5537
38+ var cache1 = CreateCacheInstance ( maxCount ) ;
5639
57- TestClass item = new TestClass ( "1" ) ;
40+ var item = new TestClass ( "1" ) ;
5841 cache . Add ( item ) ;
5942 cache1 . Add ( item ) ;
6043 Assert . AreEqual ( 1 , cache1 . Count ) ;
6144
62- for ( int i = 0 ; i < 100000 ; i ++ ) {
63- TestClass test = new TestClass ( "" + i ) ;
64- cache1 . Add ( test ) ;
45+ for ( var i = 0 ; i < 100000 ; i ++ ) {
46+ cache1 . Add ( new TestClass ( "" + i ) ) ;
6547 }
6648 }
6749
68- [ Test ]
69- public void ConstructorDenyTest ( )
50+ [ TestCase ( - 1 ) ]
51+ [ TestCase ( 0 ) ]
52+ [ TestCase ( 1 ) ]
53+ [ TestCase ( 2 ) ]
54+ public void ConstructorDenyTest ( int deniedCapacity )
7055 {
71- Assert . Throws < ArgumentOutOfRangeException > ( ( ) => {
72- var cache =
73- new FastConcurrentLruCache < string , TestClass > (
74- - 1 ,
75- value => value . Text
76- ) ;
56+ _ = Assert . Throws < ArgumentOutOfRangeException > ( ( ) => {
57+ _ = CreateCacheInstance ( deniedCapacity ) ;
7758 } ) ;
7859 }
7960
8061 [ Test ]
8162 public void AddRemoveTest ( )
8263 {
83- var cache = new FastConcurrentLruCache < string , TestClass > (
84- 100 ,
85- value => value . Text ) ;
64+ var cache = CreateCacheInstance ( TestCacheCapacity ) ;
8665
87- TestClass item = new TestClass ( "1" ) ;
66+ var item = new TestClass ( "1" ) ;
8867 cache . Add ( item ) ;
8968 Assert . AreEqual ( 1 , cache . Count ) ;
69+
9070 item = new TestClass ( "2" ) ;
9171 cache . Add ( item ) ;
9272 Assert . AreEqual ( 2 , cache . Count ) ;
9373 Assert . AreEqual ( item , cache [ item . Text , false ] ) ;
74+
9475 ICache < string , TestClass > icache = cache ;
9576 Assert . AreEqual ( item , icache [ item . Text , false ] ) ;
9677 Assert . AreEqual ( null , icache [ "3" , false ] ) ;
78+
9779 cache . Remove ( item ) ;
9880 Assert . AreEqual ( 1 , cache . Count ) ;
81+
9982 cache . Clear ( ) ;
10083 Assert . AreEqual ( 0 , cache . Count ) ;
10184 }
10285
10386 [ Test ]
10487 public void AddDenyTest1 ( )
10588 {
106- var cache = new FastConcurrentLruCache < string , TestClass > (
107- 100 ,
108- value => value . Text ) ;
109- Assert . Throws < ArgumentNullException > ( ( ) => cache . Add ( null ) ) ;
89+ var cache = CreateCacheInstance ( TestCacheCapacity ) ;
90+ _ = Assert . Throws < ArgumentNullException > ( ( ) => cache . Add ( null ) ) ;
11091 }
11192
11293 [ Test ]
11394 public void AddDenyTest3 ( )
11495 {
115- var cache =
116- new FastConcurrentLruCache < string , BadTestClass > (
117- 100 ,
96+ var cache = new FastConcurrentLruCache < string , BadTestClass > (
97+ TestCacheCapacity ,
11898 value => value . Identifier ) ;
119- Assert . Throws < ArgumentNullException > ( ( ) => cache . Add ( new BadTestClass ( ) ) ) ;
99+ _ = Assert . Throws < ArgumentNullException > ( ( ) => cache . Add ( new BadTestClass ( ) ) ) ;
120100 }
121101
122102 [ Test ]
123103 public void RemoveDenyTest1 ( )
124104 {
125- var cache =
126- new FastConcurrentLruCache < string , TestClass > (
127- 100 ,
128- value => value . Text ) ;
129- Assert . Throws < ArgumentNullException > ( ( ) => cache . Remove ( null ) ) ;
105+ var cache = CreateCacheInstance ( TestCacheCapacity ) ;
106+ _ = Assert . Throws < ArgumentNullException > ( ( ) => cache . Remove ( null ) ) ;
130107 }
131108
132109 [ Test ]
133110 public void RemoveDenyTest2 ( )
134111 {
135- var cache =
136- new FastConcurrentLruCache < string , TestClass > (
137- 100 ,
138- value => value . Text ) ;
139- Assert . Throws < ArgumentNullException > ( ( ) => cache . RemoveKey ( null ) ) ;
112+ var cache = CreateCacheInstance ( TestCacheCapacity ) ;
113+ _ = Assert . Throws < ArgumentNullException > ( ( ) => cache . RemoveKey ( null ) ) ;
140114 }
141115
142116 [ Test ]
143117 public void RemoveDenyTest3 ( )
144118 {
145119 var cache =
146120 new FastConcurrentLruCache < string , BadTestClass > (
147- 100 ,
121+ TestCacheCapacity ,
148122 value => value . Identifier ) ;
149- BadTestClass test1 = new BadTestClass ( ) ;
150- Assert . Throws < ArgumentNullException > ( ( ) => cache . Remove ( test1 ) ) ;
123+ var test1 = new BadTestClass ( ) ;
124+ _ = Assert . Throws < ArgumentNullException > ( ( ) => cache . Remove ( test1 ) ) ;
151125 }
152126
153127 private static readonly bool canFinish = true ;
@@ -165,18 +139,18 @@ public void SynchronizationTest()
165139 var removeThreads = new Task [ 10 ] ;
166140 var cancellationTokenSource = new CancellationTokenSource ( ) ;
167141
168- for ( int i = 0 ; i < 10 ; i ++ ) {
142+ for ( var i = 0 ; i < 10 ; i ++ ) {
169143 addThreads [ i ] = new Task ( ( ) => AddItem ( cancellationTokenSource . Token ) , cancellationTokenSource . Token ) ;
170144 removeThreads [ i ] = new Task ( ( ) => RemoveItem ( cancellationTokenSource . Token ) , cancellationTokenSource . Token ) ;
171145 }
172146
173147 try {
174- for ( int i = 0 ; i < 10 ; i ++ ) {
148+ for ( var i = 0 ; i < 10 ; i ++ ) {
175149 addThreads [ i ] . Start ( ) ;
176150 }
177151 Thread . Sleep ( 10 ) ;
178152
179- for ( int i = 0 ; i < 10 ; i ++ ) {
153+ for ( var i = 0 ; i < 10 ; i ++ ) {
180154 removeThreads [ i ] . Start ( ) ;
181155 }
182156 Thread . Sleep ( 200 ) ;
@@ -193,9 +167,7 @@ public void SynchronizationTest()
193167
194168 private void AddItem ( CancellationToken cancellationToken )
195169 {
196- int count = random . Next ( 100000 ) ;
197- int counter = 0 ;
198- bool whileCondition = ( counter ++ ) < 10 || ! cancellationToken . IsCancellationRequested ;
170+ var count = random . Next ( 100000 ) ;
199171 while ( ! cancellationToken . IsCancellationRequested ) {
200172 globalCache . Add ( new TestClass ( "item " + count ) ) ;
201173 count ++ ;
@@ -205,19 +177,23 @@ private void AddItem(CancellationToken cancellationToken)
205177
206178 private void RemoveItem ( CancellationToken cancellationToken )
207179 {
208- int counter = 0 ;
209180 while ( ! cancellationToken . IsCancellationRequested ) {
210181 TestClass test = null ;
211182 foreach ( TestClass testClass in globalCache ) {
212183 test = testClass ;
213184 break ;
214185 }
215- if ( test != null )
186+ if ( test != null ) {
216187 globalCache . Remove ( test ) ;
188+ }
217189 }
218190 cancellationToken . ThrowIfCancellationRequested ( ) ;
219191 }
220192
193+ private FastConcurrentLruCache < string , TestClass > CreateCacheInstance ( int maxCount ) =>
194+ new FastConcurrentLruCache < string , TestClass > ( maxCount , value => value . Text ) ;
195+
196+
221197 private class ThreadPoolThreadsIncreaser : Disposable
222198 {
223199 private int previousWorkingThreadsCount ;
@@ -228,29 +204,21 @@ private class ThreadPoolThreadsIncreaser : Disposable
228204
229205 private void Increase ( int workingThreadsCount , int ioThreadsCount )
230206 {
231- int minWorkingThreads ;
232- int minIOTheads ;
233- ThreadPool . GetMinThreads ( out minWorkingThreads , out minIOTheads ) ;
207+ ThreadPool . GetMinThreads ( out var minWorkingThreads , out var minIOTheads ) ;
234208 previousWorkingThreadsCount = minWorkingThreads ;
235209 previousIOThreadsCount = minIOTheads ;
236210
237- ThreadPool . SetMinThreads ( workingThreadsCount , ioThreadsCount ) ;
211+ _ = ThreadPool . SetMinThreads ( workingThreadsCount , ioThreadsCount ) ;
238212 }
239213
240214 private static void Decrease ( Func < int > workingThreadsCountAcccessor , Func < int > ioThreadsCountAcccessor )
241215 {
242- ThreadPool . SetMinThreads ( workingThreadsCountAcccessor ( ) , ioThreadsCountAcccessor ( ) ) ;
216+ _ = ThreadPool . SetMinThreads ( workingThreadsCountAcccessor ( ) , ioThreadsCountAcccessor ( ) ) ;
243217 }
244218
245- private int Aa ( )
246- {
247- return previousWorkingThreadsCount ;
248- }
219+ private int Aa ( ) => previousWorkingThreadsCount ;
249220
250- private int Bb ( )
251- {
252- return previousIOThreadsCount ;
253- }
221+ private int Bb ( ) => previousIOThreadsCount ;
254222
255223
256224 public ThreadPoolThreadsIncreaser ( int workingThreadsCount , int ioThreadsCount )
0 commit comments