|
| 1 | +package org.jitsi.utils |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.ShouldSpec |
| 4 | +import io.kotest.matchers.shouldBe |
| 5 | +import org.jitsi.utils.time.FakeClock |
| 6 | + |
| 7 | +class RateLimitTest : ShouldSpec() { |
| 8 | + init { |
| 9 | + context("RateLimit test") { |
| 10 | + val clock = FakeClock() |
| 11 | + val rateLimit = RateLimit(clock = clock) |
| 12 | + |
| 13 | + should("allow 1st request") { |
| 14 | + rateLimit.accept() shouldBe true |
| 15 | + } |
| 16 | + should("not allow next request immediately") { |
| 17 | + rateLimit.accept() shouldBe false |
| 18 | + } |
| 19 | + clock.elapse(5.secs) |
| 20 | + should("not allow next request after 5 seconds") { |
| 21 | + rateLimit.accept() shouldBe false |
| 22 | + } |
| 23 | + clock.elapse(6.secs) |
| 24 | + should("allow 2nd request after 11 seconds") { |
| 25 | + rateLimit.accept() shouldBe true |
| 26 | + } |
| 27 | + should("not allow 3rd request after 11 seconds") { |
| 28 | + rateLimit.accept() shouldBe false |
| 29 | + } |
| 30 | + clock.elapse(10.secs) |
| 31 | + should("allow 3rd request after 21 seconds") { |
| 32 | + rateLimit.accept() shouldBe true |
| 33 | + } |
| 34 | + clock.elapse(11.secs) |
| 35 | + should("not allow more than 3 request within the last minute (31 second)") { |
| 36 | + rateLimit.accept() shouldBe false |
| 37 | + } |
| 38 | + clock.elapse(10.secs) |
| 39 | + should("not allow more than 3 request within the last minute (41 second)") { |
| 40 | + rateLimit.accept() shouldBe false |
| 41 | + } |
| 42 | + clock.elapse(10.secs) |
| 43 | + should("not allow more than 3 request within the last minute (51 second)") { |
| 44 | + rateLimit.accept() shouldBe false |
| 45 | + } |
| 46 | + clock.elapse(10.secs) |
| 47 | + should("allow the 4th request after 60 seconds have passed since the 1st (61 second)") { |
| 48 | + rateLimit.accept() shouldBe true |
| 49 | + } |
| 50 | + clock.elapse(5.secs) |
| 51 | + should("not allow the 5th request in 66th second") { |
| 52 | + rateLimit.accept() shouldBe false |
| 53 | + } |
| 54 | + clock.elapse(5.secs) |
| 55 | + should("allow the 5th request in 71st second") { |
| 56 | + rateLimit.accept() shouldBe true |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments