| 1 | package g8scip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/ |
| 2 | |
| 3 | import ( |
| 4 | "testingscip-go gomod github.com/golang/go/src go1.23.3 testing/" |
| 5 | "timescip-go gomod github.com/golang/go/src go1.23.3 time/" |
| 6 | ) |
| 7 | |
| 8 | func TestNewRateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/TestNewRateLimiter().(t *testingscip-go gomod github.com/golang/go/src go1.23.3 testing/.Tscip-go gomod github.com/golang/go/src go1.23.3 testing/T#) { |
| 9 | rl := NewRateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewRateLimiter().(2) |
| 10 | if rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond. != 2 { |
| 11 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond.) |
| 12 | } |
| 13 | if rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow. != 2 { |
| 14 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected executionsLeftInWindow to be %d, got %d", 2, rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow.) |
| 15 | } |
| 16 | // First execution: should not be rate limited |
| 17 | if notRateLimited := rl.Tryscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#Try().(); !notRateLimited { |
| 18 | t.Errorscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Error().("expected Try to return true") |
| 19 | } |
| 20 | if rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond. != 2 { |
| 21 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond.) |
| 22 | } |
| 23 | if rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow. != 1 { |
| 24 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected executionsLeftInWindow to be %d, got %d", 1, rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow.) |
| 25 | } |
| 26 | // Second execution: should not be rate limited |
| 27 | if notRateLimited := rl.Tryscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#Try().(); !notRateLimited { |
| 28 | t.Errorscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Error().("expected Try to return true") |
| 29 | } |
| 30 | if rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond. != 2 { |
| 31 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond.) |
| 32 | } |
| 33 | if rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow. != 0 { |
| 34 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected executionsLeftInWindow to be %d, got %d", 0, rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow.) |
| 35 | } |
| 36 | // Third execution: should be rate limited |
| 37 | if notRateLimited := rl.Tryscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#Try().(); notRateLimited { |
| 38 | t.Errorscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Error().("expected Try to return false") |
| 39 | } |
| 40 | if rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond. != 2 { |
| 41 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected maximumExecutionsPerSecond to be %d, got %d", 2, rl.maximumExecutionsPerSecondscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#maximumExecutionsPerSecond.) |
| 42 | } |
| 43 | if rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow. != 0 { |
| 44 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Errorf().("expected executionsLeftInWindow to be %d, got %d", 0, rl.executionsLeftInWindowscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#executionsLeftInWindow.) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestRateLimiter_Tryscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/TestRateLimiter_Try().(t *testingscip-go gomod github.com/golang/go/src go1.23.3 testing/.Tscip-go gomod github.com/golang/go/src go1.23.3 testing/T#) { |
| 49 | rl := NewRateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewRateLimiter().(5) |
| 50 | for i := 0; i < 20; i++ { |
| 51 | notRateLimited := rl.Tryscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#Try().() |
| 52 | if i < 5 { |
| 53 | if !notRateLimited { |
| 54 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Fatal().("expected to not be rate limited") |
| 55 | } |
| 56 | } else { |
| 57 | if notRateLimited { |
| 58 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Fatal().("expected to be rate limited") |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestRateLimiter_TryAlwaysUnderRateLimitscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/TestRateLimiter_TryAlwaysUnderRateLimit().(t *testingscip-go gomod github.com/golang/go/src go1.23.3 testing/.Tscip-go gomod github.com/golang/go/src go1.23.3 testing/T#) { |
| 65 | rl := NewRateLimiterscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/NewRateLimiter().(20) |
| 66 | for i := 0; i < 45; i++ { |
| 67 | notRateLimited := rl.Tryscip-go gomod github.com/TwiN/g8/v3 . `github.com/TwiN/g8/v3`/RateLimiter#Try().() |
| 68 | if !notRateLimited { |
| 69 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.3 testing/common#Fatal().("expected to not be rate limited") |
| 70 | } |
| 71 | timescip-go gomod github.com/golang/go/src go1.23.3 time/.Sleepscip-go gomod github.com/golang/go/src go1.23.3 time/Sleep().(51 * timescip-go gomod github.com/golang/go/src go1.23.3 time/.Millisecondscip-go gomod github.com/golang/go/src go1.23.3 time/Millisecond.) |
| 72 | } |
| 73 | } |