/
watcher.go
514 lines (429 loc) · 12 KB
/
watcher.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package watcher
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Watcher struct {
mu sync.Mutex
// Expected and observed data slices.
// Calling Add/Expect* adds data to expected.
// Calling Observe adds data to observed.
expected []interface{}
observed []interface{}
// Expected data that is yet to be observed.
// Calling Prepare/Expect adds data and
// calling Observe removes it.
remaining map[interface{}]struct{}
// When the watcher begins waiting for expected data
// to be observed, closable set to true.
closable bool
// When closable is true and all remaining data is
// observed, this channel is closed to signal completion.
finished chan struct{}
finishedOnce sync.Once
// If true, tests that the observed data is in the exact
// order of the expected data.
verifyOrder bool
}
// TestingT is an interface wrapper around *testing.T
type TestingT interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fail()
FailNow()
}
// ErrTimeout denotes that the Watcher timed out
// waiting for remaining data to be observed.
var ErrTimeout = errors.New("timeout")
// NewOrdered creates a Watcher that expects
// observed data to match the ordering of the
// expected data.
func NewOrdered() *Watcher {
return New(true)
}
func NewUnordered() *Watcher {
return New(false)
}
func New(verifyOrder bool) *Watcher {
return &Watcher{
expected: make([]interface{}, 0, 1000),
observed: make([]interface{}, 0, 1000),
remaining: make(map[interface{}]struct{}, 1000),
finished: make(chan struct{}, 1),
verifyOrder: verifyOrder,
}
}
// Reset clears all the underlying state and returns
// the watcher to a initial state.
func (w *Watcher) Reset() {
w.mu.Lock()
defer w.mu.Unlock()
w.expected = make([]interface{}, 0, 1000)
w.observed = make([]interface{}, 0, 1000)
w.remaining = make(map[interface{}]struct{}, 1000)
w.closable = false
w.finished = make(chan struct{}, 1)
w.finishedOnce = sync.Once{}
}
// Prepare is called before a network operation
// is called to add expected `data` to the `remaining` map.
// This is so that Observe can verify the data is expected
// and add it to the `observed` slice.
// Use Prepare and Add together when created expected data
// while a separate goroutine that calls Observer is running.
func (w *Watcher) Prepare(data ...interface{}) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.remaining[item] = struct{}{}
}
}
// Add is called after the network operation completes
// successfully and adds `data` to the `expected` slice
// so that it can be compared to the `observed` data
// at the end of the test scenario.
// Use Prepare and Add together when created expected data
// while a separate goroutine that calls Observer is running.
func (w *Watcher) Add(data ...interface{}) {
w.mu.Lock()
defer w.mu.Unlock()
w.expected = append(w.expected, data...)
}
// Remove is called if the network operation fails
// and removes `data` from the `remaining` map added
// during `Prepare`. This is so that if the `Publish` '
// operation fails, `data` added for tracking could be
// removed afterwards.
func (w *Watcher) Remove(data ...interface{}) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
delete(w.remaining, item)
}
}
// Expect adds data to both the `remaining` map
// add the expected slice in a single call.
// Use this only when a test scenario can prepare
// the expected data prior to an Observe calls.
func (w *Watcher) Expect(data ...interface{}) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.remaining[item] = struct{}{}
}
w.expected = append(w.expected, data...)
}
// ExpectStrings provides a simple function to
// add expected strings.
func (w *Watcher) ExpectStrings(data ...string) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectInts provides a simple function to
// add expected integers.
func (w *Watcher) ExpectInts(data ...int) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectI64s provides a simple function to
// add expected int64s.
func (w *Watcher) ExpectI64s(data ...int64) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectI32s provides a simple function to
// add expected int32s.
func (w *Watcher) ExpectI32s(data ...int32) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectI16s provides a simple function to
// add expected int16s.
func (w *Watcher) ExpectI16s(data ...int16) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectI8s provides a simple function to
// add expected int8s.
func (w *Watcher) ExpectI8s(data ...int8) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectUInts provides a simple function to
// add expected unsigned integers.
func (w *Watcher) ExpectUInts(data ...uint) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectU64s provides a simple function to
// add expected uint64s.
func (w *Watcher) ExpectU64s(data ...uint64) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectU32s provides a simple function to
// add expected uint32s.
func (w *Watcher) ExpectU32s(data ...uint32) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectU16s provides a simple function to
// add expected uint16s.
func (w *Watcher) ExpectU16s(data ...uint16) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectBytes provides a simple function to
// add expected bytes.
func (w *Watcher) ExpectBytes(data ...byte) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// ExpectRunes provides a simple function to
// add expected runes.
func (w *Watcher) ExpectRunes(data ...rune) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
w.expected = append(w.expected, item)
w.remaining[item] = struct{}{}
}
}
// Observe adds any data that is in `remaining` to
// the `observed` slice. If the the watcher is closable
// (all expected data captured) and there is no more
// remaining data to observe, then the finish channel
// is closed.
func (w *Watcher) Observe(data ...interface{}) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
if _, ok := w.remaining[item]; ok {
w.observed = append(w.observed, item)
delete(w.remaining, item)
}
}
if w.closable && len(w.remaining) == 0 {
w.finish()
}
}
// ObserveJSON adds any json data that is in `remaining` to
// the `observed` slice. If the the watcher is closable
// (all expected data captured) and there is no more
// remaining data to observe, then the finish channel
// is closed.
func (w *Watcher) ObserveJSON(data ...interface{}) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
b, _ := json.Marshal(&item)
str := string(b)
if _, ok := w.remaining[str]; ok {
w.observed = append(w.observed, str)
delete(w.remaining, str)
}
}
if w.closable && len(w.remaining) == 0 {
w.finish()
}
}
// WaitForResult waits for up to `timeout` for all
// expected data to be observed and returns an error
// if expected and observed data differ.
func (w *Watcher) WaitForResult(timeout time.Duration) error {
w.checkClosable()
select {
case <-time.After(timeout):
return ErrTimeout
case <-w.finished:
w.mu.Lock()
diff := cmp.Diff(w.expected, w.observed)
w.mu.Unlock()
if len(diff) > 0 {
return errors.New(diff)
}
}
return nil
}
func (w *Watcher) FailIfNotExpected(t TestingT, data ...interface{}) {
w.mu.Lock()
defer w.mu.Unlock()
for _, item := range data {
_, ok := w.remaining[item]
if !ok {
assert.Fail(t, fmt.Sprintf("Encountered an unexpected item: %v", item), item)
}
}
}
// Result waits for up to `timeout` for all
// expected data to be observed and returns
// the expected and observed slices.
func (w *Watcher) Result(t TestingT, timeout time.Duration) (TestingT, interface{}, interface{}) {
w.checkClosable()
select {
case <-time.After(timeout):
w.mu.Lock()
remainingCount := len(w.remaining)
w.mu.Unlock()
t.Errorf("Timed out with %d items remaining", remainingCount)
t.Fail()
return t, nil, nil
case <-w.finished:
w.mu.Lock()
defer w.mu.Unlock()
return t, w.expected, w.observed
}
}
// Partial waits for up to `timeout` for any
// expected data to be observed and returns
// the expected and observed slices even if
// not complete.
func (w *Watcher) Partial(t TestingT, timeout time.Duration) (TestingT, interface{}, interface{}) {
w.checkClosable()
select {
case <-time.After(timeout):
w.mu.Lock()
defer w.mu.Unlock()
return t, w.expected, w.observed
case <-w.finished:
w.mu.Lock()
defer w.mu.Unlock()
return t, w.expected, w.observed
}
}
// Assert waits for up to `timeout` for all
// expected data to be observed and asserts
// the expected and observed data are either
// equal (in order) or have matching elemenets
// (out of order is acceptable).
func (w *Watcher) Assert(t TestingT, timeout time.Duration) bool {
w.checkClosable()
select {
case <-time.After(timeout):
w.mu.Lock()
defer w.mu.Unlock()
t.Errorf("Timed out with %d items remaining: %v", len(w.remaining), w.remaining)
return false
case <-w.finished:
w.mu.Lock()
defer w.mu.Unlock()
if w.verifyOrder {
return assert.Equal(t, w.expected, w.observed)
}
return assert.ElementsMatch(t, w.expected, w.observed)
}
}
func (w *Watcher) AssertNotDelivered(t TestingT, timeout time.Duration) bool {
w.checkClosable()
select {
case <-time.After(timeout):
w.mu.Lock()
defer w.mu.Unlock()
return true
case <-w.finished:
w.mu.Lock()
defer w.mu.Unlock()
return len(w.observed) == 0
}
}
// Assert waits for up to `timeout` for all
// expected data to be observed and requires
// the expected and observed data are either
// equal (in order) or have matching elemenets
// (out of order is acceptable).
func (w *Watcher) Require(t TestingT, timeout time.Duration) {
w.checkClosable()
select {
case <-time.After(timeout):
w.mu.Lock()
remainingCount := len(w.remaining)
w.mu.Unlock()
t.Errorf("Timed out with %d items remaining", remainingCount)
require.FailNow(t, "timeout")
case <-w.finished:
w.mu.Lock()
defer w.mu.Unlock()
if w.verifyOrder {
require.Equal(t, w.expected, w.observed)
} else {
require.ElementsMatch(t, w.expected, w.observed)
}
}
}
func (w *Watcher) checkClosable() {
w.mu.Lock()
defer w.mu.Unlock()
w.closable = true
// Close the finished channel if observations
// are already complete.
if len(w.remaining) == 0 {
w.finish()
}
}
func (w *Watcher) finish() {
w.finishedOnce.Do(func() {
close(w.finished)
})
}