Merge pull request #5364 from 0xff-dev/master

fix: goroutine leak
This commit is contained in:
Kubernetes Prow Robot
2023-11-10 17:14:34 +01:00
committed by GitHub
4 changed files with 22 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ import (
// TimedCall runs fn, failing if it doesn't complete in the given duration.
// The description is used in the timeout error message.
func TimedCall(description string, d time.Duration, fn func() error) error {
done := make(chan error)
done := make(chan error, 1)
timer := time.NewTimer(d)
defer timer.Stop()
go func() { done <- fn() }()

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
. "sigs.k8s.io/kustomize/api/internal/utils"
)
@@ -62,3 +63,20 @@ func TestTimedCallSlowWithError(t *testing.T) {
t.Fail()
}
}
func TestTimedCallGoroutineLeak(t *testing.T) {
defer goleak.VerifyNone(t)
err := TimedCall("function done, no goroutine leaks", timeToWait, func() error {
time.Sleep(tooSlow)
return fmt.Errorf("function done")
})
if assert.Error(t, err) {
assert.EqualError(t, err, errMsg("function done, no goroutine leaks"))
} else {
t.Fail()
}
// The code introduces a 2-second sleep to allow the goroutine to complete its execution.
// Subsequently, it verifies if the goroutine created by the function exits as expected.
time.Sleep(tooSlow)
}