@Test
  public void addRequiredSegmentsFailureTest() throws Exception {
    requiredSwapSegmentsPerSize = 2;
    alloc = constructAllocator();
    alloc.initializePool(Collections.singletonList(new DiskSpaceRequirements(storeId0, 50, 1, 0)));
    verifyPoolState(new ExpectedState().addStoreSeg(storeId0, 50, 1).addSwapSeg(50, 2));
    assertEquals("Mismatch in number of swap segments", 2L, (long) alloc.getSwapSegmentBySizeMap().get(50L));
    // create disk space requirements for store1
    DiskSpaceRequirements requirements1 = new DiskSpaceRequirements(storeId1, 20L, 3L, 1L);
    Map<String, Map<Long, Long>> overallRequirements =
        alloc.getOverallRequirements(Collections.singletonList(requirements1));
    assertEquals("Mismatch in number of swap segments", 2L, (long) alloc.getSwapSegmentBySizeMap().get(20L));

    File swapReserveDir = new File(reserveFileDir, DiskSpaceAllocator.SWAP_DIR_NAME);
    File swapSegSizeDir = new File(swapReserveDir, DiskSpaceAllocator.FILE_SIZE_DIR_PREFIX + 20);
    // create a file there to trigger add segments failure
    assertTrue("Cannot create file", swapSegSizeDir.createNewFile());
    try {
      alloc.addRequiredSegments(overallRequirements, false);
      fail("should fail");
    } catch (IOException e) {
      //expected
    }
    assertEquals("Mismatch in number of swap segments", 1L, (long) alloc.getSwapSegmentBySizeMap().get(20L));
  }
Test that failure occurs when adding required segments for new store. SwapSegmentBySize map is supposed to reduce
number of swap segments (including both swap segments in use and those available in pool)
@throws Exception
ambry-store/src/test/java/com/github/ambry/store/DiskSpaceAllocatorTest.java