-
Notifications
You must be signed in to change notification settings - Fork 13
/
mocks_test.go
60 lines (55 loc) · 1.33 KB
/
mocks_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
package pipeline
import (
"context"
"fmt"
"time"
)
// mockProcess is a mock of the Processor interface
type mockProcessor[Item any] struct {
processDuration time.Duration
cancelDuration time.Duration
processReturnsErrs bool
processed []Item
canceled []Item
errs []string
}
// Process waits processDuration before returning its input as its output
func (m *mockProcessor[Item]) Process(ctx context.Context, i Item) (Item, error) {
var zero Item
select {
case <-ctx.Done():
return zero, ctx.Err()
case <-time.After(m.processDuration):
break
}
if m.processReturnsErrs {
return zero, fmt.Errorf("process error: %v", i)
}
m.processed = append(m.processed, i)
return i, nil
}
// Cancel collects all inputs that were canceled in m.canceled
func (m *mockProcessor[Item]) Cancel(i Item, err error) {
time.Sleep(m.cancelDuration)
m.canceled = append(m.canceled, i)
m.errs = append(m.errs, err.Error())
}
// containsAll returns true if a and b contain all of the same elements
// in any order or if both are empty / nil
func containsAll[Item comparable](a, b []Item) bool {
if len(a) != len(b) {
return false
} else if len(a) == 0 {
return true
}
aMap := make(map[Item]bool)
for _, i := range a {
aMap[i] = true
}
for _, i := range b {
if !aMap[i] {
return false
}
}
return true
}