-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils_test.go
179 lines (157 loc) · 3.92 KB
/
utils_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
package common
import (
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestMinUint(t *testing.T) {
tests := []struct {
inputA, inputB uint
want uint
}{
{inputA: 1, inputB: 2, want: 1},
{inputA: 2, inputB: 1, want: 1},
{inputA: 0, inputB: 1, want: 0},
{inputA: 1, inputB: 0, want: 0},
}
for _, tc := range tests {
result := MinUint(tc.inputA, tc.inputB)
if result != tc.want {
t.Errorf(
"Expected %v < %v to be %v got %v", tc.inputA, tc.inputB, tc.want, result)
}
}
}
func TestEnsureDot(t *testing.T) {
tests := []struct {
input string
want string
}{
{input: "foo", want: "foo."},
{input: "foo.", want: "foo."},
{input: "", want: "."},
}
for _, tc := range tests {
result := EnsureDot(tc.input)
if result != tc.want {
t.Errorf(
"Expected %v got %v", tc.input, tc.want)
}
}
}
func TestRemoveDot(t *testing.T) {
tests := []struct {
input string
want string
}{
{input: "foo.", want: "foo"},
{input: "foo..", want: "foo."},
{input: ".", want: ""},
{input: "..", want: "."},
}
for _, tc := range tests {
result := RemoveDot(tc.input)
if result != tc.want {
t.Errorf(
"Expected %v got %v", tc.input, tc.want)
}
}
}
type Config struct {
Index int `yaml:"index"`
}
func TestLoadAndListenConfig_NonExistingFile(t *testing.T) {
err := LoadAndListenConfig("invalid.yaml", &ConfigLock{}, &Config{}, nil)
assert.ErrorContains(t, err, "no such file or directory")
}
func TestLoadAndListenConfig_InvalidSyntax(t *testing.T) {
err := LoadAndListenConfig("testdata/invalid.yaml", &ConfigLock{}, &Config{}, nil)
assert.ErrorContains(t, err, "invalid syntax")
}
func (u *UpdateValues) Set(updateCalls int, oldConf interface{}, notifyFn func(interface{})) {
u.mu.Lock()
defer u.mu.Unlock()
u.UpdateCalls += updateCalls
u.OldValue = oldConf.(Config).Index
notifyFn(oldConf)
}
func (u *UpdateValues) GetUpdateCalls() int {
u.mu.Lock()
defer u.mu.Unlock()
return u.UpdateCalls
}
func (u *UpdateValues) GetOldValue() int {
u.mu.Lock()
defer u.mu.Unlock()
return u.OldValue
}
type UpdateValues struct {
mu sync.Mutex
UpdateCalls int
OldValue int
}
func TestLoadAndListenConfigOnUpdate(t *testing.T) {
filePath := "testdata/test2.yaml"
data, err := yaml.Marshal(&Config{})
require.NoError(t, err)
err = os.WriteFile(filePath, data, 0o600)
require.NoError(t, err)
realConf := &Config{}
l := &ConfigLock{}
values := &UpdateValues{}
notifyFn, waitForUpdate := updateCallbacks()
err = LoadAndListenConfig(filePath, l, realConf, func(oldConf interface{}) {
values.Set(1, oldConf, notifyFn)
})
require.NoError(t, err)
l.lock.Lock()
assert.Equal(t, 0, realConf.Index)
l.lock.Unlock()
assert.Equal(t, 0, values.GetOldValue())
assert.Equal(t, 0, values.GetUpdateCalls())
data, err = yaml.Marshal(&Config{
Index: 1,
})
require.NoError(t, err)
err = os.WriteFile(filePath, data, 0o600)
require.NoError(t, err)
waitForUpdate(t)
l.lock.Lock()
assert.Equal(t, 1, realConf.Index)
l.lock.Unlock()
assert.Equal(t, 0, values.GetOldValue())
assert.Equal(t, 1, values.GetUpdateCalls())
data, err = yaml.Marshal(&Config{
Index: 2,
})
require.NoError(t, err)
err = os.WriteFile(filePath, data, 0o600)
require.NoError(t, err)
waitForUpdate(t)
l.lock.Lock()
assert.Equal(t, 2, realConf.Index)
l.lock.Unlock()
assert.Equal(t, 1, values.GetOldValue())
assert.Equal(t, 2, values.GetUpdateCalls())
}
func updateCallbacks() (func(interface{}), func(testing.TB)) {
// Give some buffer for channel in case
// viper decides to send multiple events.
ch := make(chan struct{}, 10)
notifier := func(interface{}) {
ch <- struct{}{}
}
waitForUpdate := func(t testing.TB) {
const onUpdateTimeout = time.Second * 2
select {
case <-ch:
case <-time.After(onUpdateTimeout):
t.Fatalf("OnUpdate not triggered with in %s", onUpdateTimeout)
}
}
return notifier, waitForUpdate
}