mirror of
https://github.com/TuxCoding/FastLogin.git
synced 2026-01-29 09:40:08 +01:00
Migrate tests to use Junit5
This commit is contained in:
@@ -25,132 +25,80 @@
|
||||
*/
|
||||
package com.github.games647.fastlogin.core;
|
||||
|
||||
import com.github.games647.fastlogin.core.antibot.RateLimiter;
|
||||
import com.github.games647.fastlogin.core.antibot.TickingRateLimiter;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TickingRateLimiterTest {
|
||||
class TickingRateLimiterTest {
|
||||
|
||||
/**
|
||||
* Always expired
|
||||
*/
|
||||
@Test
|
||||
public void allowExpire() {
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = {5_000_000L, -5_000_000L})
|
||||
void allowExpire(long initial) {
|
||||
int size = 3;
|
||||
|
||||
FakeTicker ticker = new FakeTicker(5_000_000L);
|
||||
FakeTicker ticker = new FakeTicker(initial);
|
||||
|
||||
// run twice the size to fill it first and then test it
|
||||
TickingRateLimiter rateLimiter = new TickingRateLimiter(ticker, size, 0);
|
||||
RateLimiter rateLimiter = new TickingRateLimiter(ticker, size, 0);
|
||||
for (int i = 0; i < size; i++) {
|
||||
assertThat("Filling up", rateLimiter.tryAcquire(), is(true));
|
||||
assertTrue(rateLimiter.tryAcquire(), "Filling up");
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
ticker.add(Duration.ofSeconds(1));
|
||||
assertThat("Should be expired", rateLimiter.tryAcquire(), is(true));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowExpireNegative() {
|
||||
int size = 3;
|
||||
|
||||
FakeTicker ticker = new FakeTicker(-5_000_000L);
|
||||
|
||||
// run twice the size to fill it first and then test it
|
||||
TickingRateLimiter rateLimiter = new TickingRateLimiter(ticker, size, 0);
|
||||
for (int i = 0; i < size; i++) {
|
||||
assertThat("Filling up", rateLimiter.tryAcquire(), is(true));
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
ticker.add(Duration.ofSeconds(1));
|
||||
assertThat("Should be expired", rateLimiter.tryAcquire(), is(true));
|
||||
assertTrue(rateLimiter.tryAcquire(), "Should be expired");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Too many requests
|
||||
*/
|
||||
@Test
|
||||
public void shouldBlock() {
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = {5_000_000L, -5_000_000L})
|
||||
void shouldBlock(long initial) {
|
||||
int size = 3;
|
||||
|
||||
FakeTicker ticker = new FakeTicker(5_000_000L);
|
||||
FakeTicker ticker = new FakeTicker(initial);
|
||||
|
||||
// fill the size
|
||||
TickingRateLimiter rateLimiter = new TickingRateLimiter(ticker, size, TimeUnit.SECONDS.toMillis(30));
|
||||
RateLimiter rateLimiter = new TickingRateLimiter(ticker, size, TimeUnit.SECONDS.toMillis(30));
|
||||
for (int i = 0; i < size; i++) {
|
||||
assertThat("Filling up", rateLimiter.tryAcquire(), is(true));
|
||||
assertTrue(rateLimiter.tryAcquire(), "Filling up");
|
||||
}
|
||||
|
||||
assertThat("Should be full and no entry should be expired", rateLimiter.tryAcquire(), is(false));
|
||||
assertFalse(rateLimiter.tryAcquire(), "Should be full and no entry should be expired");
|
||||
}
|
||||
|
||||
/**
|
||||
* Too many requests
|
||||
*/
|
||||
@Test
|
||||
public void shouldBlockNegative() {
|
||||
int size = 3;
|
||||
|
||||
FakeTicker ticker = new FakeTicker(-5_000_000L);
|
||||
|
||||
// fill the size
|
||||
TickingRateLimiter rateLimiter = new TickingRateLimiter(ticker, size, TimeUnit.SECONDS.toMillis(30));
|
||||
for (int i = 0; i < size; i++) {
|
||||
assertThat("Filling up", rateLimiter.tryAcquire(), is(true));
|
||||
}
|
||||
|
||||
assertThat("Should be full and no entry should be expired", rateLimiter.tryAcquire(), is(false));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Blocked attempts shouldn't replace existing ones.
|
||||
*/
|
||||
@Test
|
||||
public void blockedNotAdded() {
|
||||
FakeTicker ticker = new FakeTicker(5_000_000L);
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = {5_000_000L, -5_000_000L})
|
||||
void blockedNotAdded(long initial) {
|
||||
FakeTicker ticker = new FakeTicker(initial);
|
||||
|
||||
// fill the size - 100ms should be reasonable high
|
||||
TickingRateLimiter rateLimiter = new TickingRateLimiter(ticker, 1, 100);
|
||||
assertThat("Filling up", rateLimiter.tryAcquire(), is(true));
|
||||
RateLimiter rateLimiter = new TickingRateLimiter(ticker, 1, 100);
|
||||
assertTrue(rateLimiter.tryAcquire(), "Filling up");
|
||||
|
||||
ticker.add(Duration.ofMillis(50));
|
||||
|
||||
// still is full - should fail
|
||||
assertThat("Expired too early", rateLimiter.tryAcquire(), is(false));
|
||||
assertFalse(rateLimiter.tryAcquire(), "Expired too early");
|
||||
|
||||
// wait the remaining time and add a threshold, because
|
||||
ticker.add(Duration.ofMillis(50));
|
||||
assertThat("Request not released", rateLimiter.tryAcquire(), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocked attempts shouldn't replace existing ones.
|
||||
*/
|
||||
@Test
|
||||
public void blockedNotAddedNegative() {
|
||||
FakeTicker ticker = new FakeTicker(-5_000_000L);
|
||||
|
||||
// fill the size - 100ms should be reasonable high
|
||||
TickingRateLimiter rateLimiter = new TickingRateLimiter(ticker, 1, 100);
|
||||
assertThat("Filling up", rateLimiter.tryAcquire(), is(true));
|
||||
|
||||
ticker.add(Duration.ofMillis(50));
|
||||
|
||||
// still is full - should fail
|
||||
assertThat("Expired too early", rateLimiter.tryAcquire(), is(false));
|
||||
|
||||
// wait the remaining time and add a threshold, because
|
||||
ticker.add(Duration.ofMillis(50));
|
||||
assertThat("Request not released", rateLimiter.tryAcquire(), is(true));
|
||||
assertTrue(rateLimiter.tryAcquire(), "Request not released");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user