client2_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package rpc
  2. import (
  3. "context"
  4. "net"
  5. "sync"
  6. "testing"
  7. "time"
  8. "go-common/library/conf/env"
  9. "go-common/library/naming"
  10. "go-common/library/naming/discovery"
  11. xtime "go-common/library/time"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. var c = &discovery.Config{
  15. Nodes: []string{"api.bilibili.co"},
  16. Zone: "sh001",
  17. Env: "test",
  18. Key: "0c4b8fe3ff35a4b6",
  19. Secret: "b370880d1aca7d3a289b9b9a7f4d6812",
  20. Host: "host_1",
  21. }
  22. var in = &naming.Instance{
  23. AppID: "test2",
  24. Version: "1",
  25. Metadata: map[string]string{
  26. "test": "1",
  27. "weight": "8",
  28. "color": "",
  29. "cluster": "red",
  30. },
  31. }
  32. var in2 = &naming.Instance{
  33. AppID: "test3",
  34. Version: "1",
  35. Metadata: map[string]string{
  36. "test": "1",
  37. "weight": "8",
  38. "color": "",
  39. "cluster": "red",
  40. },
  41. }
  42. var (
  43. svrAddr1, svrAddr2, svrAddr3 string
  44. once1, once2, once3 sync.Once
  45. )
  46. type TestArgs struct {
  47. A, B int
  48. }
  49. type TestReply struct {
  50. C int
  51. }
  52. type TestTimeout struct {
  53. T time.Duration
  54. }
  55. type TestRPC int
  56. func startTestServer1() {
  57. svr := newServer()
  58. svr.RegisterName("RPC", new(TestRPC))
  59. var l net.Listener
  60. l, svrAddr1 = listenTCP()
  61. go svr.Accept(l)
  62. }
  63. func TestDiscoveryCli(t *testing.T) {
  64. env.Hostname = "host_1"
  65. env.Zone = "sh001"
  66. once1.Do(startTestServer1)
  67. Convey("test discovery cli", t, func() {
  68. once1.Do(startTestServer1)
  69. in.Addrs = []string{scheme + "://" + svrAddr1}
  70. dis := discovery.New(c)
  71. _, err := dis.Register(context.TODO(), in)
  72. So(err, ShouldBeNil)
  73. cli := NewDiscoveryCli("test2", &ClientConfig{
  74. Cluster: "",
  75. Timeout: xtime.Duration(time.Second),
  76. })
  77. time.Sleep(time.Second * 2)
  78. args := &TestArgs{7, 8}
  79. reply := new(TestReply)
  80. err = cli.Call(context.TODO(), "RPC.Add", args, reply)
  81. So(err, ShouldBeNil)
  82. })
  83. Convey("test discovery no zone", t, func() {
  84. env.Zone = "test2"
  85. cli := NewDiscoveryCli("test2", &ClientConfig{
  86. Cluster: "",
  87. Timeout: xtime.Duration(time.Second),
  88. })
  89. time.Sleep(time.Second * 2)
  90. args := &TestArgs{7, 8}
  91. reply := new(TestReply)
  92. err := cli.Call(context.TODO(), "RPC.Add", args, reply)
  93. So(err, ShouldBeNil)
  94. })
  95. Convey("test discovery with color", t, func() {
  96. env.Zone = "test2"
  97. cli := NewDiscoveryCli("test2", &ClientConfig{
  98. Color: "red",
  99. Timeout: xtime.Duration(time.Second),
  100. })
  101. time.Sleep(time.Second * 2)
  102. args := &TestArgs{7, 8}
  103. reply := new(TestReply)
  104. err := cli.Call(context.TODO(), "RPC.Add", args, reply)
  105. So(err, ShouldBeNil)
  106. })
  107. Convey("test discovery with cluster", t, func() {
  108. env.Zone = "test2"
  109. cli := NewDiscoveryCli("test2", &ClientConfig{
  110. Cluster: "red",
  111. Timeout: xtime.Duration(time.Second),
  112. })
  113. time.Sleep(time.Second * 2)
  114. args := &TestArgs{7, 8}
  115. reply := new(TestReply)
  116. err := cli.Call(context.TODO(), "RPC.Add", args, reply)
  117. So(err, ShouldBeNil)
  118. })
  119. Convey("test conf Zone cli", t, func() {
  120. env.Zone = "testsh"
  121. once1.Do(startTestServer1)
  122. in2.Addrs = []string{scheme + "://" + svrAddr1}
  123. dis := discovery.New(c)
  124. _, err := dis.Register(context.TODO(), in2)
  125. So(err, ShouldBeNil)
  126. env.Zone = "sh001"
  127. cli := NewDiscoveryCli("test3", &ClientConfig{
  128. Cluster: "",
  129. Timeout: xtime.Duration(time.Second),
  130. Zone: "testsh",
  131. })
  132. time.Sleep(time.Second * 2)
  133. args := &TestArgs{7, 8}
  134. reply := new(TestReply)
  135. err = cli.Call(context.TODO(), "RPC.Add", args, reply)
  136. So(err, ShouldBeNil)
  137. })
  138. }