mirror of
https://github.com/netbirdio/netbird.git
synced 2025-08-09 15:25:20 +02:00
[client] Add peer conn init limit (#3001)
Limit the peer connection initialization to 200 peers at the same time
This commit is contained in:
66
util/semaphore-group/semaphore_group_test.go
Normal file
66
util/semaphore-group/semaphore_group_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
package semaphoregroup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSemaphoreGroup(t *testing.T) {
|
||||
semGroup := NewSemaphoreGroup(2)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
semGroup.Add(context.Background())
|
||||
go func(id int) {
|
||||
defer semGroup.Done(context.Background())
|
||||
|
||||
got := len(semGroup.semaphore)
|
||||
if got == 0 {
|
||||
t.Errorf("Expected semaphore length > 0 , got 0")
|
||||
}
|
||||
|
||||
time.Sleep(time.Millisecond)
|
||||
t.Logf("Goroutine %d is running\n", id)
|
||||
}(i)
|
||||
}
|
||||
|
||||
semGroup.Wait()
|
||||
|
||||
want := 0
|
||||
got := len(semGroup.semaphore)
|
||||
if got != want {
|
||||
t.Errorf("Expected semaphore length %d, got %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSemaphoreGroupContext(t *testing.T) {
|
||||
semGroup := NewSemaphoreGroup(1)
|
||||
semGroup.Add(context.Background())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
rChan := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
semGroup.Add(ctx)
|
||||
rChan <- struct{}{}
|
||||
}()
|
||||
select {
|
||||
case <-rChan:
|
||||
case <-time.NewTimer(2 * time.Second).C:
|
||||
t.Error("Adding to semaphore group should not block when context is not done")
|
||||
}
|
||||
|
||||
semGroup.Done(context.Background())
|
||||
|
||||
ctxDone, cancelDone := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
t.Cleanup(cancelDone)
|
||||
go func() {
|
||||
semGroup.Done(ctxDone)
|
||||
rChan <- struct{}{}
|
||||
}()
|
||||
select {
|
||||
case <-rChan:
|
||||
case <-time.NewTimer(2 * time.Second).C:
|
||||
t.Error("Releasing from semaphore group should not block when context is not done")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user