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));
}
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));
}
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));
}
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());
}
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());
}
Aggregations