1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
package backoff_test
import (
"context"
"runtime"
"sync"
"testing"
"time"
"github.com/lestrrat-go/backoff/v2"
)
func TestLeak(t *testing.T) {
beforeGoroutine := runtime.NumGoroutine()
var wg sync.WaitGroup
tasks := 100
wg.Add(tasks)
for range make([]struct{}, tasks) {
go func() {
defer wg.Done()
exp := backoff.Exponential()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := exp.Start(ctx)
for backoff.Continue(c) {
time.Sleep(1300 * time.Millisecond)
cancel()
}
}()
}
wg.Wait()
afterGoroutine := runtime.NumGoroutine()
if afterGoroutine > beforeGoroutine+10 {
t.Errorf("goroutines seem to be leaked. before: %d, after: %d", beforeGoroutine, afterGoroutine)
}
}
|