From 318adb66587963fce75f87ca185b19a59686644d Mon Sep 17 00:00:00 2001 From: x-xy-y Date: Wed, 9 Mar 2022 21:15:02 +0800 Subject: [PATCH] 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 --- archaius_test.go | 23 ++++++++++++++++++++--- source/manager.go | 4 ++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/archaius_test.go b/archaius_test.go index af9d4da..4ebec6a 100755 --- a/archaius_test.go +++ b/archaius_test.go @@ -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 @@ -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() @@ -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 diff --git a/source/manager.go b/source/manager.go index 89a05c1..9c7ce8e 100755 --- a/source/manager.go +++ b/source/manager.go @@ -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 } }