Search in sources :

Example 96 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class PagingTraversalTest method shouldRespondWith404WhenTraversalHasExpired.

@Test
public void shouldRespondWith404WhenTraversalHasExpired() {
    Response response = createAPagedTraverser();
    ((FakeClock) leaseManager.getClock()).forward(enoughMinutesToExpireTheTraversal(), TimeUnit.MINUTES);
    String traverserId = parseTraverserIdFromLocationUri(response);
    response = service.pagedTraverse(traverserId, TraverserReturnType.node);
    assertEquals(404, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) FakeClock(org.neo4j.time.FakeClock) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 97 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class PagingTraversalTest method shouldRenewLeaseAtEachTraversal.

@Test
public void shouldRenewLeaseAtEachTraversal() {
    Response response = createAPagedTraverser();
    String traverserId = parseTraverserIdFromLocationUri(response);
    ((FakeClock) leaseManager.getClock()).forward(30, TimeUnit.SECONDS);
    response = service.pagedTraverse(traverserId, TraverserReturnType.node);
    assertEquals(200, response.getStatus());
    ((FakeClock) leaseManager.getClock()).forward(30, TimeUnit.SECONDS);
    response = service.pagedTraverse(traverserId, TraverserReturnType.node);
    assertEquals(200, response.getStatus());
    ((FakeClock) leaseManager.getClock()).forward(this.enoughMinutesToExpireTheTraversal(), TimeUnit.MINUTES);
    response = service.pagedTraverse(traverserId, TraverserReturnType.node);
    assertEquals(404, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) FakeClock(org.neo4j.time.FakeClock) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 98 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class CountsTrackerTest method shouldNotEndUpInBrokenStateAfterRotationFailure.

@Test
@Resources.Life(STARTED)
public void shouldNotEndUpInBrokenStateAfterRotationFailure() throws Exception {
    // GIVEN
    FakeClock clock = Clocks.fakeClock();
    CountsTracker tracker = resourceManager.managed(newTracker(clock));
    int labelId = 1;
    try (CountsAccessor.Updater tx = tracker.apply(2).get()) {
        // now at 1
        tx.incrementNodeCount(labelId, 1);
    }
    // WHEN
    Predicate<Thread> arrived = thread -> stackTraceContains(thread, all(classNameContains("Rotation"), methodIs("rotate")));
    Future<Object> rotation = threading.executeAndAwait(t -> t.rotate(4), tracker, arrived, 100, MILLISECONDS);
    try (CountsAccessor.Updater tx = tracker.apply(3).get()) {
        // now at 2
        tx.incrementNodeCount(labelId, 1);
    }
    clock.forward(Config.empty().get(GraphDatabaseSettings.counts_store_rotation_timeout) * 2, MILLISECONDS);
    try {
        rotation.get();
        fail("Should've failed rotation due to timeout");
    } catch (ExecutionException e) {
        // good
        assertTrue(e.getCause() instanceof RotationTimeoutException);
    }
    // THEN
    Register.DoubleLongRegister register = Registers.newDoubleLongRegister();
    tracker.get(CountsKeyFactory.nodeKey(labelId), register);
    assertEquals(2, register.readSecond());
    // and WHEN later attempting rotation again
    try (CountsAccessor.Updater tx = tracker.apply(4).get()) {
        // now at 3
        tx.incrementNodeCount(labelId, 1);
    }
    tracker.rotate(4);
    // THEN
    tracker.get(CountsKeyFactory.nodeKey(labelId), register);
    assertEquals(3, register.readSecond());
}
Also used : CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) Registers(org.neo4j.register.Registers) Assert.assertSame(org.junit.Assert.assertSame) Future(java.util.concurrent.Future) FakeClock(org.neo4j.time.FakeClock) CountsVisitor(org.neo4j.kernel.impl.api.CountsVisitor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) CountsKeyFactory(org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory) DebugUtil.classNameContains(org.neo4j.kernel.impl.util.DebugUtil.classNameContains) Assert.fail(org.junit.Assert.fail) ReadableBuffer(org.neo4j.kernel.impl.store.kvstore.ReadableBuffer) STARTED(org.neo4j.test.rule.Resources.InitialLifecycle.STARTED) Config(org.neo4j.kernel.configuration.Config) CountsOracle(org.neo4j.kernel.impl.store.CountsOracle) Barrier(org.neo4j.test.Barrier) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Predicate(java.util.function.Predicate) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Register(org.neo4j.register.Register) IOFunction(org.neo4j.function.IOFunction) ThrowingFunction(org.neo4j.function.ThrowingFunction) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) DataInitializer(org.neo4j.kernel.impl.store.kvstore.DataInitializer) FILE_IN_EXISTING_DIRECTORY(org.neo4j.test.rule.Resources.TestPath.FILE_IN_EXISTING_DIRECTORY) DebugUtil.stackTraceContains(org.neo4j.kernel.impl.util.DebugUtil.stackTraceContains) RotationTimeoutException(org.neo4j.kernel.impl.store.kvstore.RotationTimeoutException) Rule(org.junit.Rule) CountsAccessor(org.neo4j.kernel.impl.api.CountsAccessor) Resources(org.neo4j.test.rule.Resources) ThreadingRule(org.neo4j.test.rule.concurrent.ThreadingRule) Clock(java.time.Clock) DebugUtil.methodIs(org.neo4j.kernel.impl.util.DebugUtil.methodIs) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Clocks(org.neo4j.time.Clocks) Assert.assertEquals(org.junit.Assert.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Predicates.all(org.neo4j.function.Predicates.all) Mockito.mock(org.mockito.Mockito.mock) FakeClock(org.neo4j.time.FakeClock) CountsAccessor(org.neo4j.kernel.impl.api.CountsAccessor) Register(org.neo4j.register.Register) RotationTimeoutException(org.neo4j.kernel.impl.store.kvstore.RotationTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 99 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class BufferingIdGeneratorFactoryTest method shouldDelayFreeingOfAggressivelyReusedIdsConsideringTimeAsWell.

@Test
public void shouldDelayFreeingOfAggressivelyReusedIdsConsideringTimeAsWell() throws Exception {
    // GIVEN
    MockedIdGeneratorFactory actual = new MockedIdGeneratorFactory();
    final FakeClock clock = Clocks.fakeClock();
    final long safeZone = MINUTES.toMillis(1);
    ControllableSnapshotSupplier boundaries = new ControllableSnapshotSupplier();
    BufferingIdGeneratorFactory bufferingIdGeneratorFactory = new BufferingIdGeneratorFactory(actual, boundaries, t -> clock.millis() - t.snapshotTime() >= safeZone, new CommunityIdTypeConfigurationProvider());
    IdGenerator idGenerator = bufferingIdGeneratorFactory.open(new File("doesnt-matter"), 10, IdType.STRING_BLOCK, 0, Integer.MAX_VALUE);
    // WHEN
    idGenerator.freeId(7);
    verifyNoMoreInteractions(actual.get(IdType.STRING_BLOCK));
    // after some maintenance and transaction still not closed
    bufferingIdGeneratorFactory.maintenance();
    verifyNoMoreInteractions(actual.get(IdType.STRING_BLOCK));
    // although after transactions have all closed
    boundaries.setMostRecentlyReturnedSnapshotToAllClosed();
    bufferingIdGeneratorFactory.maintenance();
    // ... the clock would still say "nope" so no interaction
    verifyNoMoreInteractions(actual.get(IdType.STRING_BLOCK));
    // then finally after time has passed as well
    clock.forward(70, SECONDS);
    bufferingIdGeneratorFactory.maintenance();
    // THEN
    verify(actual.get(IdType.STRING_BLOCK)).freeId(7);
}
Also used : CommunityIdTypeConfigurationProvider(org.neo4j.kernel.impl.store.id.configuration.CommunityIdTypeConfigurationProvider) FakeClock(org.neo4j.time.FakeClock) File(java.io.File) Test(org.junit.Test)

Example 100 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class TransportWriteThrottleTest method shouldAllocateMemoryUponInstall.

@Test
void shouldAllocateMemoryUponInstall() throws InterruptedException {
    var lock = newThrottleLockMock();
    FakeClock clock = Clocks.fakeClock(1, TimeUnit.SECONDS);
    var throttle = newThrottle(lock, clock, Duration.ofSeconds(5));
    var memoryTracker = mock(MemoryTracker.class);
    throttle.install(channel, memoryTracker);
    verify(memoryTracker).allocateHeap(anyLong());
}
Also used : FakeClock(org.neo4j.time.FakeClock) Test(org.junit.jupiter.api.Test)

Aggregations

FakeClock (org.neo4j.time.FakeClock)111 Test (org.junit.Test)82 Test (org.junit.jupiter.api.Test)21 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)20 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)17 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)16 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)16 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)9 QueryLogger (org.neo4j.kernel.impl.query.QueryLoggerKernelExtension.QueryLogger)9 User (org.neo4j.kernel.impl.security.User)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)7 Channel (org.jboss.netty.channel.Channel)5 Path (java.nio.file.Path)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Response (javax.ws.rs.core.Response)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 InputStream (java.io.InputStream)3 ExecutionException (java.util.concurrent.ExecutionException)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3