Skip to content

Commit

Permalink
add UT (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang authored Sep 23, 2019
1 parent 62343f6 commit c144a36
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 20 deletions.
71 changes: 66 additions & 5 deletions archaius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ name: peter
f2Bytes := []byte(`
addr: somewhere
number: 1
exist: true
`)
d, _ := os.Getwd()
filename1 := filepath.Join(d, "f1.yaml")
Expand All @@ -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)
Expand All @@ -64,24 +64,85 @@ 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{}
err := archaius.RegisterListener(eventHandler, "a*")
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)
Expand Down
11 changes: 1 addition & 10 deletions source/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions source/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
1 change: 0 additions & 1 deletion source/manager_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions source/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion source/util/file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"github.com/go-mesh/openlogging"
"gopkg.in/yaml.v2"
"path/filepath"
)
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit c144a36

Please sign in to comment.