@Test
  public void closeIndexToCancelPersistorTest() throws StoreException, InterruptedException {
    long SCHEDULER_PERIOD_MS = 10;
    state.index.close(false);
    // re-initialize index by using mock scheduler (the intention is to speed up testing by using shorter period)
    ScheduledThreadPoolExecutor scheduler = (ScheduledThreadPoolExecutor) Utils.newScheduler(1, false);
    ScheduledThreadPoolExecutor mockScheduler = Mockito.spy(scheduler);
    AtomicReference<ScheduledFuture> persistorTask = new AtomicReference<>();
    class MockPersistor implements Runnable {
      private CountDownLatch invokeCountDown;

      @Override
      public void run() {
        invokeCountDown.countDown();
      }
    }
    MockPersistor mockPersistor = new MockPersistor();
    mockPersistor.invokeCountDown = new CountDownLatch(1);
    doAnswer(invocation -> {
      persistorTask.set(
          mockScheduler.scheduleAtFixedRate(mockPersistor, 0, SCHEDULER_PERIOD_MS, TimeUnit.MILLISECONDS));
      return persistorTask.get();
    }).when(mockScheduler)
        .scheduleAtFixedRate(any(PersistentIndex.IndexPersistor.class), anyLong(), anyLong(),
            any(TimeUnit.SECONDS.getClass()));
    state.initIndex(mockScheduler);
    // verify that the persistor task is successfully scheduled
    assertTrue("The persistor task wasn't invoked within the expected time",
        mockPersistor.invokeCountDown.await(2 * SCHEDULER_PERIOD_MS, TimeUnit.MILLISECONDS));
    state.index.close(false);
    mockPersistor.invokeCountDown = new CountDownLatch(1);
    // verify that the persisitor task is canceled after index closed and is never invoked again.
    assertTrue("The persistor task should be canceled after index closed", persistorTask.get().isCancelled());
    assertFalse("The persistor task should not be invoked again",
        mockPersistor.invokeCountDown.await(2 * SCHEDULER_PERIOD_MS, TimeUnit.MILLISECONDS));
  }
Tests {@link PersistentIndex#close(boolean)} can correctly cancel the scheduled persistor task and makes sure no persistor
is running background after index closed.
@throws StoreException
@throws InterruptedException
ambry-store/src/test/java/com/github/ambry/store/IndexTest.java