Skip to content

Commit 318adb6

Browse files
x-xy-yzhuohuang yu
andauthored
fix panic when update with slice / map val (#156)
* fix panic when update with slice / map val * use t.Run in UT Co-authored-by: zhuohuang yu <[email protected]>
1 parent e216fe5 commit 318adb6

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

archaius_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ func (e EListener) Event(event *event.Event) {
1919
openlog.Info(fmt.Sprintf("config value after change %s |%s", event.Key, event.Value))
2020
}
2121

22-
var filename2 string
23-
2422
func TestInit(t *testing.T) {
2523
f1Bytes := []byte(`
2624
age: 14
@@ -33,7 +31,7 @@ exist: true
3331
`)
3432
d, _ := os.Getwd()
3533
filename1 := filepath.Join(d, "f1.yaml")
36-
filename2 = filepath.Join(d, "f2.yaml")
34+
filename2 := filepath.Join(d, "f2.yaml")
3735
f1, err := os.Create(filename1)
3836
assert.NoError(t, err)
3937
defer f1.Close()
@@ -116,6 +114,25 @@ func TestConfig_RegisterListener(t *testing.T) {
116114

117115
}
118116

117+
func TestConfig_Update(t *testing.T) {
118+
t.Run("update a simple type config", func(t *testing.T) {
119+
assert.NoError(t, archaius.Set("aNumber", 1))
120+
assert.NoError(t, archaius.Set("aNumber", 2))
121+
})
122+
t.Run("update a slice config", func(t *testing.T) {
123+
assert.NoError(t, archaius.Set("aSlice", []int{1,2,3}))
124+
assert.NoError(t, archaius.Set("aSlice", []int{1,2,3}))
125+
assert.NoError(t, archaius.Set("aSlice", 1))
126+
assert.NoError(t, archaius.Set("aSlice", []int{1,2,3}))
127+
})
128+
t.Run("update a map config", func(t *testing.T) {
129+
assert.NoError(t, archaius.Set("aMap", map[int]int{1: 1}))
130+
assert.NoError(t, archaius.Set("aMap", map[int]int{1: 1}))
131+
assert.NoError(t, archaius.Set("aMap", 1))
132+
assert.NoError(t, archaius.Set("aMap", map[int]int{1: 1}))
133+
})
134+
}
135+
119136
func TestUnmarshalConfig(t *testing.T) {
120137
b := []byte(`
121138
key: peter

source/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ func (m *Manager) updateEventWithConfigUpdateLock(e *event.Event) error {
395395
}
396396
// we need to update cache if oldSrc != src || oldVal != val
397397
m.updateCacheWithoutConfigUpdateLock(src.GetSourceName(), e.Key, val)
398-
if oldVal == val {
399-
openlog.Info(fmt.Sprintf("the key %s value %s is up-to-date, ignore value change", e.Key, val))
398+
if reflect.DeepEqual(oldVal, val) {
399+
openlog.Info(fmt.Sprintf("the key %q value %+v is up-to-date, ignore value change", e.Key, val))
400400
return ErrIgnoreChange
401401
}
402402
}

0 commit comments

Comments
 (0)