-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh_test.go
279 lines (245 loc) · 7.24 KB
/
refresh_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package skyconf
import (
"code.cloudfoundry.org/clock/fakeclock"
"context"
"github.com/stretchr/testify/assert"
"sync"
"testing"
"time"
)
type config struct {
Param1 string `sky:",source:global,refresh:1s"`
Param2 string `sky:",refresh:1s"`
m sync.Mutex
}
func (c *config) Lock() {
c.m.Lock()
}
func (c *config) Unlock() {
c.m.Unlock()
}
func TestParseAndRefreshUsingMockClock(t *testing.T) {
parameterStore := func() mockParameterStore {
return mockParameterStore{
"/path/global/param1": "global-value1",
"/path/global/param2": "global-value2",
"/path/region1/param2": "region1-value2",
}
}
type assertUpdatesInput struct {
id string
expected string
field *string
}
assertUpdates := func(t *testing.T, updates <-chan string, expected ...assertUpdatesInput) bool {
// Make a map of expected updates to mark them as received
expectedMap := make(map[int]*bool, len(expected))
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// Collect as many updates as expected
for range expected {
var u string
select {
case <-ctx.Done():
assert.Fail(t, "timed out waiting for updates")
return false
case u = <-updates:
}
// find the expected update using the ID
for i, e := range expected {
// if already received, skip
if _, done := expectedMap[i]; done {
continue
}
if e.id == u {
a := assert.Equal(t, e.expected, *(e.field))
expectedMap[i] = &a
break
}
}
}
// Ensure all expected updates were received
for i, v := range expectedMap {
if v == nil {
assert.Failf(t, "expected update not received", "expected: %v", expected[i])
return false
}
if !assert.Truef(t, v != nil, "expected update not received; %v", expected[i]) {
return false
}
}
return true
}
tests := []struct {
name string
cfg interface{}
sources []Source
wantErr assert.ErrorAssertionFunc
want func(t *testing.T, sources []Source, cfg interface{}, r Refresher, clock *fakeclock.FakeClock)
}{
{
name: "refresh once",
cfg: &config{},
sources: []Source{
&mockSource{
ps: parameterStore(),
path: "/path/global/",
id: "global",
refreshable: true,
},
&mockSource{
ps: parameterStore(),
path: "/path/region1/",
id: "regional",
refreshable: true,
},
},
wantErr: assert.NoError,
want: func(t *testing.T, sources []Source, cfg interface{}, r Refresher, clock *fakeclock.FakeClock) {
c := cfg.(*config)
assert.Equal(t, "global-value1", c.Param1)
assert.Equal(t, "region1-value2", c.Param2)
// update the values
sources[0].(*mockSource).set("/path/global/param1", "new-global-value1")
sources[0].(*mockSource).set("/path/global/param2", "new-global-value2")
sources[1].(*mockSource).set("/path/region1/param2", "new-region1-value2")
// setup refresh
err := r.RefreshOnce(context.Background())
if assert.NoError(t, err) {
// Check the values after refresh
assert.Equal(t, "new-global-value1", c.Param1)
assert.Equal(t, "new-region1-value2", c.Param2)
}
},
},
{
name: "refresh continuously",
cfg: &config{},
sources: []Source{
&mockSource{
ps: parameterStore(),
path: "/path/global/",
id: "global",
refreshable: true,
},
&mockSource{
ps: parameterStore(),
path: "/path/region1/",
id: "regional",
refreshable: true,
},
},
wantErr: assert.NoError,
want: func(t *testing.T, sources []Source, cfg interface{}, r Refresher, clock *fakeclock.FakeClock) {
c := cfg.(*config)
assert.Equal(t, "global-value1", c.Param1)
assert.Equal(t, "region1-value2", c.Param2)
// setup refresh
ctx, cancel := context.WithCancel(context.Background())
updates := r.Refresh(ctx, func(err error) {
if assert.NoError(t, err) {
return
}
cancel()
})
allUpdates := make(chan string, 1000)
go func() {
for u := range updates {
allUpdates <- u
}
}()
// Stop refreshing when the test is done
defer cancel()
// Timeout the test after 5 seconds
go func() {
select {
case <-time.After(5 * time.Second):
assert.Failf(t, "test timed out", "")
cancel()
case <-ctx.Done():
}
}()
// Update the values in the sources
sources[0].(*mockSource).set("/path/global/param1", "new1-global-value1")
sources[0].(*mockSource).set("/path/global/param2", "new1-global-value2")
sources[1].(*mockSource).set("/path/region1/param2", "new1-region1-value2")
// Advance the clock to trigger the refresh
clock.Increment(time.Second + 1*time.Millisecond)
if !assertUpdates(t, allUpdates,
assertUpdatesInput{"Param1", "new1-global-value1", &c.Param1},
assertUpdatesInput{"Param2", "new1-region1-value2", &c.Param2},
) {
return
}
// Update the values in the sources
sources[0].(*mockSource).set("/path/global/param1", "new2-global-value1")
sources[0].(*mockSource).set("/path/global/param2", "new2-global-value2")
sources[1].(*mockSource).set("/path/region1/param2", "new2-region1-value2")
// Advance the clock to trigger the refresh
clock.Increment(time.Second + 1*time.Millisecond)
if !assertUpdates(t, allUpdates,
assertUpdatesInput{"Param1", "new2-global-value1", &c.Param1},
assertUpdatesInput{"Param2", "new2-region1-value2", &c.Param2},
) {
return
}
// Update the values in the sources
sources[0].(*mockSource).set("/path/global/param1", "new3-global-value1")
sources[0].(*mockSource).set("/path/global/param2", "new3-global-value2")
sources[1].(*mockSource).set("/path/region1/param2", "new3-region1-value2")
// Advance the clock to trigger the refresh
clock.Increment(time.Second + 1*time.Millisecond)
if !assertUpdates(t, allUpdates,
assertUpdatesInput{"Param1", "new3-global-value1", &c.Param1},
assertUpdatesInput{"Param2", "new3-region1-value2", &c.Param2},
) {
return
}
cancel()
},
},
{
name: "refresh continuously with no refreshable sources",
cfg: &config{},
sources: []Source{
&mockSource{
ps: parameterStore(),
path: "/path/global/",
id: "global",
refreshable: false,
},
&mockSource{
ps: parameterStore(),
path: "/path/region1/",
id: "regional",
refreshable: false,
},
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorIs(t, err, ErrSourceNotRefreshable)
},
},
}
testTime := time.Date(2021, 1, 1, 1, 1, 1, 1, time.UTC)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clock := fakeclock.NewFakeClock(testTime)
r, err := Parse(context.Background(), tt.cfg, true, tt.sources...)
if tt.wantErr == nil {
tt.wantErr = assert.NoError
}
if tt.wantErr(t, err) && err == nil && tt.want != nil {
// If updater, set the mock clock
if r != nil {
upd, ok := r.(*updater)
if ok {
upd.clock = clock
}
tt.want(t, tt.sources, tt.cfg, upd, clock)
} else {
tt.want(t, tt.sources, tt.cfg, r, clock)
}
}
})
}
}