Skip to content

Commit

Permalink
tests(limiter): fix unstable TestConnectionLimiter (cloudwego#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost authored Sep 25, 2024
1 parent 19a3a6a commit e4c70c1
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pkg/limiter/connection_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ import (
)

func TestConnectionLimiter(t *testing.T) {
connLimit := 50
const (
concurrency = 10
connLimit = 1000
)
ctx := context.Background()
lim := NewConnectionLimiter(connLimit)

var wg sync.WaitGroup
var connCount int32
for i := 0; i < 100; i++ {
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 10; j++ {
time.Sleep(4 * time.Millisecond) // wait other goroutines ready to start
for j := 0; j < connLimit; j++ {
if lim.Acquire(ctx) {
atomic.AddInt32(&connCount, 1)
time.Sleep(time.Millisecond)
} else {
lim.Release(ctx)
time.Sleep(time.Millisecond)
}
_, curr := lim.Status(ctx)
test.Assert(t, curr <= 60, curr)
test.Assert(t, curr <= connLimit+concurrency, curr)
}
}()
}
Expand Down

0 comments on commit e4c70c1

Please sign in to comment.