-
Notifications
You must be signed in to change notification settings - Fork 0
/
parent_service_test.go
executable file
·248 lines (203 loc) · 6.45 KB
/
parent_service_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
package bye_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.szostok.io/bye"
)
func TestParentService(t *testing.T) {
const finishImmediately = 50 * time.Millisecond
t.Run("shutdown immediately if no children", func(t *testing.T) {
t.Parallel()
// given
svc := &bye.ParentService{}
// when, then
err := AssertFuncResultAtMost(t, svc.Shutdown, finishImmediately)
assert.NoError(t, err)
})
t.Run("shutdown immediately if all register children finished", func(t *testing.T) {
t.Parallel()
// given
var (
childrenWatcher = &childrenServiceWatcher{}
svc = &bye.ParentService{}
childrenNo = 30
unlockShutdown = make(chan struct{})
)
childrenWatcher.ExpectedChildren(childrenNo)
for i := 0; i < childrenNo; i++ {
svc.Register(&delayedServiceMock{unlockShutdown, childrenWatcher.ChildDone})
}
// when
close(unlockShutdown)
res := AssertFuncResultAtMost(t, svc.Shutdown, finishImmediately)
// then
assert.NoError(t, res)
childrenWatcher.RequireResourcesRelease(t)
})
t.Run("shutdown immediately without error even if one child returned context.Cancelled", func(t *testing.T) {
t.Parallel()
// given
var (
childrenWatcher = childrenServiceWatcher{}
svc = &bye.ParentService{}
unlockShutdown = make(chan struct{})
)
childrenWatcher.ExpectedChildren(3)
svc.Register(&delayedServiceMock{unlockShutdown, childrenWatcher.ChildDone})
svc.Register(&erroneousServiceMock{unlockShutdown, childrenWatcher.ChildDone, context.Canceled})
svc.Register(&delayedServiceMock{unlockShutdown, childrenWatcher.ChildDone})
// when
close(unlockShutdown)
res := AssertFuncResultAtMost(t, svc.Shutdown, finishImmediately)
// then
assert.NoError(t, res)
childrenWatcher.RequireResourcesRelease(t)
})
t.Run("shutdown with error if one child returned generic error", func(t *testing.T) {
t.Parallel()
// given
var (
childrenWatcher = &childrenServiceWatcher{}
svc = &bye.ParentService{}
genericErr = errors.New("oops! internal error occurred :)")
unlockShutdown = make(chan struct{})
)
expErr := heredoc.Doc(`
1 error occurred:
* oops! internal error occurred :)`)
childrenWatcher.ExpectedChildren(3)
svc.Register(&delayedServiceMock{unlockShutdown, childrenWatcher.ChildDone})
svc.Register(&erroneousServiceMock{unlockShutdown, childrenWatcher.ChildDone, context.Canceled})
svc.Register(&erroneousServiceMock{unlockShutdown, childrenWatcher.ChildDone, genericErr})
// when
close(unlockShutdown)
res := AssertFuncResultAtMost(t, svc.Shutdown, finishImmediately)
// then
assert.EqualError(t, res, expErr)
childrenWatcher.RequireResourcesRelease(t)
})
t.Run("shutdown with error if all children returned generic error", func(t *testing.T) {
t.Parallel()
// given
var (
childrenWatcher = &childrenServiceWatcher{}
svc = &bye.ParentService{}
unlockShutdown = make(chan struct{})
)
childrenWatcher.ExpectedChildren(3)
svc.Register(&erroneousServiceMock{unlockShutdown, childrenWatcher.ChildDone, errors.New("oops! svc1 error :)")})
svc.Register(&erroneousServiceMock{unlockShutdown, childrenWatcher.ChildDone, errors.New("oops! svc2 error :)")})
svc.Register(&erroneousServiceMock{unlockShutdown, childrenWatcher.ChildDone, errors.New("oops! svc3 error :)")})
// when
close(unlockShutdown)
res := AssertFuncResultAtMost(t, svc.Shutdown, finishImmediately)
// then
require.Error(t, res)
assert.Contains(t, res.Error(), "3 errors occurred:")
assert.Contains(t, res.Error(), " * oops! svc1 error :)")
assert.Contains(t, res.Error(), " * oops! svc2 error :)")
assert.Contains(t, res.Error(), " * oops! svc3 error :)")
childrenWatcher.RequireResourcesRelease(t)
})
t.Run("shutdown wait for stalled child", func(t *testing.T) {
t.Parallel()
// given
var (
childrenWatcher = &childrenServiceWatcher{}
svc = &bye.ParentService{}
)
childrenWatcher.ExpectedChildren(3)
unlockShutdown := make(chan struct{})
cleanupStalled := make(chan struct{})
svc.Register(&delayedServiceMock{unlockShutdown, childrenWatcher.ChildDone}).
Register(&stalledServiceMock{cleanupStalled, childrenWatcher.ChildDone})
svc.Register(&delayedServiceMock{unlockShutdown, childrenWatcher.ChildDone})
// when, then
close(unlockShutdown)
AssertFuncDoesntResultAtMost(t, svc.Shutdown, time.Second)
// then
close(cleanupStalled)
childrenWatcher.RequireResourcesRelease(t)
})
}
type childrenServiceWatcher struct {
wg sync.WaitGroup
}
func (ts *childrenServiceWatcher) ExpectedChildren(n int) { ts.wg.Add(n) }
func (ts *childrenServiceWatcher) ChildDone() { ts.wg.Done() }
func (ts *childrenServiceWatcher) RequireResourcesRelease(t *testing.T) {
done := make(chan struct{})
go func() {
ts.wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(time.Second * 5):
t.Fatal("Resource release timed out")
}
}
// AssertFuncResultAtMost asserts that the given function returned result before timeout
func AssertFuncResultAtMost(t *testing.T, fn func() error, timeout time.Duration) error {
t.Helper()
resChanReturned := make(chan struct{})
var res error
go func() {
res = fn()
close(resChanReturned)
}()
select {
case <-resChanReturned:
case <-time.After(timeout):
t.Fatalf("given function didn't return after %v", timeout)
}
return res
}
// AssertFuncDoesntResultAtMost asserts that the given function doesn't return before timeout
func AssertFuncDoesntResultAtMost(t *testing.T, fn func() error, timeout time.Duration) {
t.Helper()
fnReturned := make(chan struct{})
go func() {
_ = fn()
close(fnReturned)
}()
select {
case <-fnReturned:
t.Fatalf("given function returned before %v", timeout)
case <-time.After(timeout):
}
}
type delayedServiceMock struct {
ShutdownUnlocked chan struct{}
OnBackgroundDone func()
}
func (s *delayedServiceMock) Shutdown() error {
<-s.ShutdownUnlocked
s.OnBackgroundDone()
return nil
}
type erroneousServiceMock struct {
ShutdownUnlocked chan struct{}
OnBackgroundDone func()
Error error
}
func (s *erroneousServiceMock) Shutdown() error {
<-s.ShutdownUnlocked
s.OnBackgroundDone()
return s.Error
}
type stalledServiceMock struct {
Cleanup chan struct{}
OnBackgroundDone func()
}
func (s *stalledServiceMock) Shutdown() error {
<-s.Cleanup
s.OnBackgroundDone()
return nil
}