-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanager_test.go
223 lines (183 loc) · 5.87 KB
/
manager_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
package reload_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/slok/reload"
"github.com/slok/reload/internal/reloadmock"
)
type priorityMockReloader struct {
priority int
m *reloadmock.Reloader
}
func TestManagerWithNotifierFunc(t *testing.T) {
tests := map[string]struct {
reloaders func() []priorityMockReloader
notifierID string
notifierErr error
expErr bool
}{
"If notifier fails it should end the execution with an error.": {
reloaders: func() []priorityMockReloader {
m1 := priorityMockReloader{0, &reloadmock.Reloader{}}
return []priorityMockReloader{m1}
},
notifierID: "test-id",
notifierErr: fmt.Errorf("something"),
expErr: true,
},
"Single reloader should be called with the expected trigger ID.": {
reloaders: func() []priorityMockReloader {
m1 := priorityMockReloader{0, &reloadmock.Reloader{}}
m1.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
return []priorityMockReloader{m1}
},
notifierID: "test-id",
},
"Single reloader error should get the error.": {
reloaders: func() []priorityMockReloader {
m1 := priorityMockReloader{0, &reloadmock.Reloader{}}
m1.m.On("Reload", mock.Anything, mock.Anything).Once().Return(fmt.Errorf("something"))
return []priorityMockReloader{m1}
},
notifierID: "test-id",
expErr: true,
},
"Multiple reloaders should be called with the expected trigger ID.": {
reloaders: func() []priorityMockReloader {
m1 := priorityMockReloader{0, &reloadmock.Reloader{}}
m1.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
m2 := priorityMockReloader{0, &reloadmock.Reloader{}}
m2.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
m3 := priorityMockReloader{0, &reloadmock.Reloader{}}
m3.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
return []priorityMockReloader{m1, m2, m3}
},
notifierID: "test-id",
},
"Multiple reloaders with different priority should be called with the expected trigger ID.": {
reloaders: func() []priorityMockReloader {
m1 := priorityMockReloader{2, &reloadmock.Reloader{}}
m1.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
m2 := priorityMockReloader{0, &reloadmock.Reloader{}}
m2.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
m3 := priorityMockReloader{1, &reloadmock.Reloader{}}
m3.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
return []priorityMockReloader{m1, m2, m3}
},
notifierID: "test-id",
},
"Having multiple reloaders with different priority, if a lower priority errors, shouldn't call the next ones.": {
reloaders: func() []priorityMockReloader {
m1 := priorityMockReloader{10, &reloadmock.Reloader{}}
m1.m.On("Reload", mock.Anything, "test-id").Once().Return(fmt.Errorf("something"))
m2 := priorityMockReloader{4, &reloadmock.Reloader{}}
m2.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
m3 := priorityMockReloader{25, &reloadmock.Reloader{}}
m4 := priorityMockReloader{20, &reloadmock.Reloader{}}
m5 := priorityMockReloader{25, &reloadmock.Reloader{}}
return []priorityMockReloader{m1, m2, m3, m4, m5}
},
notifierID: "test-id",
expErr: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
reloaders := test.reloaders()
// Prepare.
m := reload.NewManager()
for _, r := range reloaders {
m.Add(r.priority, r.m)
}
notifierC := make(chan string)
m.On(reload.NotifierFunc(func(context.Context) (string, error) {
notifierID := <-notifierC
return notifierID, test.notifierErr
}))
// Execute.
ctx, cancel := context.WithCancel(context.Background())
checksFinished := make(chan struct{})
go func() {
err := m.Run(ctx)
// Check.
if test.expErr {
assert.Error(err)
} else {
assert.NoError(err)
}
for _, r := range reloaders {
r.m.AssertExpectations(t)
}
close(checksFinished)
}()
// Release the trigger to start the execution and checks.
notifierC <- test.notifierID
// Wait for until the reloaders handle the trigger.
// Then cancel the context in case the reloaders didn't
// error.
time.Sleep(10 * time.Millisecond)
cancel()
// Wait until everything has been checked.
<-checksFinished
})
}
}
func TestManagerWithNotifierChan(t *testing.T) {
tests := map[string]struct {
reloaders func() []priorityMockReloader
notifierID string
expErr bool
}{
"Single reloader should be called with the expected trigger ID.": {
reloaders: func() []priorityMockReloader {
m1 := priorityMockReloader{0, &reloadmock.Reloader{}}
m1.m.On("Reload", mock.Anything, "test-id").Once().Return(nil)
return []priorityMockReloader{m1}
},
notifierID: "test-id",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
reloaders := test.reloaders()
// Prepare.
m := reload.NewManager()
for _, r := range reloaders {
m.Add(r.priority, r.m)
}
notifierC := make(chan string)
m.On(reload.NotifierChan(notifierC))
// Execute.
ctx, cancel := context.WithCancel(context.Background())
checksFinished := make(chan struct{})
go func() {
err := m.Run(ctx)
// Check.
if test.expErr {
assert.Error(err)
} else {
assert.NoError(err)
}
for _, r := range reloaders {
r.m.AssertExpectations(t)
}
close(checksFinished)
}()
// Release the trigger to start the execution and checks.
notifierC <- test.notifierID
// Wait for until the reloaders handle the trigger.
// Then cancel the context in case the reloaders didn't
// error.
time.Sleep(10 * time.Millisecond)
cancel()
// Wait until everything has been checked.
<-checksFinished
})
}
}