-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers_test.go
372 lines (314 loc) · 11.1 KB
/
helpers_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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package process
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var testErr1 = errors.New("oops1")
var testErr2 = errors.New("oops2")
// type testLogger struct{}
// func (l testLogger) WithFields(LogFields) Logger { return l }
// func (l testLogger) Info(msg string, args ...interface{}) { fmt.Printf("INFO: "+msg+"\n", args...) }
// func (l testLogger) Warning(msg string, args ...interface{}) { fmt.Printf("WARN: "+msg+"\n", args...) }
// func (l testLogger) Error(msg string, args ...interface{}) { fmt.Printf("ERROR: "+msg+"\n", args...) }
// forwardN returns a channel that proxies the next n values read from the
// given channel. The returned channel will be closed after the nth value
// has been written.
func forwardN(ch <-chan string, n int) <-chan string {
ch2 := make(chan string)
go func() {
defer close(ch2)
for i := 0; i < n; i++ {
ch2 <- <-ch
}
}()
return ch2
}
// traceInit returns a function that can be used as an process's Init method. The returned function
// will register an initially unhealthy health component if the given health value is non-nil. The
// returned function will write a unique string built from the given name and index to the given
// channel once it has been invoked.
func traceInit(health *Health, trace chan<- string, name string, index int, err error) singleErrorFunc {
return func(ctx context.Context) error {
if health != nil {
healthComponent, err := health.Register(testHealthKey(name, index))
if err != nil {
return err
}
healthComponent.Update(false)
}
trace <- fmt.Sprintf("%s.%d.init", name, index)
return err
}
}
// traceRun returns a function that can be used as a process's Run method. The returned
// function will update the health component registered in traceInit if the given health value
// is non-nil. The returned function will write a unique string built from the given name and
// index to the given channel once it has been invoked.
func traceRun(health *Health, trace chan<- string, name string, index int, err error) singleErrorFunc {
return func(ctx context.Context) error {
trace <- fmt.Sprintf("%s.%d.run", name, index)
if err != nil {
return err
}
if health != nil {
healthComponent, ok := health.Get(testHealthKey(name, index))
if !ok {
return fmt.Errorf("unknown health key")
}
healthComponent.Update(true)
}
<-ctx.Done()
return ctx.Err()
}
}
// testHealthKey creates a deterministic health component key from the given name and index.
func testHealthKey(name string, index int) interface{} {
return fmt.Sprintf("%s.%d", name, index)
}
// traceStop returns a function that can be used as a process's Stop method. The returned function
// will write a unique string built from the given name and index to the given channel once it has
// been invoked.
func traceStop(trace chan<- string, name string, index int, err error) singleErrorFunc {
return func(ctx context.Context) error {
trace <- fmt.Sprintf("%s.%d.stop", name, index)
return err
}
}
// traceFinalize returns a function that can be used as a process's Finalize method. The returned
// function will write a unique string built from the given name and index to the given channel
// once it has been invoked.
func traceFinalize(trace chan<- string, name string, index int, err error) singleErrorFunc {
return func(ctx context.Context) error {
trace <- fmt.Sprintf("%s.%d.finalize", name, index)
return err
}
}
// newBlockingSingleErrorFunc returns a function that blocks indefinitely. This
// method also returns a channel closes once the function is invoked. The returned
// function is not safe to call multiple times.
func newBlockingSingleErrorFunc() (singleErrorFunc, <-chan struct{}) {
scheduled := make(chan struct{})
fn := func(ctx context.Context) error {
close(scheduled)
select {}
}
return fn, scheduled
}
// newSingalingSingleErrorFunc returns a function that blocks until the given context
// is canceled. This function also returns a channel closes once the function is invoked.
// The returned function returns the context error value. The returned function is not
// safe to call multiple times.
func newSingalingSingleErrorFunc() (singleErrorFunc, <-chan struct{}) {
scheduled := make(chan struct{})
fn := func(ctx context.Context) error {
close(scheduled)
<-ctx.Done()
return ctx.Err()
}
return fn, scheduled
}
// newTracedSingleErrorFunc returns a function that sends the given list of values down
// the given channel when invoked.
func newTracedSingleErrorFunc(trace chan<- string, values <-chan string, err error) singleErrorFunc {
return func(ctx context.Context) error {
for v := range values {
trace <- v
}
return err
}
}
// traceValue writes the given value to a new channel, closes it, then returns it.
func traceValue(value string) <-chan string {
ch := make(chan string, 1)
ch <- value
close(ch)
return ch
}
type unorderedWrapper struct {
values []interface{}
}
// seq returns the given values as a slice.
func seq(values ...interface{}) []interface{} {
return values
}
// unordered wraps the given values in an instance of unorderedWrapper. This
// type is used to signify in assertChannelContents that all of the values in
// the set must occur before any other values, but they may occur in any order.
//
// For example, valid orderings of [a, b, unordered(c, d, e), f, g] are:
//
// - [a, b, c, d, e, f, g]
// - [a, b, c, e, d, f, g]
// - [a, b, d, c, e, f, g]
// - [a, b, d, e, c, f, g]
// - [a, b, e, c, d, f, g]
// - [a, b, e, d, c, f, g]
func unordered(values ...interface{}) unorderedWrapper {
return unorderedWrapper{values}
}
// assertChannelContents invokes the given read function, which returns a slice
// of values from the channel and a boolean flag indicating whether or not a read
// timed out.
//
// The values in the slice are compared against the given expected values. This
// method will report a fatal test error if the content of the slices differ in
// value or (generally) order. Runs of values can be marked as occurring in any
// order by wrapping them via the unordered function.
func assertChannelContents(t *testing.T, read readFunc, expectedValues []interface{}, msgAndArgs ...interface{}) {
values, timedOut := read(t)
if timedOut {
msgAndArgs = wrapTestError("Channel did not close", msgAndArgs...)
}
if len(expectedValues) == 0 {
// Normalize for empty case
require.Empty(t, values, msgAndArgs...)
return
}
i := 0 // index for values
j := 0 // index for expectedValues
var next []interface{}
for {
for len(next) > 0 {
if i >= len(values) {
reportMismatchedContent(t, "Expected more values.", expectedValues, values)
}
found := false
for k, candidate := range next {
if objectsAreEqualValues(values[i], candidate) {
found = true
next[0], next[k] = next[k], next[0]
break
}
}
if !found {
reportMismatchedContent(t, "Mismatched values.", expectedValues, values)
}
i++
next = next[1:]
}
if j == len(expectedValues) {
break
}
if u, ok := expectedValues[j].(unorderedWrapper); ok {
next = u.values
} else {
next = []interface{}{expectedValues[j]}
}
j++
}
if i < len(values) {
reportMismatchedContent(t, "Expected fewer values.", expectedValues, values)
}
}
// reportMismatchedContent invokes a fatal test error with a serialized version of the
// given expected values and actual values.
func reportMismatchedContent(t *testing.T, message string, expectedValues, values []interface{}) {
var flattened []string
for _, expectedValue := range expectedValues {
if u, ok := expectedValue.(unorderedWrapper); ok {
var strValues []string
for _, value := range u.values {
strValues = append(strValues, fmt.Sprintf("%s", value))
}
flattened = append(flattened, fmt.Sprintf("{ %s }", strings.Join(strValues, ", ")))
} else {
flattened = append(flattened, fmt.Sprintf("%v", expectedValue))
}
}
t.Fatalf(fmt.Sprintf("%s want=%s, have=%v", message, strings.Join(flattened, ", "), values))
}
// objectsAreEqualValues returns true if the given values are logically equal. We do
// this currently by casting them both to strings and comparing the resulting serialized
// value. This works because we're currently only testing with string and error values,
// which have well-formed and deterministic string representations.
func objectsAreEqualValues(expected, value interface{}) bool {
return fmt.Sprintf("%s", expected) == fmt.Sprintf("%s", value)
}
// permute returns a slice of permutations of the given values.
func permute(values []interface{}) [][]interface{} {
ch := make(chan []interface{})
go func() {
defer close(ch)
permuteHelper(values, 0, ch)
}()
var permutations [][]interface{}
for permutation := range ch {
permutations = append(permutations, permutation)
}
return permutations
}
// permuteHelper recursively computes permutations of the given values. When a
// full permutation is constructed, it is written to the given channel.
//
// The given values paramete stores a mutable version of the input undergoing
// permutation. The index parameter specifies the size of the slice prefix that
// should not be modified on this chain of recursive calls.
func permuteHelper(values []interface{}, index int, ch chan<- []interface{}) {
if index > len(values) {
// values is mutable, make a copy before sending it
valuesCopy := make([]interface{}, len(values))
copy(valuesCopy, values)
ch <- valuesCopy
return
}
permuteHelper(values, index+1, ch)
for j := index + 1; j < len(values); j++ {
values[index], values[j] = values[j], values[index]
permuteHelper(values, index+1, ch)
values[index], values[j] = values[j], values[index]
}
}
type readFunc func(t *testing.T) (values []interface{}, complete bool)
// wrapTestError appends the given prefix to the error message template given
// in the values composing msgAndArgs.
func wrapTestError(prefix string, msgAndArgs ...interface{}) []interface{} {
if len(msgAndArgs) == 0 {
return []interface{}{prefix}
}
if msg, ok := msgAndArgs[0].(string); ok {
prefixedMessage := fmt.Sprintf("%s (%s)", prefix, msg)
return append([]interface{}{prefixedMessage}, msgAndArgs[1:]...)
}
return msgAndArgs
}
// readErrorChannel returns a function that reads from the given channel with
// a timeout. If no value was ready to be read from the channel for the timeout
// period, a false-valued flag is returned.
func readErrorChannel(ch <-chan error) readFunc {
return func(t *testing.T) (values []interface{}, complete bool) {
for {
select {
case value, ok := <-ch:
if !ok {
return values, true
}
values = append(values, value)
case <-time.After(time.Second):
return values, false
}
}
}
}
// readStringChannel returns a function that reads from the given channel with
// a timeout. If no value was ready to be read from the channel for the timeout
// period, a false-valued flag is returned.
func readStringChannel(ch <-chan string) readFunc {
return func(t *testing.T) (values []interface{}, complete bool) {
for {
select {
case value, ok := <-ch:
if !ok {
return values, true
}
values = append(values, value)
case <-time.After(time.Second):
return values, false
}
}
}
}