Skip to content

Commit

Permalink
fix panic when update with slice / map val (#156)
Browse files Browse the repository at this point in the history
* fix panic when update with slice / map val

* use t.Run in UT

Co-authored-by: zhuohuang yu <[email protected]>
  • Loading branch information
x-xy-y and zhuohuang yu authored Mar 9, 2022
1 parent e216fe5 commit 318adb6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 20 additions & 3 deletions archaius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func (e EListener) Event(event *event.Event) {
openlog.Info(fmt.Sprintf("config value after change %s |%s", event.Key, event.Value))
}

var filename2 string

func TestInit(t *testing.T) {
f1Bytes := []byte(`
age: 14
Expand All @@ -33,7 +31,7 @@ exist: true
`)
d, _ := os.Getwd()
filename1 := filepath.Join(d, "f1.yaml")
filename2 = filepath.Join(d, "f2.yaml")
filename2 := filepath.Join(d, "f2.yaml")
f1, err := os.Create(filename1)
assert.NoError(t, err)
defer f1.Close()
Expand Down Expand Up @@ -116,6 +114,25 @@ func TestConfig_RegisterListener(t *testing.T) {

}

func TestConfig_Update(t *testing.T) {
t.Run("update a simple type config", func(t *testing.T) {
assert.NoError(t, archaius.Set("aNumber", 1))
assert.NoError(t, archaius.Set("aNumber", 2))
})
t.Run("update a slice config", func(t *testing.T) {
assert.NoError(t, archaius.Set("aSlice", []int{1,2,3}))
assert.NoError(t, archaius.Set("aSlice", []int{1,2,3}))
assert.NoError(t, archaius.Set("aSlice", 1))
assert.NoError(t, archaius.Set("aSlice", []int{1,2,3}))
})
t.Run("update a map config", func(t *testing.T) {
assert.NoError(t, archaius.Set("aMap", map[int]int{1: 1}))
assert.NoError(t, archaius.Set("aMap", map[int]int{1: 1}))
assert.NoError(t, archaius.Set("aMap", 1))
assert.NoError(t, archaius.Set("aMap", map[int]int{1: 1}))
})
}

func TestUnmarshalConfig(t *testing.T) {
b := []byte(`
key: peter
Expand Down
4 changes: 2 additions & 2 deletions source/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ func (m *Manager) updateEventWithConfigUpdateLock(e *event.Event) error {
}
// we need to update cache if oldSrc != src || oldVal != val
m.updateCacheWithoutConfigUpdateLock(src.GetSourceName(), e.Key, val)
if oldVal == val {
openlog.Info(fmt.Sprintf("the key %s value %s is up-to-date, ignore value change", e.Key, val))
if reflect.DeepEqual(oldVal, val) {
openlog.Info(fmt.Sprintf("the key %q value %+v is up-to-date, ignore value change", e.Key, val))
return ErrIgnoreChange
}
}
Expand Down

0 comments on commit 318adb6

Please sign in to comment.