Skip to content

Commit

Permalink
fix ratelimiter test case when in some os environment
Browse files Browse the repository at this point in the history
  • Loading branch information
hugefiver committed Dec 5, 2024
1 parent 4f38230 commit 697d63a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestSSHRateLimiter(t *testing.T) {

rl := NewSSHRateLimiter(
[]*conf.RateLimitConfig{
{Limit: 3, Interval: utils.Duration(time.Second)},
{Limit: 5, Interval: utils.Duration(time.Second * 10)},
{Limit: 3, Interval: utils.Duration(time.Second * 2)},
{Limit: 6, Interval: utils.Duration(time.Second * 9)},
},
[]*conf.RateLimitConfig{
{Limit: 1, Interval: utils.Duration(time.Second), PerIP: true},
Expand All @@ -57,41 +57,51 @@ func TestSSHRateLimiter(t *testing.T) {
x1 := rl.Allow("1")
x2 := rl.Allow("1")

// global: 1|1, `1`: 1
if !x1.OK() || x2.OK() {
t.Errorf("x1: %v, x2: %v", x1, x2)
}

y1 := rl.Allow("2")
y2 := rl.Allow("2")

// global: 2|2, `1`: 1, `2`: 1
if !y1.OK() || y2.OK() {
t.Errorf("y1: %v, y2: %v", y1, y2)
}

// to avoid timer lag
time.Sleep(time.Second * 1)

z1 := rl.Allow("3")
z2 := rl.Allow("3")

// global: 3|3, `1`: 0~1, `2`: 0~1, `3`: 1
if !z1.OK() || z2.OK() {
t.Errorf("z1: %v, z2: %v", z1, z2)
}

time.Sleep(time.Second * 1)
time.Sleep(time.Second * 2)
// global: 0|1(because sleep ~1/3 ratelimiter duration), `1`: 0, `2`: 0, `3`: 0

x3 := rl.Allow("1")

// global: 1|2, `1`: 1, `2`: 0, `3`: 0
if !x3.OK() {
t.Errorf("x3: %v", x3)
}

y3 := rl.Allow("2")

// global: 2|3, `1`: 1, `2`: 1, `3`: 0
if !y3.OK() {
t.Errorf("y3: %v", y3)
}

z3 := rl.Allow("3")

if z3.OK() {
// global: 3|4, `1`: 1, `2`: 1, `3`: 1
if !z3.OK() {
t.Errorf("z3: %v", z3)
}
}

0 comments on commit 697d63a

Please sign in to comment.