EvictExpiredRecordsPersistence extends Action { public Observable<Integer> startEvictingExpiredRecords() { List<String> allKeys = persistence.allKeys(); for (String key : allKeys) { Record record = persistence.retrieveRecord(key, false, encryptKey); if (record == null && encryptKey != null && !encryptKey.isEmpty()) { record = persistence.retrieveRecord(key, true, encryptKey); } if (record != null && hasRecordExpired.hasRecordExpired(record)) { persistence.evict(key); } } return Observable.just(1); } @Inject  EvictExpiredRecordsPersistence(Memory memory, Persistence persistence,
      HasRecordExpired hasRecordExpired, String encryptKey); Observable<Integer> startEvictingExpiredRecords();  }
@Test public void Evict_Just_Expired_Records() { int recordsCount = 100; for (int i = 0; i < recordsCount/2; i++) { twoLayersCache.save(i+"_expired", "", "", new Mock(i+"_expired"), ONE_SECOND_LIFE, true, false); twoLayersCache.save(i+"_live", "", "", new Mock(i+"_live"), THIRTY_SECOND_LIFE, true, false); } waitTime(MORE_THAN_ONE_SECOND_LIFE); assertThat(disk.allKeys().size(), is(recordsCount)); TestObserver<Integer> testObserver = evictExpiredRecordsPersistenceUT.startEvictingExpiredRecords().test(); testObserver.awaitTerminalEvent(); testObserver.assertNoErrors(); List<String> allKeys = disk.allKeys(); assertThat(allKeys.size(), is(recordsCount / 2)); for (String key : allKeys) { key = key.substring(0, key.indexOf("$")); Record<Mock> record = twoLayersCache.retrieve(key, "", "", false, THIRTY_SECOND_LIFE, false); assert(record.getData().getMessage().contains("live")); assert(!record.getData().getMessage().contains("expired")); } }