-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle_test.go
256 lines (241 loc) · 6.46 KB
/
lifecycle_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
package slice
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/goava/di"
"github.com/stretchr/testify/require"
)
func TestLifecycle_createContainer(t *testing.T) {
t.Run("provide user dependency", func(t *testing.T) {
c, err := createContainer(
di.Provide(http.NewServeMux),
)
require.NoError(t, err)
var mux *http.ServeMux
has, err := c.Has(&mux)
require.NoError(t, err)
require.True(t, has)
})
t.Run("incorrect option cause error", func(t *testing.T) {
c, err := createContainer(
di.Provide(func() {}),
)
require.Nil(t, c)
require.Error(t, err)
require.Contains(t, err.Error(), "lifecycle_test.go:")
require.Contains(t, err.Error(), ": invalid constructor signature, got func()")
})
}
func TestLifecycle_before(t *testing.T) {
t.Run("iterates over bundles and run before hook", func(t *testing.T) {
c, err := di.New()
require.NoError(t, err)
require.NotNil(t, c)
var order []string
firstBundle := Bundle{
Name: "first-bundle",
Hooks: []Hook{{
BeforeStart: func() {
order = append(order, "first-bundle")
},
BeforeShutdown: func() {},
}},
}
secondBundle := Bundle{
Name: "second-bundle",
Hooks: []Hook{{
BeforeStart: func() {
order = append(order, "second-bundle")
},
}},
}
shutdowns, err := beforeStart(context.Background(), c, firstBundle, secondBundle)
require.NoError(t, err)
require.Len(t, shutdowns, 1)
require.Equal(t, []string{"first-bundle", "second-bundle"}, order)
})
t.Run("bundle boot error causes boot error", func(t *testing.T) {
c, err := di.New()
require.NoError(t, err)
require.NotNil(t, c)
bundle := Bundle{
Name: "error-bundle",
Hooks: []Hook{{
BeforeStart: func() error { return errors.New("unexpected error") },
}},
}
hooks, err := beforeStart(context.Background(), c, bundle)
require.EqualError(t, err, "- boot error-bundle bundle failed: unexpected error\n")
require.Len(t, hooks, 0)
})
t.Run("shutdowns correct on context cancel", func(t *testing.T) {
c, err := di.New()
require.NoError(t, err)
require.NotNil(t, c)
firstBundle := Bundle{
Name: "first-bundle",
Hooks: []Hook{{
BeforeStart: func() error {
time.Sleep(2 * time.Millisecond)
return nil
},
}},
}
secondBundle := Bundle{
Name: "second-bundle",
Hooks: []Hook{{
BeforeStart: func() {},
}},
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
hooks, err := beforeStart(ctx, c, firstBundle, secondBundle)
require.EqualError(t, err, "boot first-bundle bundle failed: context canceled")
require.Len(t, hooks, 0)
})
}
func TestLifecycle_dispatch(t *testing.T) {
t.Run("resolve dispatchers and run only once", func(t *testing.T) {
dispatcher := &DispatcherMock{
RunFunc: func(ctx context.Context) error {
return nil
},
}
ctx, cancel := context.WithCancel(context.Background())
err := dispatch(ctx, &stdLogger{}, cancel, []Dispatcher{dispatcher})
require.NoError(t, err)
require.Len(t, dispatcher.RunCalls(), 1)
})
// todo: rewrite test case
//t.Run("undefined dispatcher cause error", func(t *testing.T) {
// c, err := di.New()
// require.NoError(t, err)
// require.NotNil(t, c)
// ctx, cancel := context.WithCancel(context.Background())
// err = dispatch(ctx, cancel)
// require.Error(t, err)
// require.Contains(t, err.Error(), "dispatch failed: ")
// require.Contains(t, err.Error(), "lifecycle.go:")
// require.Contains(t, err.Error(), ": type []slice.Dispatcher not exists in the container")
//})
t.Run("run error causes error", func(t *testing.T) {
d1 := &DispatcherMock{
RunFunc: func(ctx context.Context) error {
return errors.New("unexpected error")
},
}
contextCancelled := false
d2 := &DispatcherMock{
RunFunc: func(ctx context.Context) error {
select {
case <-ctx.Done():
contextCancelled = true
return ctx.Err()
case <-time.After(time.Second):
require.Fail(t, "context should be cancelled")
return nil
}
},
}
ctx, cancel := context.WithCancel(context.Background())
err := dispatch(ctx, &stdLogger{}, cancel, []Dispatcher{d1, d2})
require.EqualError(t, err, "failure: *slice.DispatcherMock: unexpected error")
require.Len(t, d1.RunCalls(), 1)
require.True(t, contextCancelled)
})
}
func TestLifecycle_after(t *testing.T) {
t.Run("reverse order", func(t *testing.T) {
c, err := di.New()
require.NoError(t, err)
require.NotNil(t, c)
var order []string
hooks := []hook{
{
name: "first-shutdown",
hook: func() {
order = append(order, "first-shutdown")
},
},
{
name: "second-shutdown",
hook: func() {
order = append(order, "second-shutdown")
},
},
{
name: "third-shutdown",
hook: func() {
order = append(order, "third-shutdown")
},
},
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err = beforeShutdown(ctx, c, hooks)
require.NoError(t, err)
require.Equal(t, []string{"third-shutdown", "second-shutdown", "first-shutdown"}, order)
})
t.Run("shutdown errors returns one", func(t *testing.T) {
c, err := di.New()
require.NoError(t, err)
require.NotNil(t, c)
hooks := []hook{
{
name: "first-shutdown",
hook: func() error {
return errors.New("first-error")
},
},
{
name: "second-shutdown",
hook: func() error {
return errors.New("second-error")
},
},
{
name: "third-shutdown",
hook: func() error {
return errors.New("third-error")
},
},
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err = beforeShutdown(ctx, c, hooks)
require.EqualError(t, err, "shutdown failed: shutdown third-shutdown failed: third-error; shutdown second-shutdown failed: second-error; shutdown first-shutdown failed: first-error")
})
t.Run("context error", func(t *testing.T) {
c, err := di.New()
require.NoError(t, err)
require.NotNil(t, c)
shutdowns := []hook{
{
name: "first-shutdown",
hook: func() error {
time.Sleep(time.Hour)
return nil
},
},
{
name: "second-shutdown",
hook: func() error {
return errors.New("second-error")
},
},
{
name: "third-shutdown",
hook: func() error {
return errors.New("third-error")
},
},
}
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel()
err = beforeShutdown(ctx, c, shutdowns)
require.EqualError(t, err, "shutdown failed: context deadline exceeded")
})
}