@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 shouldNotSnapshotPopulatingIndexes() throws Exception {
    // GIVEN
    CountDownLatch populatorLatch = new CountDownLatch(1);
    IndexAccessor indexAccessor = mock(IndexAccessor.class);
    IndexingService indexing =
        newIndexingServiceWithMockedDependencies(
            populator, indexAccessor, new DataUpdates(new NodePropertyUpdate[0]));
    int indexId = 1;
    int indexId2 = 2;
    File theFile = new File("Blah");

    IndexRule rule1 = indexRule(indexId, 2, 3, PROVIDER_DESCRIPTOR);
    IndexRule rule2 = indexRule(indexId2, 4, 5, PROVIDER_DESCRIPTOR);

    doAnswer(waitForLatch(populatorLatch)).when(populator).create();
    when(indexAccessor.snapshotFiles()).thenAnswer(newResourceIterator(theFile));
    when(indexProvider.getInitialState(indexId)).thenReturn(POPULATING);
    when(indexProvider.getInitialState(indexId2)).thenReturn(ONLINE);

    indexing.initIndexes(iterator(rule1, rule2));
    life.start();

    // WHEN
    ResourceIterator<File> files = indexing.snapshotStoreFiles();
    populatorLatch
        .countDown(); // only now, after the snapshot, is the population job allowed to finish

    // THEN
    // We get a snapshot from the online index, but no snapshot from the populating one
    assertThat(asCollection(files), equalTo(asCollection(iterator(theFile))));
  }
  @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 shouldSnapshotOnlineIndexes() throws Exception {
    // GIVEN
    IndexAccessor indexAccessor = mock(IndexAccessor.class);
    IndexingService indexing =
        newIndexingServiceWithMockedDependencies(
            mock(IndexPopulator.class), indexAccessor, new DataUpdates(new NodePropertyUpdate[0]));
    int indexId = 1;
    int indexId2 = 2;
    File theFile = new File("Blah");

    IndexRule rule1 = indexRule(indexId, 2, 3, PROVIDER_DESCRIPTOR);
    IndexRule rule2 = indexRule(indexId2, 4, 5, PROVIDER_DESCRIPTOR);

    when(indexAccessor.snapshotFiles()).thenAnswer(newResourceIterator(theFile));
    when(indexProvider.getInitialState(indexId)).thenReturn(ONLINE);
    when(indexProvider.getInitialState(indexId2)).thenReturn(ONLINE);

    indexing.initIndexes(iterator(rule1, rule2));
    life.start();

    // WHEN
    ResourceIterator<File> files = indexing.snapshotStoreFiles();

    // THEN
    // We get a snapshot per online index
    assertThat(asCollection(files), equalTo(asCollection(iterator(theFile, theFile))));
  }
 private ClusterManager.ManagedCluster startClusterSeededWith(File seedDir) {
   ClusterManager.ManagedCluster cluster =
       life.add(new ClusterManager.Builder(targetDir.directory()).withSeedDir(seedDir).build())
           .getDefaultCluster();
   cluster.await(allSeesAllAsAvailable());
   return cluster;
 }
  private IndexingService newIndexingServiceWithMockedDependencies(
      IndexPopulator populator, IndexAccessor accessor, DataUpdates data) throws IOException {
    StringLogger logger = mock(StringLogger.class);
    indexProvider = mock(SchemaIndexProvider.class);
    IndexStoreView storeView = mock(IndexStoreView.class);
    UpdateableSchemaState schemaState = mock(UpdateableSchemaState.class);

    when(indexProvider.getProviderDescriptor()).thenReturn(PROVIDER_DESCRIPTOR);
    when(indexProvider.getPopulator(anyLong(), any(IndexConfiguration.class)))
        .thenReturn(populator);
    data.getsProcessedByStoreScanFrom(storeView);
    when(indexProvider.getOnlineAccessor(anyLong(), any(IndexConfiguration.class)))
        .thenReturn(accessor);

    return life.add(
        new IndexingService(
            life.add(new Neo4jJobScheduler(logger)),
            new DefaultSchemaIndexProviderMap(indexProvider),
            storeView,
            mock(TokenNameLookup.class),
            schemaState,
            mockLogging(logger)));
  }
  @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);
  }
  @Test
  public void indexCreationShouldBeIdempotent() throws Exception {
    // given
    when(accessor.newUpdater(any(IndexUpdateMode.class))).thenReturn(updater);

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

    life.start();

    // when
    indexingService.createIndex(
        IndexRule.indexRule(0, labelId, propertyKeyId, PROVIDER_DESCRIPTOR));
    indexingService.createIndex(
        IndexRule.indexRule(0, labelId, propertyKeyId, PROVIDER_DESCRIPTOR));

    // We are asserting that the second call to createIndex does not throw an exception.
  }
  @Test
  public void shouldBringConstraintIndexOnlineWhenExplicitlyToldTo() throws Exception {
    // given
    IndexingService indexingService =
        newIndexingServiceWithMockedDependencies(populator, accessor, withData());

    life.start();

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

    indexingService.activateIndex(0);

    // then
    assertEquals(ONLINE, proxy.getState());
    InOrder order = inOrder(populator, accessor);
    order.verify(populator).create();
    order.verify(populator).close(true);
  }
 @Before
 public void startLife() {
   life.start();
 }