File: controller_test.go

package info (click to toggle)
golang-github-lestrrat-go-backoff 2.0.8-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 2
file content (36 lines) | stat: -rw-r--r-- 743 bytes parent folder | download
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)
	}
}