Skip to content

Commit

Permalink
Update unit tests to use try-with-resources
Browse files Browse the repository at this point in the history
Signed-off-by: Kartik Ganesh <gkart@amazon.com>
  • Loading branch information
kartg committed Dec 22, 2022
1 parent 0ae756d commit 1ab2481
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
38 changes: 20 additions & 18 deletions server/src/test/java/org/opensearch/common/lucene/LuceneTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,24 +387,26 @@ public void testReadSegmentInfosExtendedCompatibility() throws IOException {
final Version minVersion = LegacyESVersion.fromId(6000099);
Path tmp = createTempDir();
TestUtil.unzip(getClass().getResourceAsStream(pathToTestIndex), tmp);
MockDirectoryWrapper dir = newMockFSDirectory(tmp);
// The standard API will throw an exception
expectThrows(IndexFormatTooOldException.class, () -> Lucene.readSegmentInfos(dir));
SegmentInfos si = Lucene.readSegmentInfosExtendedCompatibility(dir, minVersion);
assertEquals(1, Lucene.getNumDocs(si));
IndexCommit indexCommit = Lucene.getIndexCommit(si, dir);
// uses the "expert" Lucene API
StandardDirectoryReader reader = (StandardDirectoryReader) DirectoryReader.open(
indexCommit,
minVersion.minimumIndexCompatibilityVersion().luceneVersion.major,
null
);
IndexSearcher searcher = newSearcher(reader);
// radius too small, should get no results
assertFalse(Lucene.exists(searcher, LatLonPoint.newDistanceQuery("testLocation", 48.57532, -112.87695, 2)));
assertTrue(Lucene.exists(searcher, LatLonPoint.newDistanceQuery("testLocation", 48.57532, -112.87695, 20000)));
reader.close();
dir.close();
try (MockDirectoryWrapper dir = newMockFSDirectory(tmp)) {
// The standard API will throw an exception
expectThrows(IndexFormatTooOldException.class, () -> Lucene.readSegmentInfos(dir));
SegmentInfos si = Lucene.readSegmentInfosExtendedCompatibility(dir, minVersion);
assertEquals(1, Lucene.getNumDocs(si));
IndexCommit indexCommit = Lucene.getIndexCommit(si, dir);
// uses the "expert" Lucene API
try (
StandardDirectoryReader reader = (StandardDirectoryReader) DirectoryReader.open(
indexCommit,
minVersion.minimumIndexCompatibilityVersion().luceneVersion.major,
null
)
) {
IndexSearcher searcher = newSearcher(reader);
// radius too small, should get no results
assertFalse(Lucene.exists(searcher, LatLonPoint.newDistanceQuery("testLocation", 48.57532, -112.87695, 2)));
assertTrue(Lucene.exists(searcher, LatLonPoint.newDistanceQuery("testLocation", 48.57532, -112.87695, 20000)));
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,22 @@ public void testReadOldIndices() throws IOException {
final String pathToTestIndex = "/indices/bwc/es-6.3.0/testIndex-es-6.3.0.zip";
Path tmp = createTempDir();
TestUtil.unzip(getClass().getResourceAsStream(pathToTestIndex), tmp);
Store store = createStore(newFSDirectory(tmp));
EngineConfig config = config(defaultSettings, store, createTempDir(), newMergePolicy(), null, null, globalCheckpoint::get);
try (
ReadOnlyEngine readOnlyEngine = new ReadOnlyEngine(
config,
null,
new TranslogStats(),
true,
Function.identity(),
true,
LegacyESVersion.fromId(6000099)
)
) {
assertVisibleCount(readOnlyEngine, 1, false);
try (Store store = createStore(newFSDirectory(tmp))) {
EngineConfig config = config(defaultSettings, store, createTempDir(), newMergePolicy(), null, null, globalCheckpoint::get);
try (
ReadOnlyEngine readOnlyEngine = new ReadOnlyEngine(
config,
null,
new TranslogStats(),
true,
Function.identity(),
true,
LegacyESVersion.fromId(6000099)
)
) {
assertVisibleCount(readOnlyEngine, 1, false);
}
}
store.close();
}

/**
Expand Down

0 comments on commit 1ab2481

Please sign in to comment.