@@ -164,15 +164,15 @@ void testPeriodicEvictionStrategyEvictsAtInterval() throws InterruptedException
164
164
.build ();
165
165
166
166
periodicCache .put ("x" , "1" );
167
- int ev1 = periodicCache . size ( );
168
- int ev2 = periodicCache .size ( );
169
- Thread . sleep ( 50 );
170
- int ev3 = periodicCache .size ( );
171
-
172
- Assertions .assertEquals (1 , ev1 );
173
- Assertions .assertEquals (1 , ev2 );
174
- Assertions .assertEquals (0 , ev3 , "Eviction should happen on the 3rd access" );
175
- Assertions .assertEquals (0 , cache .size ());
167
+ Thread . sleep ( 100 );
168
+ int ev1 = periodicCache .getEvictionStrategy (). onAccess ( periodicCache );
169
+ int ev2 = periodicCache . getEvictionStrategy (). onAccess ( periodicCache );
170
+ int ev3 = periodicCache .getEvictionStrategy (). onAccess ( periodicCache );
171
+
172
+ Assertions .assertEquals (0 , ev1 );
173
+ Assertions .assertEquals (0 , ev2 );
174
+ Assertions .assertEquals (1 , ev3 , "Eviction should happen on the 3rd access" );
175
+ Assertions .assertEquals (0 , periodicCache .size ());
176
176
}
177
177
178
178
@ Test
@@ -193,10 +193,10 @@ void testNoEvictionStrategyEvictsOnEachCall() throws InterruptedException {
193
193
.build ();
194
194
195
195
noEvictionStrategyCache .put ("x" , "1" );
196
- Thread .sleep (50 );
197
- int size = noEvictionStrategyCache .size ( );
196
+ Thread .sleep (100 );
197
+ int evicted = noEvictionStrategyCache .getEvictionStrategy (). onAccess ( noEvictionStrategyCache );
198
198
199
- Assertions .assertEquals (0 , size );
199
+ Assertions .assertEquals (1 , evicted );
200
200
}
201
201
202
202
@ Test
@@ -208,4 +208,37 @@ void testBuilderThrowsExceptionIfEvictionStrategyNull() {
208
208
209
209
Assertions .assertThrows (IllegalArgumentException .class , executable );
210
210
}
211
+
212
+
213
+ @ Test
214
+ void testReturnsCorrectStrategyInstance () {
215
+ RRCache .EvictionStrategy <String , String > strategy = new RRCache .NoEvictionStrategy <>();
216
+
217
+ RRCache <String , String > newCache = new RRCache .Builder <String , String >(10 )
218
+ .defaultTTL (1000 )
219
+ .evictionStrategy (strategy )
220
+ .build ();
221
+
222
+ Assertions .assertSame (strategy , newCache .getEvictionStrategy (), "Returned strategy should be the same instance" );
223
+ }
224
+
225
+ @ Test
226
+ void testDefaultStrategyIsNoEviction () {
227
+ RRCache <String , String > newCache = new RRCache .Builder <String , String >(5 )
228
+ .defaultTTL (1000 )
229
+ .build ();
230
+
231
+ Assertions .assertTrue (
232
+ newCache .getEvictionStrategy () instanceof RRCache .PeriodicEvictionStrategy <String , String >,
233
+ "Default strategy should be NoEvictionStrategy"
234
+ );
235
+ }
236
+
237
+ @ Test
238
+ void testGetEvictionStrategyIsNotNull () {
239
+ RRCache <String , String > newCache = new RRCache .Builder <String , String >(5 )
240
+ .build ();
241
+
242
+ Assertions .assertNotNull (newCache .getEvictionStrategy (), "Eviction strategy should never be null" );
243
+ }
211
244
}
0 commit comments