123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package config
- import (
- "fmt"
- "os"
- "path/filepath"
- "runtime"
- "strings"
- "testing"
- "time"
- "gogs.baozhida.cn/zoie/OAuth-core/config/source"
- "gogs.baozhida.cn/zoie/OAuth-core/config/source/env"
- "gogs.baozhida.cn/zoie/OAuth-core/config/source/file"
- "gogs.baozhida.cn/zoie/OAuth-core/config/source/memory"
- )
- func createFileForIssue18(t *testing.T, content string) *os.File {
- data := []byte(content)
- path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
- fh, err := os.Create(path)
- if err != nil {
- t.Error(err)
- }
- _, err = fh.Write(data)
- if err != nil {
- t.Error(err)
- }
- return fh
- }
- func createFileForTest(t *testing.T) *os.File {
- data := []byte(`{"foo": "bar"}`)
- path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
- fh, err := os.Create(path)
- if err != nil {
- t.Error(err)
- }
- _, err = fh.Write(data)
- if err != nil {
- t.Error(err)
- }
- return fh
- }
- func TestConfigLoadWithGoodFile(t *testing.T) {
- fh := createFileForTest(t)
- path := fh.Name()
- defer func() {
- fh.Close()
- os.Remove(path)
- }()
- // Create new config
- conf, err := NewConfig()
- if err != nil {
- t.Fatalf("Expected no error but got %v", err)
- }
- // Load file source
- if err := conf.Load(file.NewSource(
- file.WithPath(path),
- )); err != nil {
- t.Fatalf("Expected no error but got %v", err)
- }
- }
- func TestConfigLoadWithInvalidFile(t *testing.T) {
- fh := createFileForTest(t)
- path := fh.Name()
- defer func() {
- fh.Close()
- os.Remove(path)
- }()
- // Create new config
- conf, err := NewConfig()
- if err != nil {
- t.Fatalf("Expected no error but got %v", err)
- }
- // Load file source
- err = conf.Load(file.NewSource(
- file.WithPath(path),
- file.WithPath("/i/do/not/exists.json"),
- ))
- if err == nil {
- t.Fatal("Expected error but none !")
- }
- if !strings.Contains(fmt.Sprintf("%v", err), "/i/do/not/exists.json") {
- t.Fatalf("Expected error to contain the unexisting file but got %v", err)
- }
- }
- func TestConfigMerge(t *testing.T) {
- fh := createFileForIssue18(t, `{
- "amqp": {
- "host": "rabbit.platform",
- "port": 80
- },
- "handler": {
- "exchange": "springCloudBus"
- }
- }`)
- path := fh.Name()
- defer func() {
- fh.Close()
- os.Remove(path)
- }()
- os.Setenv("AMQP_HOST", "rabbit.testing.com")
- conf, err := NewConfig()
- if err != nil {
- t.Fatalf("Expected no error but got %v", err)
- }
- if err := conf.Load(
- file.NewSource(
- file.WithPath(path),
- ),
- env.NewSource(),
- ); err != nil {
- t.Fatalf("Expected no error but got %v", err)
- }
- actualHost := conf.Get("amqp", "host").String("backup")
- if actualHost != "rabbit.testing.com" {
- t.Fatalf("Expected %v but got %v",
- "rabbit.testing.com",
- actualHost)
- }
- }
- func equalS(t *testing.T, actual, expect string) {
- if actual != expect {
- t.Errorf("Expected %s but got %s", actual, expect)
- }
- }
- func TestConfigWatcherDirtyOverrite(t *testing.T) {
- n := runtime.GOMAXPROCS(0)
- defer runtime.GOMAXPROCS(n)
- runtime.GOMAXPROCS(1)
- l := 100
- ss := make([]source.Source, l, l)
- for i := 0; i < l; i++ {
- ss[i] = memory.NewSource(memory.WithJSON([]byte(fmt.Sprintf(`{"key%d": "val%d"}`, i, i))))
- }
- conf, _ := NewConfig()
- for _, s := range ss {
- _ = conf.Load(s)
- }
- runtime.Gosched()
- for i, _ := range ss {
- k := fmt.Sprintf("key%d", i)
- v := fmt.Sprintf("val%d", i)
- equalS(t, conf.Get(k).String(""), v)
- }
- }
|