/
wait.go
100 lines (79 loc) · 2.09 KB
/
wait.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package testutils
import (
"testing"
"time"
)
// WaitForAssertion waits for a testify.Assertion to succeed or ultimately fail.
// Note: This should only be used if there's absolutely no way to have the
// test code be able to be signaled that the assertion is ready to be checked.
// That is, if waiting with respect time is critical, it's okay to use this.
func WaitForAssertion(t *testing.T, assertion func(tb testing.TB)) {
t.Helper()
WaitForAssertionWithSleep(t, 50*time.Millisecond, 100, assertion)
}
// WaitForAssertionWithSleep is similar to WaitForAssertion but takes a wait
// time and maximum number of attempts as parameters.
func WaitForAssertionWithSleep(t *testing.T, wait time.Duration, maxAttempts int, assertion func(tb testing.TB)) {
t.Helper()
var attempts int
var checkOk bool
for !checkOk && attempts < maxAttempts {
noFailT := &testingTBNoFail{}
assertion(noFailT)
checkOk = !noFailT.Failed()
if checkOk {
return
}
time.Sleep(wait)
attempts++
}
assertion(t)
}
type testingTBNoFail struct {
testing.TB
failed bool
}
func (t *testingTBNoFail) Cleanup(func()) {
}
func (t *testingTBNoFail) Error(args ...interface{}) {
t.failed = true
}
func (t *testingTBNoFail) Errorf(format string, args ...interface{}) {
t.failed = true
}
func (t *testingTBNoFail) Fail() {
t.failed = true
}
func (t *testingTBNoFail) FailNow() {
t.failed = true
}
func (t *testingTBNoFail) Failed() bool {
return t.failed
}
func (t *testingTBNoFail) Fatal(args ...interface{}) {
t.failed = true
}
func (t *testingTBNoFail) Fatalf(format string, args ...interface{}) {
t.failed = true
}
func (t *testingTBNoFail) Helper() {
}
func (t *testingTBNoFail) Log(args ...interface{}) {
}
func (t *testingTBNoFail) Logf(format string, args ...interface{}) {
}
func (t *testingTBNoFail) Name() string {
return ""
}
func (t *testingTBNoFail) Skip(args ...interface{}) {
}
func (t *testingTBNoFail) SkipNow() {
}
func (t *testingTBNoFail) Skipf(format string, args ...interface{}) {
}
func (t *testingTBNoFail) Skipped() bool {
return false
}
func (t *testingTBNoFail) TempDir() string {
return ""
}