-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
timedcall_test.go
77 lines (67 loc) · 1.69 KB
/
timedcall_test.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package utils_test
import (
"fmt"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
. "sigs.k8s.io/kustomize/api/internal/utils"
)
const (
timeToWait = 10 * time.Millisecond
tooSlow = 2 * timeToWait
)
func errMsg(msg string) string {
return fmt.Sprintf("hit %s timeout running '%s'", timeToWait, msg)
}
func TestTimedCallFastNoError(t *testing.T) {
err := TimedCall(
"fast no error", timeToWait,
func() error { return nil })
if !assert.NoError(t, err) {
t.Fatal(err)
}
}
func TestTimedCallFastWithError(t *testing.T) {
err := TimedCall(
"fast with error", timeToWait,
func() error { return assert.AnError })
if assert.Error(t, err) {
assert.EqualError(t, err, assert.AnError.Error())
} else {
t.Fail()
}
}
func TestTimedCallSlowNoError(t *testing.T) {
err := TimedCall(
"slow no error", timeToWait,
func() error { time.Sleep(tooSlow); return nil })
if assert.Error(t, err) {
assert.EqualError(t, err, errMsg("slow no error"))
} else {
t.Fail()
}
}
func TestTimedCallSlowWithError(t *testing.T) {
err := TimedCall(
"slow with error", timeToWait,
func() error { time.Sleep(tooSlow); return assert.AnError })
if assert.Error(t, err) {
assert.EqualError(t, err, errMsg("slow with error"))
} else {
t.Fail()
}
}
func TestTimedCallGoroutineLeak(t *testing.T) {
beforeGroutine := runtime.NumGoroutine()
_ = TimedCall("expect no goroutine leaks", time.Duration(time.Second), func() error {
time.Sleep(2 * time.Second)
return fmt.Errorf("not done")
})
afterGoroutine := runtime.NumGoroutine()
if beforeGroutine != afterGoroutine {
t.Fatal("goroutine leak")
}
}