Skip to content

Commit

Permalink
Add assertions to improve clarity of unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Ross <andrross@amazon.com>
  • Loading branch information
andrross committed Feb 20, 2023
1 parent 4495659 commit 87ddd4a
Showing 1 changed file with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.store.SimpleFSLockFactory;
import org.hamcrest.MatcherAssert;
import org.junit.After;
import org.junit.Before;
import org.opensearch.common.blobstore.BlobContainer;
Expand All @@ -30,6 +31,7 @@

import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;

import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -60,29 +62,35 @@ public void tearDown() throws Exception {
public void testSingleAccess() throws IOException {
try (IndexInput i = fetchBlob()) {
i.seek(7);
MatcherAssert.assertThat(i.readByte(), equalTo((byte) 7));
}
}

public void testConcurrentAccess() throws Exception {
// Kick off multiple threads that all concurrently request the same resource
final ExecutorService testRunner = Executors.newFixedThreadPool(8);
final List<Future<IndexInput>> futures = new ArrayList<>();
for (int i = 0; i < 8; i++) {
futures.add(testRunner.submit(this::fetchBlob));
try {
final List<Future<IndexInput>> futures = new ArrayList<>();
for (int i = 0; i < 8; i++) {
futures.add(testRunner.submit(this::fetchBlob));
}
// Wait for all threads to complete
for (Future<IndexInput> future : futures) {
future.get(1, TimeUnit.SECONDS);
}
// Assert that all IndexInputs are independently positioned by seeking
// to the end and closing each one. If not independent, then this would
// result in EOFExceptions and/or NPEs.
for (Future<IndexInput> future : futures) {
try (IndexInput i = future.get()) {
i.seek(7);
MatcherAssert.assertThat(i.readByte(), equalTo((byte) 7));
}
}
} finally {
testRunner.shutdown();
assertTrue(testRunner.awaitTermination(1, TimeUnit.SECONDS));
}
// Wait for all threads to complete
for (Future<IndexInput> future : futures) {
future.get(1, TimeUnit.SECONDS);
}
// Assert that all IndexInputs are independently positioned by seeking
// to the end and closing each one. If not independent, then this would
// result in EOFExceptions and/or NPEs.
for (Future<IndexInput> future : futures) {
future.get().seek(7);
future.get().close();
}
testRunner.shutdown();
assertTrue(testRunner.awaitTermination(1, TimeUnit.SECONDS));
}

private IndexInput fetchBlob() {
Expand Down

0 comments on commit 87ddd4a

Please sign in to comment.