Search in sources :

Example 31 with FakeClock

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

the class RateLimitedAuthenticationStrategyTest method shouldSlowRequestRateOnMultipleFailedAttemptsWhereAttemptIsValid.

@Test
public void shouldSlowRequestRateOnMultipleFailedAttemptsWhereAttemptIsValid() throws Exception {
    // Given
    FakeClock clock = getFakeClock();
    AuthenticationStrategy authStrategy = new RateLimitedAuthenticationStrategy(clock, 3);
    User user = new User.Builder("user", Credential.forPassword("right")).build();
    // When we've failed three times
    authStrategy.authenticate(user, "wrong");
    authStrategy.authenticate(user, "wrong");
    authStrategy.authenticate(user, "wrong");
    // Then
    assertThat(authStrategy.authenticate(user, "right"), equalTo(AuthenticationResult.TOO_MANY_ATTEMPTS));
    // But when time heals all wounds
    clock.forward(5, TimeUnit.SECONDS);
    // Then things should be alright
    assertThat(authStrategy.authenticate(user, "right"), equalTo(AuthenticationResult.SUCCESS));
}
Also used : User(org.neo4j.kernel.impl.security.User) FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 32 with FakeClock

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

the class RateLimitedAuthenticationStrategyTest method shouldNotSlowRequestRateOnLessThanMaxFailedAttempts.

@Test
public void shouldNotSlowRequestRateOnLessThanMaxFailedAttempts() throws Exception {
    // Given
    FakeClock clock = getFakeClock();
    AuthenticationStrategy authStrategy = new RateLimitedAuthenticationStrategy(clock, 3);
    User user = new User.Builder("user", Credential.forPassword("right")).build();
    // When we've failed two times
    assertThat(authStrategy.authenticate(user, "wrong"), equalTo(AuthenticationResult.FAILURE));
    assertThat(authStrategy.authenticate(user, "wrong"), equalTo(AuthenticationResult.FAILURE));
    // Then
    assertThat(authStrategy.authenticate(user, "right"), equalTo(AuthenticationResult.SUCCESS));
}
Also used : User(org.neo4j.kernel.impl.security.User) FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 33 with FakeClock

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

the class RateLimitedAuthenticationStrategyTest method shouldSlowRequestRateOnMultipleFailedAttempts.

@Test
public void shouldSlowRequestRateOnMultipleFailedAttempts() throws Exception {
    // Given
    FakeClock clock = getFakeClock();
    AuthenticationStrategy authStrategy = new RateLimitedAuthenticationStrategy(clock, 3);
    User user = new User.Builder("user", Credential.forPassword("right")).build();
    // When we've failed three times
    assertThat(authStrategy.authenticate(user, "wrong"), equalTo(AuthenticationResult.FAILURE));
    assertThat(authStrategy.authenticate(user, "wrong"), equalTo(AuthenticationResult.FAILURE));
    assertThat(authStrategy.authenticate(user, "wrong"), equalTo(AuthenticationResult.FAILURE));
    // Then
    assertThat(authStrategy.authenticate(user, "wrong"), equalTo(AuthenticationResult.TOO_MANY_ATTEMPTS));
    // But when time heals all wounds
    clock.forward(5, TimeUnit.SECONDS);
    // Then things should be alright
    assertThat(authStrategy.authenticate(user, "wrong"), equalTo(AuthenticationResult.FAILURE));
}
Also used : User(org.neo4j.kernel.impl.security.User) FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 34 with FakeClock

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

the class ResourcePoolTest method shouldBuildUpGracefullyUntilReachedMinPoolSize.

@Test
public void shouldBuildUpGracefullyUntilReachedMinPoolSize() throws InterruptedException {
    // GIVEN
    StatefulMonitor stateMonitor = new StatefulMonitor();
    FakeClock clock = getFakeClocks();
    final ResourcePool<Something> pool = getResourcePool(stateMonitor, clock, 5);
    // WHEN
    acquireFromPool(pool, 5);
    // THEN
    assertEquals(-1, stateMonitor.currentPeakSize.get());
    // that means the target size was not updated
    assertEquals(-1, stateMonitor.targetSize.get());
    // no disposed happened, since the count to update is 10
    assertEquals(0, stateMonitor.disposed.get());
}
Also used : FakeClock(org.neo4j.time.FakeClock) Test(org.junit.Test)

Example 35 with FakeClock

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

the class ResourcePoolTest method shouldReclaimAndRecreateWhenUsageGoesDownBetweenSpikes.

@Test
public void shouldReclaimAndRecreateWhenUsageGoesDownBetweenSpikes() throws Exception {
    // given
    final int poolMinSize = 50;
    final int bellowPoolMinSize = poolMinSize / 5;
    final int poolMaxSize = 200;
    StatefulMonitor stateMonitor = new StatefulMonitor();
    FakeClock clock = getFakeClocks();
    final SomethingResourcePool pool = getResourcePool(stateMonitor, clock, poolMinSize);
    acquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);
    // when
    // After the peak, stay well below concurrent usage, using up all already present resources in the process
    exceedTimeout(clock);
    // not always result in reaping of resources, as there is reuse
    for (int i = 0; i < 30; i++) {
        // The latch is necessary to reduce races between batches
        CountDownLatch release = new CountDownLatch(bellowPoolMinSize);
        for (ResourceHolder holder : acquireFromPool(pool, bellowPoolMinSize)) {
            holder.release(release);
        }
        release.await();
        exceedTimeout(clock);
    }
    // then
    // currentPeakSize should not be higher than bellowPoolMinSize
    assertTrue(String.valueOf(stateMonitor.currentPeakSize.get()), stateMonitor.currentPeakSize.get() <= bellowPoolMinSize);
    // target size should remain at pool min size
    assertEquals(poolMinSize, stateMonitor.targetSize.get());
    assertEquals(poolMinSize, pool.unusedSize());
    // only the excess from the pool max size down to min size must have been disposed
    // +1 that was used to trigger initial exceed timeout check
    assertEquals(poolMaxSize - poolMinSize + 1, stateMonitor.disposed.get());
    stateMonitor.created.set(0);
    stateMonitor.disposed.set(0);
    // when
    // After the lull, recreate a peak
    acquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);
    // then
    assertEquals(poolMaxSize - poolMinSize + 1, stateMonitor.created.get());
    assertEquals(0, stateMonitor.disposed.get());
}
Also used : FakeClock(org.neo4j.time.FakeClock) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

FakeClock (org.neo4j.time.FakeClock)87 Test (org.junit.Test)85 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 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)14 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)9 QueryLogger (org.neo4j.kernel.impl.query.QueryLoggerKernelExtension.QueryLogger)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)7 Channel (org.jboss.netty.channel.Channel)5 User (org.neo4j.kernel.impl.security.User)5 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 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)3 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)3 File (java.io.File)2 Clock (java.time.Clock)2