-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_test.go
364 lines (296 loc) · 8.81 KB
/
config_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
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
package gocore
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/ordishs/gocore/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetExistingKey(t *testing.T) {
name, ok := Config().Get("name")
assert.Equal(t, "Simon", name)
assert.True(t, ok)
}
func TestGetDuration(t *testing.T) {
d, err, ok := Config().GetDuration("millis")
require.NoError(t, err)
assert.Equal(t, 2*time.Second, d)
assert.True(t, ok)
_, err, ok = Config().GetDuration("millisErr")
require.Error(t, err)
assert.Equal(t, `time: unknown unit "fg" in duration "2fg"`, err.Error())
assert.False(t, ok)
}
func TestGetMulti(t *testing.T) {
res, ok := Config().GetMulti("multi", ",")
require.True(t, ok)
assert.Len(t, res, 3)
assert.Equal(t, "simon", res[0])
assert.Equal(t, "peter", res[1])
}
func TestGetEmbedded1(t *testing.T) {
res, ok := Config().Get("city")
require.True(t, ok)
assert.Equal(t, "Paris", res)
res, ok = Config().Get("embedded")
require.True(t, ok)
assert.Equal(t, "Simon lives in Paris", res)
t.Log(res)
}
func TestGetExistingKeyInt(t *testing.T) {
name, ok := Config().Get("tel")
assert.Equal(t, "20289202982", name)
assert.True(t, ok)
}
func TestGetNotExistingKey(t *testing.T) {
name, ok := Config().Get("XXXXX")
assert.Equal(t, "", name)
assert.False(t, ok)
}
func TestGetNotExistingKeyWithDefault(t *testing.T) {
name, ok := Config().Get("XXXXX", "FOUND")
assert.Equal(t, "FOUND", name)
assert.False(t, ok)
}
func TestGetNotExistingKeyWithDefaultInt(t *testing.T) {
name, ok := Config().GetInt("XXXXX", 72)
assert.Equal(t, 72, name)
assert.False(t, ok)
}
func TestGetOutboundIP(t *testing.T) {
_, err := utils.GetOutboundIP()
assert.NoError(t, err)
// assert.Equal(t, "172.20.40.202", ip.String())
}
func TestFilePath(t *testing.T) {
file := "/Users/ordishs/dev/go/gocore/settings.conf"
dir := filepath.Dir(file)
assert.Equal(t, "/Users/ordishs/dev/go/gocore", dir)
abs, _ := filepath.Abs(dir)
assert.Equal(t, "/Users/ordishs/dev/go/gocore", abs)
}
func TestGetSecretKey(t *testing.T) {
secret, ok := Config().Get("secret")
assert.Equal(t, "secret", secret)
assert.True(t, ok)
}
func TestGetMagicNumber(t *testing.T) {
secret, ok := Config().GetInt("magicNumber")
assert.Equal(t, 42, secret)
assert.True(t, ok)
}
func TestEncryptDecrypt(t *testing.T) {
res, _ := Config().Get("magicNumber")
assert.Equal(t, "42", res)
}
func TestEncryptDecryptInt(t *testing.T) {
res, _ := Config().GetInt("magicNumber")
assert.Equal(t, 42, res)
}
func TestURL1(t *testing.T) {
res, err, found := Config().GetURL("url1")
require.NoError(t, err)
password, set := res.User.Password()
require.True(t, set)
assert.Equalf(t, "http", res.Scheme, "scheme is wrong")
assert.Equalf(t, "user", res.User.Username(), "username is wrong")
assert.Equalf(t, "password", password, "password is wrong")
assert.Equalf(t, "localhost", res.Hostname(), "hostname is wrong")
assert.Equalf(t, "8080", res.Port(), "port is wrong")
assert.Equalf(t, "", res.Path, "path is wrong")
t.Logf("%v, %v", res, found)
}
func TestURLWithEncryptedPassword(t *testing.T) {
res, err, found := Config().GetURL("url2")
require.NoError(t, err)
password, set := res.User.Password()
require.True(t, set)
assert.Equalf(t, "http", res.Scheme, "scheme is wrong")
assert.Equalf(t, "user", res.User.Username(), "username is wrong")
assert.Equalf(t, "password", password, "password is wrong")
assert.Equalf(t, "localhost", res.Hostname(), "hostname is wrong")
assert.Equalf(t, "8080", res.Port(), "port is wrong")
assert.Equalf(t, "", res.Path, "path is wrong")
t.Logf("%v, %v", res, found)
}
func TestURL3(t *testing.T) {
res, err, found := Config().GetURL("url3")
require.NoError(t, err)
password, set := res.User.Password()
require.False(t, set)
assert.Equalf(t, "p2p", res.Scheme, "scheme is wrong")
assert.Equalf(t, "", res.User.Username(), "username is wrong")
assert.Equalf(t, "", password, "password is wrong")
assert.Equalf(t, "localhost", res.Hostname(), "hostname is wrong")
assert.Equalf(t, "8333", res.Port(), "port is wrong")
assert.Equalf(t, "", res.Path, "path is wrong")
t.Logf("%v, %v", res, found)
}
func TestURL4(t *testing.T) {
res, err, found := Config().GetURL("url4")
require.NoError(t, err)
password, set := res.User.Password()
require.False(t, set)
assert.Equalf(t, "zmq", res.Scheme, "scheme is wrong")
assert.Equalf(t, "", res.User.Username(), "username is wrong")
assert.Equalf(t, "", password, "password is wrong")
assert.Equalf(t, "localhost", res.Hostname(), "hostname is wrong")
assert.Equalf(t, "28332", res.Port(), "port is wrong")
assert.Equalf(t, "", res.Path, "path is wrong")
t.Logf("%v, %v", res, found)
}
// func TestGetContext(t *testing.T) {
// context := Config().GetContext()
// assert.Equalf(t, "dev", context, "context is wrong")
//}
func TestCheckDotEnv(t *testing.T) {
val, _ := Config().Get("ENV_TEST")
assert.Equal(t, "123", val)
}
func TestEmpty(t *testing.T) {
val, found := Config().Get("empty")
assert.True(t, found)
assert.Equal(t, "", val)
}
func TestAlternativeContext(t *testing.T) {
cSpecial := Config("special")
assert.Equal(t, "special", cSpecial.context)
v1, found := Config("special").Get("city")
assert.True(t, found)
assert.Equal(t, "Madrid", v1)
c := Config()
assert.Equal(t, os.Getenv("SETTINGS_CONTEXT"), c.context)
v, found := c.Get("city")
assert.True(t, found)
assert.Equal(t, "Paris", v)
}
func TestDynamicVariables(t *testing.T) {
v1, found := Config().Get("embedded")
assert.True(t, found)
assert.Equal(t, "Simon lives in Paris", v1)
Config().Set("city", "London")
v2, found := Config().Get("embedded")
assert.True(t, found)
assert.Equal(t, "Simon lives in London", v2)
}
func TestMissing(t *testing.T) {
val, found := Config().Get("missing")
assert.False(t, found)
assert.Equal(t, "", val)
}
func TestEmptyEnvOverride(t *testing.T) {
os.Setenv("city", "")
city := os.Getenv("city")
assert.Equal(t, "", city)
val, found := Config().Get("city")
assert.True(t, found)
assert.Equal(t, "", val)
}
func TestGetUint(t *testing.T) {
val, found := Config().GetUint("number")
assert.True(t, found)
assert.Equal(t, uint(5042), val)
}
func TestGetUint8(t *testing.T) {
val, found := Config().GetUint8("number")
assert.False(t, found)
assert.Equal(t, uint8(0), val)
val, found, err := Config().TryGetUint8("number")
assert.Error(t, err)
assert.True(t, found)
assert.Equal(t, uint8(0), val)
}
// func TestApplication(t *testing.T) {
// val, found := Config().Get("app")
// assert.True(t, found)
// assert.Equal(t, "gocore", val)
// t.Log(Config().Stats())
// }
type mockListener struct {
ch chan string
}
func newMockListener(bufferSize int) *mockListener {
return &mockListener{
ch: make(chan string, bufferSize),
}
}
func (m *mockListener) UpdateSetting(key, value string) {
select {
case m.ch <- fmt.Sprintf("%s=%s", key, value):
default:
// Channel is full or closed, don't block
}
}
func TestListener(t *testing.T) {
t.Run("basic listener functionality", func(t *testing.T) {
listener := newMockListener(1)
Config().AddListener(listener)
defer func() {
Config().RemoveListener(listener)
close(listener.ch)
}()
Config().Set("key1", "value1")
select {
case val := <-listener.ch:
assert.Equal(t, "key1=value1", val)
case <-time.After(time.Second):
t.Fatal("timeout waiting for listener update")
}
})
t.Run("multiple updates", func(t *testing.T) {
listener := newMockListener(2)
Config().AddListener(listener)
defer func() {
Config().RemoveListener(listener)
close(listener.ch)
}()
Config().Set("key1", "value1")
Config().Set("key2", "value2")
expected := []string{"key1=value1", "key2=value2"}
for i, want := range expected {
select {
case got := <-listener.ch:
assert.Equal(t, want, got, "update %d", i+1)
case <-time.After(time.Second):
t.Fatalf("timeout waiting for update %d", i+1)
}
}
})
t.Run("removed listener receives no updates", func(t *testing.T) {
listener := newMockListener(1)
Config().AddListener(listener)
Config().RemoveListener(listener)
close(listener.ch)
Config().Set("key", "value")
select {
case val, ok := <-listener.ch:
if ok {
t.Errorf("removed listener received update: %s", val)
}
default:
// Expected - no updates should be received
}
})
t.Run("full channel doesn't block", func(t *testing.T) {
listener := newMockListener(1)
Config().AddListener(listener)
defer func() {
Config().RemoveListener(listener)
close(listener.ch)
}()
// Fill the channel
Config().Set("key1", "value1")
// This should not block even though channel is full
Config().Set("key2", "value2")
// Should receive at least the first update
select {
case val := <-listener.ch:
assert.Equal(t, "key1=value1", val)
case <-time.After(time.Second):
t.Fatal("timeout waiting for listener update")
}
})
}