@Test
public void testBasicSnapshots() throws Exception {
int numSnapshots = 3;
SnapshotDeletionPolicy sdp = getDeletionPolicy();
// Create 3 snapshots: snapshot0, snapshot1, snapshot2
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, getConfig(random, sdp));
prepareIndexAndSnapshots(sdp, writer, numSnapshots, "snapshot");
writer.close();
assertSnapshotExists(dir, sdp, numSnapshots);
// open a reader on a snapshot - should succeed.
IndexReader.open(sdp.getSnapshot("snapshot0"), true).close();
// open a new IndexWriter w/ no snapshots to keep and assert that all snapshots are gone.
sdp = getDeletionPolicy();
writer = new IndexWriter(dir, getConfig(random, sdp));
writer.deleteUnusedFiles();
writer.close();
assertEquals("no snapshots should exist", 1, IndexReader.listCommits(dir).size());
for (int i = 0; i < numSnapshots; i++) {
try {
sdp.getSnapshot("snapshot" + i);
fail("snapshot shouldn't have existed, but did: snapshot" + i);
} catch (IllegalStateException e) {
// expected - snapshot should not exist
}
}