mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 03:37:34 -08:00
Improve DoS test case to be more robust to timing
This commit is contained in:
parent
d907537c05
commit
9065c4daee
|
@ -31,26 +31,30 @@ public final class DoSTrackerTestCase extends Assert {
|
|||
Timer timer = new Timer();
|
||||
long timerTimeMS = 200;
|
||||
DoSTracker tracker = new DoSTracker(timer, 2, timerTimeMS, 3);
|
||||
|
||||
// 2 requests allowed per time; 3rd should be banned
|
||||
assertFalse(tracker.isBanned("A"));
|
||||
assertFalse(tracker.isBanned("A"));
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
|
||||
// After max 3 others are tracked, A should be reset/evicted and un-ban
|
||||
assertFalse(tracker.isBanned("B"));
|
||||
assertFalse(tracker.isBanned("C"));
|
||||
assertFalse(tracker.isBanned("D"));
|
||||
assertFalse(tracker.isBanned("A"));
|
||||
|
||||
// After building up a ban again, and letting plenty of time elapse, should un-ban
|
||||
assertFalse(tracker.isBanned("A"));
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
Thread.sleep(timerTimeMS * 3);
|
||||
assertFalse(tracker.isBanned("A"));
|
||||
|
||||
assertFalse(tracker.isBanned("A"));
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
// Build up a lot of hits
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
}
|
||||
// After one interval, should still have enough hits to be banned
|
||||
Thread.sleep(timerTimeMS * 2);
|
||||
assertTrue(tracker.isBanned("A"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue