nodes_test.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package dao
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. dc "go-common/app/infra/discovery/conf"
  7. "go-common/app/infra/discovery/model"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/netutil/breaker"
  10. xtime "go-common/library/time"
  11. . "github.com/smartystreets/goconvey/convey"
  12. gock "gopkg.in/h2non/gock.v1"
  13. )
  14. func TestReplicate(t *testing.T) {
  15. Convey("test replicate", t, func() {
  16. i := model.NewInstance(reg)
  17. nodes := NewNodes(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  18. Sleep: xtime.Duration(time.Millisecond * 100),
  19. Bucket: 10,
  20. Ratio: 0.5,
  21. 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{"api.bilibili.co", "uat-bilibili.co", "127.0.0.1:7171"}})
  22. nodes.nodes[0].client.SetTransport(gock.DefaultTransport)
  23. nodes.nodes[1].client.SetTransport(gock.DefaultTransport)
  24. httpMock("POST", "http://api.bilibili.co/discovery/register").Reply(200).JSON(`{"code":0}`)
  25. httpMock("POST", "http://uat-bilibili.co/discovery/register").Reply(200).JSON(`{"code":0}`)
  26. err := nodes.Replicate(context.TODO(), model.Register, i, false)
  27. So(err, ShouldBeNil)
  28. })
  29. }
  30. func TestReplicateSet(t *testing.T) {
  31. Convey("test replicate set", t, func() {
  32. nodes := NewNodes(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  33. Sleep: xtime.Duration(time.Millisecond * 100),
  34. Bucket: 10,
  35. Ratio: 0.5,
  36. 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{"api.bilibili.co"}})
  37. nodes.nodes[0].client.SetTransport(gock.DefaultTransport)
  38. httpMock("POST", "http://api.bilibili.co/discovery/set").Reply(200).JSON(`{"code":0}`)
  39. set := &model.ArgSet{
  40. Region: "shsb",
  41. Env: "pre",
  42. Appid: "main.arch.account-service",
  43. Hostname: []string{"test1"},
  44. Status: []int64{1},
  45. }
  46. err := nodes.ReplicateSet(context.TODO(), set, false)
  47. So(err, ShouldBeNil)
  48. })
  49. }
  50. func TestNodes(t *testing.T) {
  51. Convey("test replicate set", t, func() {
  52. nodes := NewNodes(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  53. Sleep: xtime.Duration(time.Millisecond * 100),
  54. Bucket: 10,
  55. Ratio: 0.5,
  56. 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{"api.bilibili.co", "uat-bilibili.co", "127.0.0.1:7171"}})
  57. res := nodes.Nodes()
  58. So(len(res), ShouldResemble, 3)
  59. })
  60. }
  61. func TestUp(t *testing.T) {
  62. Convey("test up", t, func() {
  63. nodes := NewNodes(&dc.Config{HTTPClient: &bm.ClientConfig{Breaker: &breaker.Config{Window: xtime.Duration(time.Second),
  64. Sleep: xtime.Duration(time.Millisecond * 100),
  65. Bucket: 10,
  66. Ratio: 0.5,
  67. 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{"api.bilibili.co", "uat-bilibili.co", "127.0.0.1:7171"}})
  68. nodes.UP()
  69. for _, nd := range nodes.nodes {
  70. if nd.addr == "127.0.0.1:7171" {
  71. So(nd.status, ShouldResemble, model.NodeStatusUP)
  72. }
  73. }
  74. })
  75. }