client_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package conf
  2. import (
  3. "net/http"
  4. "testing"
  5. )
  6. func TestConf_client(t *testing.T) {
  7. c := initConf()
  8. testClientValue(t, c)
  9. testCheckVersion(t, c)
  10. testUpdate(t, c)
  11. testDownload(t, c)
  12. testGetConfig(t, c)
  13. }
  14. func TestClientNew(t *testing.T) {
  15. initConf()
  16. if _, err := New(); err != nil {
  17. t.Errorf("client.New() error(%v)", err)
  18. t.FailNow()
  19. }
  20. }
  21. func testClientValue(t *testing.T, c *Client) {
  22. key := "breaker"
  23. testUpdate(t, c)
  24. test1, ok := c.Value(key)
  25. if !ok {
  26. t.Errorf("client.Value() error")
  27. t.FailNow()
  28. }
  29. t.Logf("get the result test1(%s)", test1)
  30. }
  31. func testCheckVersion(t *testing.T, c *Client) {
  32. ver, err := c.checkVersion(_unknownVersion)
  33. if err != nil && ver == _unknownVersion {
  34. t.Errorf("client.checkVersion() error(%v) ver(%d)", err, ver)
  35. t.FailNow()
  36. }
  37. }
  38. func testDownload(t *testing.T, c *Client) {
  39. ver := int64(102)
  40. if err := c.download(ver); err != nil {
  41. t.Errorf("client.downloda() error(%v) ", err)
  42. t.FailNow()
  43. }
  44. }
  45. func testUpdate(t *testing.T, c *Client) {
  46. data := &data{
  47. Version: 199,
  48. Content: "{\"\":{\"name\":\"\",\"data\":{\"breaker\":\"fuck778\",\"degrade\":\"shit233333\"}},\"redis\":{\"name\":\"redis\",\"data\":{\"444\":\"555\",\"address\":\"172.123.0\",\"array\":\"4,12,test,4\",\"float\":\"3.123\",\"router\":\"test=1,fuck=shit,abc=test\",\"switch\":\"true\",\"timeout\":\"30s\"}}}",
  49. Md5: "0843192c43148cbbf43aabb24e3e6442",
  50. }
  51. if err := c.update(data); err != nil {
  52. t.Errorf("client.update() error(%v)", err)
  53. t.FailNow()
  54. }
  55. }
  56. func testGetConfig(t *testing.T, c *Client) {
  57. ver := int64(102)
  58. data, err := c.getConfig(ver)
  59. if err != nil {
  60. t.Errorf("client.getconfiig() error(%v)", err)
  61. t.FailNow()
  62. }
  63. t.Logf("get the result data(%v)", data)
  64. }
  65. func initConf() (c *Client) {
  66. conf.Addr = "172.16.33.134:9011"
  67. conf.Host = "testHost"
  68. conf.Path = "./"
  69. conf.Svr = "config_test"
  70. conf.Ver = "shsb-docker-1"
  71. conf.Env = "10"
  72. conf.Token = "qmVUPwNXnNfcSpuyqbiIBb0H4GcbSZFV"
  73. //conf.Appoint = "88"
  74. c = &Client{
  75. httpCli: &http.Client{Timeout: _httpTimeout},
  76. event: make(chan string, 10),
  77. }
  78. c.data.Store(make(map[string]*Namespace))
  79. return
  80. }