-
Notifications
You must be signed in to change notification settings - Fork 179
/
unittest.go
230 lines (199 loc) · 6.44 KB
/
unittest.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package unittest
import (
"io/ioutil"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/lifecycle"
)
func ExpectPanic(expectedMsg string, t *testing.T) {
if r := recover(); r != nil {
err := r.(error)
if err.Error() != expectedMsg {
t.Errorf("expected %v to be %v", err, expectedMsg)
}
return
}
t.Errorf("Expected to panic with `%s`, but did not panic", expectedMsg)
}
// AssertReturnsBefore asserts that the given function returns before the
// duration expires.
func AssertReturnsBefore(t *testing.T, f func(), duration time.Duration) {
done := make(chan struct{})
go func() {
f()
close(done)
}()
select {
case <-time.After(duration):
t.Log("function did not return in time")
t.Fail()
case <-done:
return
}
}
// AssertClosesBefore asserts that the given channel closes before the
// duration expires.
func AssertClosesBefore(t *testing.T, done <-chan struct{}, duration time.Duration) {
select {
case <-time.After(duration):
assert.Fail(t, "channel did not return in time")
case <-done:
return
}
}
// RequireReturnBefore requires that the given function returns before the
// duration expires.
func RequireReturnsBefore(t testing.TB, f func(), duration time.Duration, message string) {
done := make(chan struct{})
go func() {
f()
close(done)
}()
RequireCloseBefore(t, done, duration, message+": function did not return on time")
}
// RequireComponentsDoneBefore invokes the done method of each of the input components concurrently, and
// fails the test if any components shutdown takes longer than the specified duration.
func RequireComponentsDoneBefore(t testing.TB, duration time.Duration, components ...module.ReadyDoneAware) {
done := lifecycle.AllDone(components...)
RequireCloseBefore(t, done, duration, "failed to shutdown all components on time")
}
// RequireComponentsReadyBefore invokes the ready method of each of the input components concurrently, and
// fails the test if any components startup takes longer than the specified duration.
func RequireComponentsReadyBefore(t testing.TB, duration time.Duration, components ...module.ReadyDoneAware) {
ready := lifecycle.AllReady(components...)
RequireCloseBefore(t, ready, duration, "failed to start all components on time")
}
// RequireCloseBefore requires that the given channel returns before the
// duration expires.
func RequireCloseBefore(t testing.TB, c <-chan struct{}, duration time.Duration, message string) {
select {
case <-time.After(duration):
require.Fail(t, "could not close done channel on time: "+message)
case <-c:
return
}
}
// RequireClosed is a test helper function that fails the test if channel `ch` is not closed.
func RequireClosed(t *testing.T, ch <-chan struct{}, message string) {
select {
case <-ch:
default:
require.Fail(t, "channel is not closed: "+message)
}
}
// RequireConcurrentCallsReturnBefore is a test helper that runs function `f` count-many times concurrently,
// and requires all invocations to return within duration.
func RequireConcurrentCallsReturnBefore(t *testing.T, f func(), count int, duration time.Duration, message string) {
wg := &sync.WaitGroup{}
for i := 0; i < count; i++ {
wg.Add(1)
go func() {
f()
wg.Done()
}()
}
RequireReturnsBefore(t, wg.Wait, duration, message)
}
// RequireNeverReturnBefore is a test helper that tries invoking function `f` and fails the test if either:
// - function `f` is not invoked within 1 second.
// - function `f` returns before specified `duration`.
//
// It also returns a channel that is closed once the function `f` returns and hence its openness can evaluate
// return status of function `f` for intervals longer than duration.
func RequireNeverReturnBefore(t *testing.T, f func(), duration time.Duration, message string) <-chan struct{} {
ch := make(chan struct{})
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
wg.Done()
f()
close(ch)
}()
// requires function invoked within next 1 second
RequireReturnsBefore(t, wg.Wait, 1*time.Second, "could not invoke the function: "+message)
// requires function never returns within duration
RequireNeverClosedWithin(t, ch, duration, "unexpected return: "+message)
return ch
}
// RequireNeverClosedWithin is a test helper function that fails the test if channel `ch` is closed before the
// determined duration.
func RequireNeverClosedWithin(t *testing.T, ch <-chan struct{}, duration time.Duration, message string) {
select {
case <-time.After(duration):
case <-ch:
require.Fail(t, "channel closed before timeout: "+message)
}
}
// RequireNotClosed is a test helper function that fails the test if channel `ch` is closed.
func RequireNotClosed(t *testing.T, ch <-chan struct{}, message string) {
select {
case <-ch:
require.Fail(t, "channel is closed: "+message)
default:
}
}
// AssertErrSubstringMatch asserts that two errors match with substring
// checking on the Error method (`expected` must be a substring of `actual`, to
// account for the actual error being wrapped). Fails the test if either error
// is nil.
//
// NOTE: This should only be used in cases where `errors.Is` cannot be, like
// when errors are transmitted over the network without type information.
func AssertErrSubstringMatch(t testing.TB, expected, actual error) {
require.NotNil(t, expected)
require.NotNil(t, actual)
assert.True(
t,
strings.Contains(actual.Error(), expected.Error()) || strings.Contains(expected.Error(), actual.Error()),
"expected error: '%s', got: '%s'", expected.Error(), actual.Error(),
)
}
func TempDir(t testing.TB) string {
dir, err := ioutil.TempDir("", "flow-testing-temp-")
require.NoError(t, err)
return dir
}
func RunWithTempDir(t testing.TB, f func(string)) {
dbDir := TempDir(t)
defer os.RemoveAll(dbDir)
f(dbDir)
}
func BadgerDB(t testing.TB, dir string) *badger.DB {
opts := badger.
DefaultOptions(dir).
WithKeepL0InMemory(true).
WithLogger(nil)
db, err := badger.Open(opts)
require.NoError(t, err)
return db
}
func RunWithBadgerDB(t testing.TB, f func(*badger.DB)) {
RunWithTempDir(t, func(dir string) {
db := BadgerDB(t, dir)
defer db.Close()
f(db)
})
}
func TempBadgerDB(t testing.TB) (*badger.DB, string) {
dir := TempDir(t)
db := BadgerDB(t, dir)
return db, dir
}
func Concurrently(n int, f func(int)) {
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
f(i)
wg.Done()
}(i)
}
wg.Wait()
}