Add test coverage for core server components: annotation scanning, routing, rate limiting, CORS, and JSON handling
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package dev.coph.nextusweb.server.ratelimit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class FixedWindowLimiterTest {
|
||||
|
||||
@Test
|
||||
void allowsUpToLimitThenDenies() {
|
||||
FixedWindowLimiter lim = new FixedWindowLimiter(3, 1000);
|
||||
assertTrue(lim.tryAcquire("k", 0).allowed());
|
||||
assertTrue(lim.tryAcquire("k", 0).allowed());
|
||||
assertTrue(lim.tryAcquire("k", 0).allowed());
|
||||
RateLimiter.Result r = lim.tryAcquire("k", 0);
|
||||
assertFalse(r.allowed());
|
||||
assertEquals(3, r.limit());
|
||||
assertTrue(r.retryAfterMillis() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void newWindowResetsCount() {
|
||||
FixedWindowLimiter lim = new FixedWindowLimiter(1, 100);
|
||||
assertTrue(lim.tryAcquire("k", 0).allowed());
|
||||
assertFalse(lim.tryAcquire("k", 0).allowed());
|
||||
long windowNs = 100L * 1_000_000L;
|
||||
assertTrue(lim.tryAcquire("k", windowNs).allowed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void differentKeysAreIndependent() {
|
||||
FixedWindowLimiter lim = new FixedWindowLimiter(1, 1000);
|
||||
assertTrue(lim.tryAcquire("a", 0).allowed());
|
||||
assertTrue(lim.tryAcquire("b", 0).allowed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cleanupDoesNotThrow() {
|
||||
FixedWindowLimiter lim = new FixedWindowLimiter(1, 1000);
|
||||
lim.tryAcquire("k", System.nanoTime());
|
||||
assertDoesNotThrow(() -> lim.cleanup(0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user