-
Notifications
You must be signed in to change notification settings - Fork 178
/
testUtil.go
134 lines (105 loc) · 2.23 KB
/
testUtil.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
package cmd
import (
"fmt"
"sync"
"github.com/onflow/flow-go/module/irrecoverable"
)
type testLog struct {
logs []string
mux sync.Mutex
}
// handle concurrent logging
func (l *testLog) Logf(msg string, args ...interface{}) {
l.Log(fmt.Sprintf(msg, args...))
}
func (l *testLog) Log(msg string) {
l.mux.Lock()
defer l.mux.Unlock()
l.logs = append(l.logs, msg)
}
func (l *testLog) Reset() {
l.mux.Lock()
defer l.mux.Unlock()
l.logs = []string{}
}
func newMockReadyDone(logger *testLog, name string) *mockReadyDone {
return &mockReadyDone{
name: name,
logger: logger,
readyFn: func(string) {},
doneFn: func(string) {},
ready: make(chan struct{}),
done: make(chan struct{}),
}
}
type mockReadyDone struct {
name string
logger *testLog
readyFn func(string)
doneFn func(string)
ready chan struct{}
done chan struct{}
startOnce sync.Once
stopOnce sync.Once
}
func (c *mockReadyDone) Ready() <-chan struct{} {
c.startOnce.Do(func() {
go func() {
c.readyFn(c.name)
c.logger.Logf("%s ready", c.name)
close(c.ready)
}()
})
return c.ready
}
func (c *mockReadyDone) Done() <-chan struct{} {
c.stopOnce.Do(func() {
go func() {
c.doneFn(c.name)
c.logger.Logf("%s done", c.name)
close(c.done)
}()
})
return c.done
}
func newMockComponent(logger *testLog, name string) *mockComponent {
return &mockComponent{
name: name,
logger: logger,
readyFn: func(string) {},
doneFn: func(string) {},
startFn: func(irrecoverable.SignalerContext, string) {},
ready: make(chan struct{}),
done: make(chan struct{}),
}
}
type mockComponent struct {
name string
logger *testLog
readyFn func(string)
doneFn func(string)
startFn func(irrecoverable.SignalerContext, string)
ready chan struct{}
done chan struct{}
}
func (c *mockComponent) Start(ctx irrecoverable.SignalerContext) {
c.startFn(ctx, c.name)
c.logger.Logf("%s started", c.name)
go func() {
c.readyFn(c.name)
c.logger.Logf("%s ready", c.name)
close(c.ready)
}()
go func() {
<-ctx.Done()
c.doneFn(c.name)
c.logger.Logf("%s done", c.name)
close(c.done)
}()
}
func (c *mockComponent) Ready() <-chan struct{} {
return c.ready
}
func (c *mockComponent) Done() <-chan struct{} {
return c.done
}