Search in sources :

Example 41 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class CheckPointSchedulerTest method shouldWaitOnStopUntilTheRunningCheckpointIsDone.

@Test
public void shouldWaitOnStopUntilTheRunningCheckpointIsDone() throws Throwable {
    // given
    final AtomicReference<Throwable> ex = new AtomicReference<>();
    final AtomicBoolean stoppedCompleted = new AtomicBoolean();
    final DoubleLatch checkPointerLatch = new DoubleLatch(1);
    CheckPointer checkPointer = new CheckPointer() {

        @Override
        public long checkPointIfNeeded(TriggerInfo triggerInfo) throws IOException {
            checkPointerLatch.startAndWaitForAllToStart();
            checkPointerLatch.waitForAllToFinish();
            return 42;
        }

        @Override
        public long tryCheckPoint(TriggerInfo triggerInfo) throws IOException {
            throw new RuntimeException("this should have not been called");
        }

        @Override
        public long forceCheckPoint(TriggerInfo triggerInfo) throws IOException {
            throw new RuntimeException("this should have not been called");
        }

        @Override
        public long lastCheckPointedTransactionId() {
            return 42;
        }
    };
    final CheckPointScheduler scheduler = new CheckPointScheduler(checkPointer, jobScheduler, 20L, health);
    // when
    scheduler.start();
    Thread runCheckPointer = new Thread() {

        @Override
        public void run() {
            jobScheduler.runJob();
        }
    };
    runCheckPointer.start();
    checkPointerLatch.waitForAllToStart();
    Thread stopper = new Thread() {

        @Override
        public void run() {
            try {
                scheduler.stop();
                stoppedCompleted.set(true);
            } catch (Throwable throwable) {
                ex.set(throwable);
            }
        }
    };
    stopper.start();
    Thread.sleep(10);
    // then
    assertFalse(stoppedCompleted.get());
    checkPointerLatch.finish();
    runCheckPointer.join();
    Thread.sleep(150);
    assertTrue(stoppedCompleted.get());
    // just in case
    stopper.join();
    assertNull(ex.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 42 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class IndexRestartIT method shouldBeAbleToDropIndexWhileItIsPopulating.

/* This is somewhat difficult to test since dropping an index while it's populating forces it to be cancelled
     * first (and also awaiting cancellation to complete). So this is a best-effort to have the timing as close
     * as possible. If this proves to be flaky, remove it right away.
     */
@Test
public void shouldBeAbleToDropIndexWhileItIsPopulating() throws Exception {
    // GIVEN
    startDb();
    DoubleLatch populationCompletionLatch = provider.installPopulationJobCompletionLatch();
    IndexDefinition index = createIndex();
    // await population job to start
    populationCompletionLatch.waitForAllToStart();
    // WHEN
    dropIndex(index, populationCompletionLatch);
    // THEN
    assertThat(getIndexes(db, myLabel), inTx(db, hasSize(0)));
    try {
        getIndexState(db, index);
        fail("This index should have been deleted");
    } catch (NotFoundException e) {
        assertThat(e.getMessage(), CoreMatchers.containsString(myLabel.name()));
    }
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) NotFoundException(org.neo4j.graphdb.NotFoundException) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 43 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class ContractCheckingIndexProxyTest method shouldNotCloseWhileCreating.

@Test(expected = /* THEN */
IllegalStateException.class)
public void shouldNotCloseWhileCreating() throws IOException {
    // GIVEN
    final DoubleLatch latch = new DoubleLatch();
    final IndexProxy inner = new IndexProxyAdapter() {

        @Override
        public void start() {
            latch.startAndWaitForAllToStartAndFinish();
        }
    };
    final IndexProxy outer = newContractCheckingIndexProxy(inner);
    // WHEN
    runInSeparateThread(() -> outer.start());
    try {
        latch.waitForAllToStart();
        outer.close();
    } finally {
        latch.finish();
    }
}
Also used : SchemaIndexTestHelper.mockIndexProxy(org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 44 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class ContractCheckingIndexProxyTest method shouldNotCloseWhileUpdating.

@Test(expected = /* THEN */
IllegalStateException.class)
public void shouldNotCloseWhileUpdating() throws IOException {
    // GIVEN
    final DoubleLatch latch = new DoubleLatch();
    final IndexProxy inner = new IndexProxyAdapter() {

        @Override
        public IndexUpdater newUpdater(IndexUpdateMode mode) {
            return super.newUpdater(mode);
        }
    };
    final IndexProxy outer = newContractCheckingIndexProxy(inner);
    outer.start();
    // WHEN
    runInSeparateThread(() -> {
        try (IndexUpdater updater = outer.newUpdater(IndexUpdateMode.ONLINE)) {
            updater.process(null);
            latch.startAndWaitForAllToStartAndFinish();
        } catch (IndexEntryConflictException e) {
            throw new RuntimeException(e);
        }
    });
    try {
        latch.waitForAllToStart();
        outer.close();
    } finally {
        latch.finish();
    }
}
Also used : SchemaIndexTestHelper.mockIndexProxy(org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy) DoubleLatch(org.neo4j.test.DoubleLatch) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) Test(org.junit.Test)

Example 45 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class ContractCheckingIndexProxyTest method shouldNotDropWhileCreating.

@Test(expected = /* THEN */
IllegalStateException.class)
public void shouldNotDropWhileCreating() throws IOException {
    // GIVEN
    final DoubleLatch latch = new DoubleLatch();
    final IndexProxy inner = new IndexProxyAdapter() {

        @Override
        public void start() {
            latch.startAndWaitForAllToStartAndFinish();
        }
    };
    final IndexProxy outer = newContractCheckingIndexProxy(inner);
    // WHEN
    runInSeparateThread(() -> outer.start());
    try {
        latch.waitForAllToStart();
        outer.drop();
    } finally {
        latch.finish();
    }
}
Also used : SchemaIndexTestHelper.mockIndexProxy(org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Aggregations

DoubleLatch (org.neo4j.test.DoubleLatch)48 Test (org.junit.Test)37 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 Collections.emptyMap (java.util.Collections.emptyMap)9 Map (java.util.Map)9 MapUtil.stringMap (org.neo4j.helpers.collection.MapUtil.stringMap)9 OffsetDateTime (java.time.OffsetDateTime)5 JobScheduler (org.neo4j.kernel.impl.util.JobScheduler)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Server (org.eclipse.jetty.server.Server)3 SchemaIndexTestHelper.mockIndexProxy (org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy)3 Neo4jJobScheduler (org.neo4j.kernel.impl.util.Neo4jJobScheduler)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.never (org.mockito.Mockito.never)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2