-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers_test.go
More file actions
103 lines (87 loc) · 1.81 KB
/
helpers_test.go
File metadata and controls
103 lines (87 loc) · 1.81 KB
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
package glock
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type mockTestingT struct{}
func (mockTestingT) Errorf(format string, args ...interface{}) {}
func allEventually(t *testing.T, conds ...func() bool) bool {
var wg sync.WaitGroup
wg.Add(len(conds))
var succeeded int64
for _, cond := range conds {
go func(testCond func() bool) {
if testCond() {
atomic.AddInt64(&succeeded, 1)
}
wg.Done()
}(cond)
}
wg.Wait()
return int(succeeded) == len(conds)
}
func eventually(t *testing.T, cond func() bool) bool {
return assert.Eventually(t, cond, time.Second, 10*time.Millisecond)
}
func consistently(t *testing.T, cond func() bool) bool {
if !assert.Eventually(mockTestingT{}, func() bool { return !cond() }, 100*time.Millisecond, 10*time.Millisecond) {
return true
}
return assert.Fail(t, "Condition not met during test period")
}
func consistentlyNot(t *testing.T, cond func() bool) bool {
return consistently(t, func() bool { return !cond() })
}
func chanClosed[T any](ch <-chan T) func() bool {
return func() bool {
select {
case _, ok := <-ch:
return !ok
default:
return false
}
}
}
func chanReceives(ch <-chan time.Time, expected time.Time) func() bool {
return func() bool {
select {
case v := <-ch:
return v == expected
default:
return false
}
}
}
func chanDoesNotReceive(ch <-chan time.Time) func() bool {
return func() bool {
select {
case <-ch:
return false
default:
return true
}
}
}
func structChanReceives(ch <-chan struct{}) func() bool {
return func() bool {
select {
case <-ch:
return true
default:
return false
}
}
}
func structChanDoesNotReceive(ch <-chan struct{}) func() bool {
return func() bool {
select {
case <-ch:
return false
default:
return true
}
}
}