@Test public void shouldCreateTrashFilesForUnusedBinaries() throws Exception { Set<String> storedSha1s = new HashSet<String>(); for (int i = 0; i != CONTENT.length; ++i) { Binary binary = storeAndCheck(i); if (binary instanceof StoredBinaryValue) storedSha1s.add(binary.getHexHash()); } // Make sure there are files for all stored values ... assertThat(countStoredFiles(), is(storedSha1s.size())); assertThat(countTrashFiles(), is(0)); // Mark one of the files as being unused ... String unused = storedSha1s.iterator().next(); store.markAsUnused(Collections.singleton(new BinaryKey(unused))); // Make sure the trash file was created assertThat(countStoredFiles(), is(storedSha1s.size())); assertThat(countTrashFiles(), is(1)); // Check that the name of the trash file is the SHA1 File trashFile = collectFiles(trash).get(0); assertNotNull(trashFile); assertEquals(unused, trashFile.getName()); Thread.sleep( 1100L); // Sleep more than a second, since modified times may only be accurate to nearest // second ... store.removeValuesUnusedLongerThan(1, TimeUnit.SECONDS); // Make sure the file was removed from the trash ... assertThat(countStoredFiles(), is(storedSha1s.size() - 1)); assertThat(countTrashFiles(), is(0)); // And that all directories in the trash were removed (since they should be empty) ... assertThat(trash.listFiles().length, is(0)); }