From c144a362fde8f460a01bf87ef71d873b40ecc87e Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 23 Sep 2019 18:13:21 +0800 Subject: [PATCH] add UT (#83) --- archaius_test.go | 71 ++++++++++++++++++++++++++++++++++--- source/file/file.go | 11 +----- source/file/file_test.go | 7 ++++ source/manager_test.go | 1 - source/unmarshal.go | 6 ++-- source/util/file_handler.go | 12 ++++++- 6 files changed, 88 insertions(+), 20 deletions(-) delete mode 100755 source/manager_test.go diff --git a/archaius_test.go b/archaius_test.go index 95408189..ab71bfb7 100755 --- a/archaius_test.go +++ b/archaius_test.go @@ -28,6 +28,7 @@ name: peter f2Bytes := []byte(` addr: somewhere number: 1 +exist: true `) d, _ := os.Getwd() filename1 := filepath.Join(d, "f1.yaml") @@ -41,7 +42,6 @@ number: 1 defer f2.Close() defer os.Remove(filename2) _, err = io.WriteString(f1, string(f1Bytes)) - t.Log(string(f1Bytes)) assert.NoError(t, err) _, err = io.WriteString(f2, string(f2Bytes)) assert.NoError(t, err) @@ -64,17 +64,39 @@ number: 1 } func TestAddFile(t *testing.T) { - s := archaius.Get("number") - assert.Equal(t, 1, s) } func TestConfig_Get(t *testing.T) { + s := archaius.Get("number") + assert.Equal(t, 1, s) + + e := archaius.GetBool("exist", false) + assert.Equal(t, true, e) + n := archaius.Get("name") assert.Equal(t, "peter", n) + + n3 := archaius.GetString("name", "") + assert.Equal(t, "peter", n3) + + n2 := archaius.GetValue("name") + name, err := n2.ToString() + assert.NoError(t, err) + assert.Equal(t, "peter", name) + + b := archaius.Exist("name") + assert.True(t, b) + + b = archaius.Exist("none") + assert.False(t, b) + + m := archaius.GetConfigs() + t.Log(m) + assert.Equal(t, 1, m["number"]) } func TestConfig_GetInt(t *testing.T) { - s := archaius.Get("age") - assert.Equal(t, "15", s) + s := archaius.GetInt("number", 0) + assert.Equal(t, 1, s) } func TestConfig_RegisterListener(t *testing.T) { eventHandler := EListener{} @@ -82,6 +104,45 @@ func TestConfig_RegisterListener(t *testing.T) { assert.NoError(t, err) defer archaius.UnRegisterListener(eventHandler, "a*") +} + +func TestUnmarshalConfig(t *testing.T) { + b := []byte(` +key: peter +info: + address: a + number: 8 +metadata: + a: b +`) + d, _ := os.Getwd() + filename1 := filepath.Join(d, "f3.yaml") + f1, err := os.Create(filename1) + assert.NoError(t, err) + defer f1.Close() + defer os.Remove(filename1) + _, err = io.WriteString(f1, string(b)) + assert.NoError(t, err) + + type Info struct { + Addr string `yaml:"address"` + Number int `yaml:"number"` + } + type Person struct { + Name string `yaml:"key"` + MD map[string]string `yaml:"metadata"` + Info *Info `yaml:"info"` + } + err = archaius.AddFile(filename1) + assert.NoError(t, err) + p := &Person{} + err = archaius.UnmarshalConfig(p) + assert.NoError(t, err) + assert.Equal(t, "peter", p.Name) + assert.Equal(t, "b", p.MD["a"]) + assert.Equal(t, "a", p.Info.Addr) + assert.Equal(t, 8, p.Info.Number) + } func TestInitConfigCenter(t *testing.T) { err := archaius.EnableRemoteSource(&archaius.RemoteInfo{}, nil) diff --git a/source/file/file.go b/source/file/file.go index e91de35c..7cf2d1bd 100755 --- a/source/file/file.go +++ b/source/file/file.go @@ -561,21 +561,12 @@ func (fSource *Source) Cleanup() error { fSource.filelock.Lock() defer fSource.filelock.Unlock() - if fSource == nil { - return nil - } - if fSource.watchPool != nil && fSource.watchPool.watcher != nil { fSource.watchPool.watcher.Close() } - if fSource.watchPool != nil { - fSource.watchPool.fileSource = nil - fSource.watchPool.callback = nil - fSource.watchPool = nil - } - fSource.Configurations = nil fSource.files = make([]file, 0) + fSource.Configurations = make(map[string]*ConfigInfo, 0) return nil } diff --git a/source/file/file_test.go b/source/file/file_test.go index 55802cab..217be906 100755 --- a/source/file/file_test.go +++ b/source/file/file_test.go @@ -124,4 +124,11 @@ others: assert.NoError(t, err) assert.Equal(t, 13, age) }) + + t.Run("clean up", func(t *testing.T) { + err := fSource.Cleanup() + assert.NoError(t, err) + age, err := fSource.GetConfigurationByKey("age") + assert.Equal(t, nil, age) + }) } diff --git a/source/manager_test.go b/source/manager_test.go deleted file mode 100755 index 1dd3531f..00000000 --- a/source/manager_test.go +++ /dev/null @@ -1 +0,0 @@ -package source_test diff --git a/source/unmarshal.go b/source/unmarshal.go index c254cf64..8c4a8450 100755 --- a/source/unmarshal.go +++ b/source/unmarshal.go @@ -247,8 +247,8 @@ func (m *Manager) getMapKeys(configValue map[string]interface{}, prefix string, } } else { for key := range configValue { - isPrifix, index := checkPrefix(key, prefix) - if !isPrifix { + isPrefix, index := checkPrefix(key, prefix) + if !isPrefix { continue } @@ -575,7 +575,7 @@ func ToRvalueType(confValue interface{}, convertType reflect.Type) (returnValue } returnValue.SetBool(returnBool) default: - err = errors.New("canot convert type") + err = errors.New("can not convert type") } return returnValue, err diff --git a/source/util/file_handler.go b/source/util/file_handler.go index 41c0e687..de7882af 100644 --- a/source/util/file_handler.go +++ b/source/util/file_handler.go @@ -2,6 +2,7 @@ package util import ( "fmt" + "github.com/go-mesh/openlogging" "gopkg.in/yaml.v2" "path/filepath" ) @@ -40,7 +41,16 @@ func retrieveItems(prefix string, subItems yaml.MapSlice) map[string]interface{} result[k] = v } } else { - result[prefix+item.Key.(string)] = item.Value + k, ok := item.Key.(string) + if !ok { + openlogging.Error("yaml path is not string", openlogging.WithTags( + openlogging.Tags{ + "key": item.Key, + }, + )) + continue + } + result[prefix+k] = item.Value } }