### Question:
SparkCredentialsUpdater extends AbstractIdleService implements Runnable { private void cleanup() { try { Locations.processLocations(credentialsDir, false, new Processor<LocationStatus, Runnable>() { private final List<LocationStatus> cleanupFiles = new ArrayList<>(); @Override public boolean process(LocationStatus status) { if (!status.isDir()) { cleanupFiles.add(status); } return true; } @Override public Runnable getResult() { return new Runnable() { @Override public void run() { if (cleanupFiles.size() <= minFilesToKeep) { return; } Collections.sort(cleanupFiles, new Comparator<LocationStatus>() { @Override public int compare(LocationStatus o1, LocationStatus o2) { return Long.compare(o2.getLastModified(), o1.getLastModified()); } }); final long thresholdTime = System.currentTimeMillis() - cleanupExpireMs; for (LocationStatus locationStatus : cleanupFiles.subList(minFilesToKeep, cleanupFiles.size())) { if (locationStatus.getLastModified() >= thresholdTime) { continue; } Location location = credentialsDir.getLocationFactory().create(locationStatus.getUri()); try { location.delete(); LOG.debug("Removed old credential file {}", location); } catch (Exception e) { LOG.warn("Failed to cleanup old credential file {}", location, e); } } } }; } }).run(); } catch (Exception e) { LOG.warn("Exception raised when cleaning up credential files in {}", credentialsDir, e); } } SparkCredentialsUpdater(Supplier<Credentials> credentialsSupplier,
                                 Location credentialsDir, String fileNamePrefix, long updateIntervalMs,
                                 long cleanupExpireMs, int minFilesToKeep); @Override void run();  }### Answer:
@Test public void testCleanup() throws IOException, InterruptedException { Location credentialsDir = Locations.toLocation(TEMPORARY_FOLDER.newFolder()); SparkCredentialsUpdater updater = new SparkCredentialsUpdater(createCredentialsSupplier(), credentialsDir, "credentials", TimeUnit.DAYS.toMillis(1), TimeUnit.SECONDS.toMillis(3), 3) { @Override long getNextUpdateDelay(Credentials credentials) throws IOException { return TimeUnit.DAYS.toMillis(1); } }; updater.startAndWait(); try { for (int i = 1; i <= 5; i++) { Assert.assertEquals(i, credentialsDir.list().size()); updater.run(); } TimeUnit.SECONDS.sleep(3); updater.run(); Assert.assertEquals(3, credentialsDir.list().size()); } finally { updater.stopAndWait(); } }