test config struct (#1212)

pull/1215/head
Kuiba 3 years ago committed by GitHub
parent ff24e18a94
commit 44dd641f28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 159
      config/config_test.go

@ -1,12 +1,171 @@
package config
import (
"errors"
"reflect"
"testing"
"github.com/go-kratos/kratos/v2/log"
"github.com/stretchr/testify/assert"
)
const (
_testJSON = `
{
"server":{
"http":{
"addr":"0.0.0.0",
"port":80,
"timeout":0.5,
"enable_ssl":true
},
"grpc":{
"addr":"0.0.0.0",
"port":10080,
"timeout":0.2
}
},
"data":{
"database":{
"driver":"mysql",
"source":"root:root@tcp(127.0.0.1:3306)/karta_id?parseTime=true"
}
},
"endpoints":[
"www.aaa.com",
"www.bbb.org"
]
}`
)
type testConfigStruct struct {
Server struct {
Http struct {
Addr string `json:"addr"`
Port int `json:"port"`
Timeout float64 `json:"timeout"`
EnableSSL bool `json:"enable_ssl"`
} `json:"http"`
GRpc struct {
Addr string `json:"addr"`
Port int `json:"port"`
Timeout float64 `json:"timeout"`
} `json:"grpc"`
} `json:"server"`
Data struct {
Database struct {
Driver string `json:"driver"`
Source string `json:"source"`
} `json:"database"`
} `json:"data"`
Endpoints []string `json:"endpoints"`
}
type testJsonSource struct {
data string
sig chan struct{}
err chan struct{}
}
func newTestJsonSource(data string) *testJsonSource {
return &testJsonSource{data: data, sig: make(chan struct{}), err: make(chan struct{})}
}
func (p *testJsonSource) Load() ([]*KeyValue, error) {
kv := &KeyValue{
Key: "json",
Value: []byte(p.data),
Format: "json",
}
return []*KeyValue{kv}, nil
}
func (p *testJsonSource) Watch() (Watcher, error) {
return newTestWatcher(p.sig, p.err), nil
}
type testWatcher struct {
sig chan struct{}
err chan struct{}
exit chan struct{}
}
func newTestWatcher(sig, err chan struct{}) Watcher {
return &testWatcher{sig: sig, err: err, exit: make(chan struct{})}
}
func (w *testWatcher) Next() ([]*KeyValue, error) {
select {
case <-w.sig:
return nil, nil
case <-w.err:
return nil, errors.New("error")
case <-w.exit:
return nil, nil
}
}
func (w *testWatcher) Stop() error {
close(w.exit)
return nil
}
func TestConfig(t *testing.T) {
var (
err error
httpAddr = "0.0.0.0"
httpTimeout = 0.5
grpcPort = 10080
enableSSL = true
endpoint1 = "www.aaa.com"
databaseDriver = "mysql"
)
c := New(
WithSource(newTestJsonSource(_testJSON)),
WithDecoder(defaultDecoder),
WithResolver(defaultResolver),
WithLogger(log.DefaultLogger),
)
err = c.Close()
assert.Nil(t, err)
jSource := newTestJsonSource(_testJSON)
opts := options{
sources: []Source{jSource},
decoder: defaultDecoder,
resolver: defaultResolver,
logger: log.DefaultLogger,
}
cf := &config{}
cf.opts = opts
cf.reader = newReader(opts)
err = cf.Load()
assert.Nil(t, err)
val, err := cf.Value("data.database.driver").String()
assert.Nil(t, err)
assert.Equal(t, databaseDriver, val)
err = cf.Watch("endpoints", func(key string, value Value) {
})
assert.Nil(t, err)
jSource.sig <- struct{}{}
jSource.err <- struct{}{}
var testConf testConfigStruct
err = cf.Scan(&testConf)
assert.Nil(t, err)
assert.Equal(t, httpAddr, testConf.Server.Http.Addr)
assert.Equal(t, httpTimeout, testConf.Server.Http.Timeout)
assert.Equal(t, enableSSL, testConf.Server.Http.EnableSSL)
assert.Equal(t, grpcPort, testConf.Server.GRpc.Port)
assert.Equal(t, endpoint1, testConf.Endpoints[0])
assert.Equal(t, 2, len(testConf.Endpoints))
}
func TestDefaultResolver(t *testing.T) {
var (
portString = "8080"

Loading…
Cancel
Save