@@ -164,15 +164,15 @@ void testPeriodicEvictionStrategyEvictsAtInterval() throws InterruptedException
164164 .build ();
165165
166166 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 ());
176176 }
177177
178178 @ Test
@@ -193,10 +193,10 @@ void testNoEvictionStrategyEvictsOnEachCall() throws InterruptedException {
193193 .build ();
194194
195195 noEvictionStrategyCache .put ("x" , "1" );
196- Thread .sleep (50 );
197- int size = noEvictionStrategyCache .size ( );
196+ Thread .sleep (100 );
197+ int evicted = noEvictionStrategyCache .getEvictionStrategy (). onAccess ( noEvictionStrategyCache );
198198
199- Assertions .assertEquals (0 , size );
199+ Assertions .assertEquals (1 , evicted );
200200 }
201201
202202 @ Test
@@ -208,4 +208,37 @@ void testBuilderThrowsExceptionIfEvictionStrategyNull() {
208208
209209 Assertions .assertThrows (IllegalArgumentException .class , executable );
210210 }
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+ }
211244}
0 commit comments