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