default_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "time"
  10. "gogs.baozhida.cn/zoie/OAuth-core/config/source"
  11. "gogs.baozhida.cn/zoie/OAuth-core/config/source/env"
  12. "gogs.baozhida.cn/zoie/OAuth-core/config/source/file"
  13. "gogs.baozhida.cn/zoie/OAuth-core/config/source/memory"
  14. )
  15. func createFileForIssue18(t *testing.T, content string) *os.File {
  16. data := []byte(content)
  17. path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
  18. fh, err := os.Create(path)
  19. if err != nil {
  20. t.Error(err)
  21. }
  22. _, err = fh.Write(data)
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. return fh
  27. }
  28. func createFileForTest(t *testing.T) *os.File {
  29. data := []byte(`{"foo": "bar"}`)
  30. path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
  31. fh, err := os.Create(path)
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. _, err = fh.Write(data)
  36. if err != nil {
  37. t.Error(err)
  38. }
  39. return fh
  40. }
  41. func TestConfigLoadWithGoodFile(t *testing.T) {
  42. fh := createFileForTest(t)
  43. path := fh.Name()
  44. defer func() {
  45. fh.Close()
  46. os.Remove(path)
  47. }()
  48. // Create new config
  49. conf, err := NewConfig()
  50. if err != nil {
  51. t.Fatalf("Expected no error but got %v", err)
  52. }
  53. // Load file source
  54. if err := conf.Load(file.NewSource(
  55. file.WithPath(path),
  56. )); err != nil {
  57. t.Fatalf("Expected no error but got %v", err)
  58. }
  59. }
  60. func TestConfigLoadWithInvalidFile(t *testing.T) {
  61. fh := createFileForTest(t)
  62. path := fh.Name()
  63. defer func() {
  64. fh.Close()
  65. os.Remove(path)
  66. }()
  67. // Create new config
  68. conf, err := NewConfig()
  69. if err != nil {
  70. t.Fatalf("Expected no error but got %v", err)
  71. }
  72. // Load file source
  73. err = conf.Load(file.NewSource(
  74. file.WithPath(path),
  75. file.WithPath("/i/do/not/exists.json"),
  76. ))
  77. if err == nil {
  78. t.Fatal("Expected error but none !")
  79. }
  80. if !strings.Contains(fmt.Sprintf("%v", err), "/i/do/not/exists.json") {
  81. t.Fatalf("Expected error to contain the unexisting file but got %v", err)
  82. }
  83. }
  84. func TestConfigMerge(t *testing.T) {
  85. fh := createFileForIssue18(t, `{
  86. "amqp": {
  87. "host": "rabbit.platform",
  88. "port": 80
  89. },
  90. "handler": {
  91. "exchange": "springCloudBus"
  92. }
  93. }`)
  94. path := fh.Name()
  95. defer func() {
  96. fh.Close()
  97. os.Remove(path)
  98. }()
  99. os.Setenv("AMQP_HOST", "rabbit.testing.com")
  100. conf, err := NewConfig()
  101. if err != nil {
  102. t.Fatalf("Expected no error but got %v", err)
  103. }
  104. if err := conf.Load(
  105. file.NewSource(
  106. file.WithPath(path),
  107. ),
  108. env.NewSource(),
  109. ); err != nil {
  110. t.Fatalf("Expected no error but got %v", err)
  111. }
  112. actualHost := conf.Get("amqp", "host").String("backup")
  113. if actualHost != "rabbit.testing.com" {
  114. t.Fatalf("Expected %v but got %v",
  115. "rabbit.testing.com",
  116. actualHost)
  117. }
  118. }
  119. func equalS(t *testing.T, actual, expect string) {
  120. if actual != expect {
  121. t.Errorf("Expected %s but got %s", actual, expect)
  122. }
  123. }
  124. func TestConfigWatcherDirtyOverrite(t *testing.T) {
  125. n := runtime.GOMAXPROCS(0)
  126. defer runtime.GOMAXPROCS(n)
  127. runtime.GOMAXPROCS(1)
  128. l := 100
  129. ss := make([]source.Source, l, l)
  130. for i := 0; i < l; i++ {
  131. ss[i] = memory.NewSource(memory.WithJSON([]byte(fmt.Sprintf(`{"key%d": "val%d"}`, i, i))))
  132. }
  133. conf, _ := NewConfig()
  134. for _, s := range ss {
  135. _ = conf.Load(s)
  136. }
  137. runtime.Gosched()
  138. for i, _ := range ss {
  139. k := fmt.Sprintf("key%d", i)
  140. v := fmt.Sprintf("val%d", i)
  141. equalS(t, conf.Get(k).String(""), v)
  142. }
  143. }