Repository navigation
Expand file tree
/
Copy pathparser_watch_test.go
More file actions
76 lines (68 loc) · 1.88 KB
/
Copy pathparser_watch_test.go
File metadata and controls
76 lines (68 loc) · 1.88 KB
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
package tinyconf
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/insei/fmap/v3"
"github.com/stretchr/testify/assert"
)
// burstWatchMockDriver reports several changes while the first one is still handled,
// its value is changed after every reading.
type burstWatchMockDriver struct {
handling chan struct{}
reported chan struct{}
reads atomic.Int32
}
func (md *burstWatchMockDriver) GenDoc(...*Registered) string { return "" }
func (md *burstWatchMockDriver) GetName() string { return "burst" }
func (md *burstWatchMockDriver) GetValue(fmap.Field) (*Value, error) {
return &Value{Source: "burst", Value: fmt.Sprint(md.reads.Add(1))}, nil
}
func (md *burstWatchMockDriver) Watch(ctx context.Context, onChange func()) error {
onChange()
// the first change is taken for handling, the next is waiting, the others are dropped
<-md.handling
onChange()
onChange()
onChange()
close(md.reported)
<-ctx.Done()
return nil
}
func TestSubscribe_ChangesBurst(t *testing.T) {
type Config struct {
Name string
}
driver := &burstWatchMockDriver{handling: make(chan struct{}), reported: make(chan struct{})}
c, err := New(WithDriver(driver))
assert.NoError(t, err)
assert.NoError(t, c.Register(&Config{}))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
handled := make(chan struct{}, 10)
first := true
assert.NoError(t, Subscribe(ctx, c, func(prev, next *Config) {
if first {
first = false
close(driver.handling)
<-driver.reported
}
handled <- struct{}{}
}))
// four changes are reported, but the config is reloaded only twice
for i := 0; i < 2; i++ {
select {
case <-handled:
case <-time.After(5 * time.Second):
t.Fatal("change is not handled")
}
}
select {
case <-handled:
t.Fatal("dropped change is handled")
case <-time.After(50 * time.Millisecond):
}
assert.Equal(t, int32(2), driver.reads.Load())
}