node_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dao
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "time"
  7. dc "go-common/app/infra/discovery/conf"
  8. "go-common/app/infra/discovery/model"
  9. "go-common/library/ecode"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/netutil/breaker"
  12. xtime "go-common/library/time"
  13. . "github.com/smartystreets/goconvey/convey"
  14. gock "gopkg.in/h2non/gock.v1"
  15. )
  16. func TestCall(t *testing.T) {
  17. Convey("test call", t, func() {
  18. var res *model.Instance
  19. node := newNode(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  20. Sleep: xtime.Duration(time.Millisecond * 100),
  21. Bucket: 10,
  22. Ratio: 0.5,
  23. Request: 100}, Timeout: xtime.Duration(time.Second), App: &bm.App{Key: "0c4b8fe3ff35a4b6", Secret: "b370880d1aca7d3a289b9b9a7f4d6812"}}, BM: &dc.HTTPServers{Inner: &bm.ServerConfig{Addr: "127.0.0.1:7171"}}, Nodes: []string{"127.0.0.1:7171"}}, "api.bilibili.co")
  24. node.client.SetTransport(gock.DefaultTransport)
  25. httpMock("POST", "http://api.bilibili.co/discovery/register").Reply(200).JSON(`{"ts":1514341945,"code":-409,"data":{"region":"shsb","zone":"fuck","appid":"main.arch.account-service","env":"pre","hostname":"cs4sq","http":"","rpc":"0.0.0.0:18888","weight":2}}`)
  26. i := model.NewInstance(reg)
  27. err := node.call(context.TODO(), model.Register, i, "http://api.bilibili.co/discovery/register", &res)
  28. So(err, ShouldResemble, ecode.Conflict)
  29. So(res.Appid, ShouldResemble, "main.arch.account-service")
  30. })
  31. }
  32. func TestNodeCancel(t *testing.T) {
  33. Convey("test node renew 409 error", t, func() {
  34. i := model.NewInstance(reg)
  35. node := newNode(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  36. Sleep: xtime.Duration(time.Millisecond * 100),
  37. Bucket: 10,
  38. Ratio: 0.5,
  39. Request: 100}, Timeout: xtime.Duration(time.Second), App: &bm.App{Key: "0c4b8fe3ff35a4b6", Secret: "b370880d1aca7d3a289b9b9a7f4d6812"}}, BM: &dc.HTTPServers{Inner: &bm.ServerConfig{Addr: "127.0.0.1:7171"}}, Nodes: []string{"127.0.0.1:7171"}}, "api.bilibili.co")
  40. node.pRegisterURL = "http://127.0.0.1:7171/discovery/register"
  41. node.client.SetTransport(gock.DefaultTransport)
  42. httpMock("POST", "http://api.bilibili.co/discovery/cancel").Reply(200).JSON(`{"code":0}`)
  43. err := node.Cancel(context.TODO(), i)
  44. So(err, ShouldBeNil)
  45. })
  46. }
  47. func TestNodeRenew(t *testing.T) {
  48. Convey("test node renew 409 error", t, func() {
  49. i := model.NewInstance(reg)
  50. node := newNode(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  51. Sleep: xtime.Duration(time.Millisecond * 100),
  52. Bucket: 10,
  53. Ratio: 0.5,
  54. Request: 100}, Timeout: xtime.Duration(time.Second), App: &bm.App{Key: "0c4b8fe3ff35a4b6", Secret: "b370880d1aca7d3a289b9b9a7f4d6812"}}, BM: &dc.HTTPServers{Inner: &bm.ServerConfig{Addr: "127.0.0.1:7171"}}, Nodes: []string{"127.0.0.1:7171"}}, "api.bilibili.co")
  55. node.pRegisterURL = "http://127.0.0.1:7171/discovery/register"
  56. node.client.SetTransport(gock.DefaultTransport)
  57. httpMock("POST", "http://api.bilibili.co/discovery/renew").Reply(200).JSON(`{"code":-409,"data":{"region":"shsb","zone":"fuck","appid":"main.arch.account-service","env":"pre","hostname":"cs4sq","http":"","rpc":"0.0.0.0:18888","weight":2}}`)
  58. httpMock("POST", "http://127.0.0.1:7171/discovery/register").Reply(200).JSON(`{"code":0}`)
  59. err := node.Renew(context.TODO(), i)
  60. So(err, ShouldBeNil)
  61. })
  62. }
  63. func TestNodeRenew2(t *testing.T) {
  64. Convey("test node renew 404 error", t, func() {
  65. i := model.NewInstance(reg)
  66. node := newNode(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  67. Sleep: xtime.Duration(time.Millisecond * 100),
  68. Bucket: 10,
  69. Ratio: 0.5,
  70. Request: 100}, Timeout: xtime.Duration(time.Second), App: &bm.App{Key: "0c4b8fe3ff35a4b6", Secret: "b370880d1aca7d3a289b9b9a7f4d6812"}}, BM: &dc.HTTPServers{Inner: &bm.ServerConfig{Addr: "127.0.0.1:7171"}}, Nodes: []string{"127.0.0.1:7171"}}, "api.bilibili.co")
  71. node.client.SetTransport(gock.DefaultTransport)
  72. httpMock("POST", "http://api.bilibili.co/discovery/renew").Reply(200).JSON(`{"code":-404}`)
  73. httpMock("POST", "http://api.bilibili.co/discovery/register").Reply(200).JSON(`{"code":0}`)
  74. err := node.Renew(context.TODO(), i)
  75. So(err, ShouldBeNil)
  76. })
  77. }
  78. func TestSet(t *testing.T) {
  79. Convey("test set", t, func() {
  80. node := newNode(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  81. Sleep: xtime.Duration(time.Millisecond * 100),
  82. Bucket: 10,
  83. Ratio: 0.5,
  84. Request: 100}, Timeout: xtime.Duration(time.Second), App: &bm.App{Key: "0c4b8fe3ff35a4b6", Secret: "b370880d1aca7d3a289b9b9a7f4d6812"}}, BM: &dc.HTTPServers{Inner: &bm.ServerConfig{Addr: "127.0.0.1:7171"}}, Nodes: []string{"127.0.0.1:7171"}}, "api.bilibili.co")
  85. node.client.SetTransport(gock.DefaultTransport)
  86. httpMock("POST", "http://api.bilibili.co/discovery/set").Reply(200).JSON(`{"ts":1514341945,"code":0}`)
  87. set := &model.ArgSet{
  88. Region: "shsb",
  89. Env: "pre",
  90. Appid: "main.arch.account-service",
  91. Hostname: []string{"test1"},
  92. Status: []int64{1},
  93. }
  94. err := node.Set(context.TODO(), set)
  95. So(err, ShouldBeNil)
  96. })
  97. }
  98. func httpMock(method, url string) *gock.Request {
  99. r := gock.New(url)
  100. r.Method = strings.ToUpper(method)
  101. return r
  102. }