@@ -301,7 +301,7 @@ void testRetentionCleanupDisabledByDefault() {
301301 cached .setInstanceState (InstanceStatusEnum .DELETED .getCode ());
302302 cached .setLastHeartBeatTime (System .currentTimeMillis () - Duration .ofDays (30 ).toMillis ());
303303
304- ReflectionTestUtils . invokeMethod ( instanceCheckService , " purgeExpiredInstances" );
304+ instanceCheckService . purgeExpiredInstances ( );
305305
306306 assertNotNull (instanceCheckService .getInstanceHealthBeatInfo (dto ));
307307 verify (instanceInfoService , never ()).deleteExpired (anyLong ());
@@ -324,7 +324,7 @@ void testRetentionCleanupRemovesOnlyExpiredDeletedInstances() {
324324 freshDeleted .setInstanceState (InstanceStatusEnum .DELETED .getCode ());
325325 InstanceInfoVO online = instanceCheckService .getInstanceHealthBeatInfo (onlineDTO );
326326
327- ReflectionTestUtils . invokeMethod ( instanceCheckService , " purgeExpiredInstances" );
327+ instanceCheckService . purgeExpiredInstances ( );
328328
329329 assertNull (instanceCheckService .getInstanceHealthBeatInfo (expiredDTO ));
330330 assertNotNull (instanceCheckService .getInstanceHealthBeatInfo (freshDeletedDTO ));
@@ -344,7 +344,7 @@ void testRetentionCleanupKeepsRenewedIdentity() {
344344
345345 // the instance heartbeats again before the cleanup cycle runs, so its cached record is kept
346346 instanceCheckService .handleBeatInfo (dto );
347- ReflectionTestUtils . invokeMethod ( instanceCheckService , " purgeExpiredInstances" );
347+ instanceCheckService . purgeExpiredInstances ( );
348348
349349 assertNotNull (instanceCheckService .getInstanceHealthBeatInfo (dto ));
350350 }
@@ -359,12 +359,28 @@ void testRetentionCleanupRemovesLegacyRowsWithoutHeartbeat() {
359359 when (instanceInfoService .list ()).thenReturn (Collections .singletonList (legacy ));
360360 instanceCheckService .fetchInstanceData ();
361361
362- ReflectionTestUtils . invokeMethod ( instanceCheckService , " purgeExpiredInstances" );
362+ instanceCheckService . purgeExpiredInstances ( );
363363
364364 assertNull (instanceCheckService .getInstanceHealthBeatInfo ("11.0.0.1:8090@http#nsH" ));
365365 verify (instanceInfoService ).deleteExpired (anyLong ());
366366 }
367367
368+ @ Test
369+ void testDatabaseSweepIsThrottledToConfiguredInterval () {
370+ when (instanceRetentionProperties .isEnabled ()).thenReturn (true );
371+ when (instanceRetentionProperties .getTtl ()).thenReturn (Duration .ofDays (7 ));
372+
373+ instanceCheckService .purgeExpiredInstances ();
374+ instanceCheckService .purgeExpiredInstances ();
375+ verify (instanceInfoService , times (1 )).deleteExpired (anyLong ());
376+
377+ // the database sweep runs again only after the cleanup interval has elapsed
378+ ReflectionTestUtils .setField (instanceCheckService , "lastDbCleanupTime" ,
379+ System .currentTimeMillis () - Duration .ofHours (2 ).toMillis ());
380+ instanceCheckService .purgeExpiredInstances ();
381+ verify (instanceInfoService , times (2 )).deleteExpired (anyLong ());
382+ }
383+
368384 private InstanceInfoVO buildVO (final String ip , final String port , final String type , final String ns ) {
369385 InstanceInfoVO v = new InstanceInfoVO ();
370386 v .setInstanceIp (ip );
@@ -389,3 +405,4 @@ private InstanceBeatInfoDTO buildDTO(final String ip, final String port, final S
389405 return dto ;
390406 }
391407}
408+
0 commit comments