@Test
  public void shouldStillReportInternalIndexStateAsPopulatingWhenConstraintIndexIsDonePopulating()
      throws Exception {
    // given
    when(accessor.newUpdater(any(IndexUpdateMode.class))).thenReturn(updater);

    IndexingService indexingService =
        newIndexingServiceWithMockedDependencies(populator, accessor, withData());

    life.start();

    // when
    indexingService.createIndex(
        IndexRule.constraintIndexRule(0, labelId, propertyKeyId, PROVIDER_DESCRIPTOR, null));
    IndexProxy proxy = indexingService.getProxyForRule(0);

    verify(populator, timeout(1000)).close(true);

    try (IndexUpdater updater = proxy.newUpdater(IndexUpdateMode.ONLINE)) {
      updater.process(add(10, "foo"));
    }

    // then
    assertEquals(InternalIndexState.POPULATING, proxy.getState());
    InOrder order = inOrder(populator, accessor, updater);
    order.verify(populator).create();
    order.verify(populator).close(true);
    order.verify(accessor).newUpdater(IndexUpdateMode.ONLINE);
    order.verify(updater).process(add(10, "foo"));
    order.verify(updater).close();
  }
  @Test
  public void shouldBringIndexOnlineAndFlipOverToIndexAccessor() throws Exception {
    // given
    when(accessor.newUpdater(any(IndexUpdateMode.class))).thenReturn(updater);

    IndexingService indexingService =
        newIndexingServiceWithMockedDependencies(populator, accessor, withData());

    life.start();

    // when
    indexingService.createIndex(indexRule(0, labelId, propertyKeyId, PROVIDER_DESCRIPTOR));
    IndexProxy proxy = indexingService.getProxyForRule(0);

    verify(populator, timeout(1000)).close(true);

    try (IndexUpdater updater = proxy.newUpdater(IndexUpdateMode.ONLINE)) {
      updater.process(add(10, "foo"));
    }

    // then
    assertEquals(InternalIndexState.ONLINE, proxy.getState());
    InOrder order = inOrder(populator, accessor, updater);
    order.verify(populator).create();
    order.verify(populator).close(true);
    order.verify(accessor).newUpdater(IndexUpdateMode.ONLINE);
    order.verify(updater).process(add(10, "foo"));
    order.verify(updater).close();
  }
  @Test
  public void shouldDeliverUpdatesThatOccurDuringPopulationToPopulator() throws Exception {
    // given
    when(populator.newPopulatingUpdater()).thenReturn(updater);

    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(afterAwaiting(latch)).when(populator).add(anyLong(), any());

    IndexingService indexingService =
        newIndexingServiceWithMockedDependencies(populator, accessor, withData(add(1, "value1")));

    life.start();

    // when
    indexingService.createIndex(indexRule(0, labelId, propertyKeyId, PROVIDER_DESCRIPTOR));
    IndexProxy proxy = indexingService.getProxyForRule(0);
    assertEquals(InternalIndexState.POPULATING, proxy.getState());

    try (IndexUpdater updater = proxy.newUpdater(IndexUpdateMode.ONLINE)) {
      updater.process(add(2, "value2"));
    }

    latch.countDown();

    verify(populator, timeout(1000)).close(true);

    // then
    assertEquals(InternalIndexState.ONLINE, proxy.getState());
    InOrder order = inOrder(populator, accessor, updater);
    order.verify(populator).create();
    order.verify(populator).add(1, "value1");

    // this is invoked from indexAllNodes(),
    // empty because the id we added (2) is bigger than the one we indexed (1)
    order.verify(populator).newPopulatingUpdater();
    order.verify(updater).close();

    order.verify(populator).newPopulatingUpdater();
    order.verify(updater).process(add(2, "value2"));
    order.verify(updater).close();

    order.verify(populator).close(true);
    verifyNoMoreInteractions(updater);
    verifyNoMoreInteractions(populator);
    verifyZeroInteractions(accessor);
  }