mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-18 03:55:15 +00:00
@@ -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() }()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user