@Test
  public void memoryMapFailureTest() throws IOException, StoreException {
    assumeTrue(formatVersion == PersistentIndex.VERSION_1
        && config.storeIndexMemState == IndexMemState.MMAP_WITHOUT_FORCE_LOAD);
    LogSegmentName logSegmentName = LogSegmentName.fromPositionAndGeneration(0, 0);
    StoreKeyFactory mockStoreKeyFactory = Mockito.spy(STORE_KEY_FACTORY);
    IndexSegment indexSegment = generateIndexSegment(new Offset(logSegmentName, 0), mockStoreKeyFactory);
    // verify that StoreErrorCodes.File_Not_Found can be captured when performing memory map
    try {
      indexSegment.seal();
      fail("should fail");
    } catch (StoreException e) {
      assertEquals("Mismatch in error code", StoreErrorCodes.FileNotFound, e.getErrorCode());
    }
    // verify that StoreErrorCodes.IOError can be captured when performing memory map
    indexSegment = generateIndexSegment(new Offset(logSegmentName, 0), mockStoreKeyFactory);
    MockId id1 = new MockId("0" + TestUtils.getRandomString(CUSTOM_ID_SIZE - 1));
    IndexValue value1 =
        IndexValueTest.getIndexValue(1000, new Offset(logSegmentName, 0), Utils.Infinite_Time, time.milliseconds(),
            Utils.getRandomShort(TestUtils.RANDOM), Utils.getRandomShort(TestUtils.RANDOM), (short) 0, formatVersion);
    indexSegment.addEntry(new IndexEntry(id1, value1), new Offset(logSegmentName, 1000));
    indexSegment.writeIndexSegmentToFile(new Offset(logSegmentName, 1000));
    doThrow(new IOException(StoreException.IO_ERROR_STR)).when(mockStoreKeyFactory)
        .getStoreKey(any(DataInputStream.class));
    try {
      indexSegment.seal();
      fail("should fail");
    } catch (StoreException e) {
      assertEquals("Mismatch in error code", StoreErrorCodes.IOError, e.getErrorCode());
    }
    // test that when IOException's error message is null, the error code should be Unknown_Error
    doThrow(new IOException()).when(mockStoreKeyFactory).getStoreKey(any(DataInputStream.class));
    try {
      indexSegment.seal();
      fail("should fail");
    } catch (StoreException e) {
      assertEquals("Mismatch in error code", StoreErrorCodes.UnknownError, e.getErrorCode());
    }
  }
Test cases where exceptions occurred while performing memory mapping of index segment.
@throws IOException
@throws StoreException
ambry-store/src/test/java/com/github/ambry/store/IndexSegmentTest.java