resovler_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package resolver
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "time"
  8. "go-common/library/conf/env"
  9. "go-common/library/naming"
  10. "go-common/library/net/netutil/breaker"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/rpc/warden/balancer/wrr"
  13. pb "go-common/library/net/rpc/warden/proto/testproto"
  14. "go-common/library/net/rpc/warden/resolver"
  15. xtime "go-common/library/time"
  16. "github.com/stretchr/testify/assert"
  17. "google.golang.org/grpc"
  18. )
  19. var testServerMap map[string]*testServer
  20. func init() {
  21. testServerMap = make(map[string]*testServer)
  22. }
  23. const testAppID = "main.test"
  24. type testServer struct {
  25. SayHelloCount int
  26. }
  27. func resetCount() {
  28. for _, s := range testServerMap {
  29. s.SayHelloCount = 0
  30. }
  31. }
  32. func (ts *testServer) SayHello(context.Context, *pb.HelloRequest) (*pb.HelloReply, error) {
  33. ts.SayHelloCount++
  34. return &pb.HelloReply{Message: "hello", Success: true}, nil
  35. }
  36. func (ts *testServer) StreamHello(ss pb.Greeter_StreamHelloServer) error {
  37. panic("not implement error")
  38. }
  39. func createServer(name, listen string) *warden.Server {
  40. s := warden.NewServer(&warden.ServerConfig{Timeout: xtime.Duration(time.Second)})
  41. ts := &testServer{}
  42. testServerMap[name] = ts
  43. pb.RegisterGreeterServer(s.Server(), ts)
  44. go func() {
  45. if err := s.Run(listen); err != nil {
  46. panic(fmt.Sprintf("run warden server fail! err: %s", err))
  47. }
  48. }()
  49. return s
  50. }
  51. func NSayHello(c pb.GreeterClient, n int) func(*testing.T) {
  52. return func(t *testing.T) {
  53. for i := 0; i < n; i++ {
  54. if _, err := c.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  55. t.Fatalf("call sayhello fail! err: %s", err)
  56. }
  57. }
  58. }
  59. }
  60. func createTestClient(t *testing.T) pb.GreeterClient {
  61. client := warden.NewClient(&warden.ClientConfig{
  62. Dial: xtime.Duration(time.Second * 10),
  63. Timeout: xtime.Duration(time.Second * 10),
  64. Breaker: &breaker.Config{
  65. Window: xtime.Duration(3 * time.Second),
  66. Sleep: xtime.Duration(3 * time.Second),
  67. Bucket: 10,
  68. Ratio: 0.3,
  69. Request: 20,
  70. },
  71. })
  72. conn, err := client.Dial(context.TODO(), "mockdiscovery://authority/main.test")
  73. if err != nil {
  74. t.Fatalf("create client fail!err%s", err)
  75. }
  76. return pb.NewGreeterClient(conn)
  77. }
  78. var mockResolver *mockDiscoveryBuilder
  79. func newMockDiscoveryBuilder() *mockDiscoveryBuilder {
  80. return &mockDiscoveryBuilder{
  81. instances: make(map[string]*naming.Instance),
  82. watchch: make(map[string][]*mockDiscoveryResolver),
  83. }
  84. }
  85. func TestMain(m *testing.M) {
  86. ctx := context.TODO()
  87. mockResolver = newMockDiscoveryBuilder()
  88. resolver.Set(mockResolver)
  89. s1 := createServer("server1", "127.0.0.1:18081")
  90. s2 := createServer("server2", "127.0.0.1:18082")
  91. s3 := createServer("server3", "127.0.0.1:18083")
  92. s4 := createServer("server4", "127.0.0.1:18084")
  93. s5 := createServer("server5", "127.0.0.1:18085")
  94. defer s1.Shutdown(ctx)
  95. defer s2.Shutdown(ctx)
  96. defer s3.Shutdown(ctx)
  97. defer s4.Shutdown(ctx)
  98. defer s5.Shutdown(ctx)
  99. os.Exit(m.Run())
  100. }
  101. func TestAddResolver(t *testing.T) {
  102. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{})
  103. c := createTestClient(t)
  104. t.Run("test_say_hello", NSayHello(c, 10))
  105. assert.Equal(t, 10, testServerMap["server1"].SayHelloCount)
  106. resetCount()
  107. }
  108. func TestDeleteResolver(t *testing.T) {
  109. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{})
  110. mockResolver.registry(testAppID, "server2", "127.0.0.1:18082", map[string]string{})
  111. c := createTestClient(t)
  112. t.Run("test_say_hello", NSayHello(c, 10))
  113. assert.Equal(t, 10, testServerMap["server1"].SayHelloCount+testServerMap["server2"].SayHelloCount)
  114. mockResolver.cancel("server1")
  115. resetCount()
  116. time.Sleep(time.Millisecond * 10)
  117. t.Run("test_say_hello", NSayHello(c, 10))
  118. assert.Equal(t, 0, testServerMap["server1"].SayHelloCount)
  119. resetCount()
  120. }
  121. func TestUpdateResolver(t *testing.T) {
  122. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{})
  123. mockResolver.registry(testAppID, "server2", "127.0.0.1:18082", map[string]string{})
  124. c := createTestClient(t)
  125. t.Run("test_say_hello", NSayHello(c, 10))
  126. assert.Equal(t, 10, testServerMap["server1"].SayHelloCount+testServerMap["server2"].SayHelloCount)
  127. mockResolver.registry(testAppID, "server1", "127.0.0.1:18083", map[string]string{})
  128. mockResolver.registry(testAppID, "server2", "127.0.0.1:18084", map[string]string{})
  129. resetCount()
  130. time.Sleep(time.Millisecond * 10)
  131. t.Run("test_say_hello", NSayHello(c, 10))
  132. assert.Equal(t, 0, testServerMap["server1"].SayHelloCount+testServerMap["server2"].SayHelloCount)
  133. assert.Equal(t, 10, testServerMap["server3"].SayHelloCount+testServerMap["server4"].SayHelloCount)
  134. resetCount()
  135. }
  136. func TestErrorResolver(t *testing.T) {
  137. mockResolver := newMockDiscoveryBuilder()
  138. resolver.Set(mockResolver)
  139. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{})
  140. mockResolver.registry(testAppID, "server6", "127.0.0.1:18086", map[string]string{})
  141. c := createTestClient(t)
  142. t.Run("test_say_hello", NSayHello(c, 10))
  143. assert.Equal(t, 10, testServerMap["server1"].SayHelloCount)
  144. resetCount()
  145. }
  146. func TestClusterResolver(t *testing.T) {
  147. mockResolver := newMockDiscoveryBuilder()
  148. resolver.Set(mockResolver)
  149. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{"cluster": "c1"})
  150. mockResolver.registry(testAppID, "server2", "127.0.0.1:18082", map[string]string{"cluster": "c1"})
  151. mockResolver.registry(testAppID, "server3", "127.0.0.1:18083", map[string]string{"cluster": "c2"})
  152. mockResolver.registry(testAppID, "server4", "127.0.0.1:18084", map[string]string{})
  153. mockResolver.registry(testAppID, "server5", "127.0.0.1:18084", map[string]string{})
  154. client := warden.NewClient(&warden.ClientConfig{Clusters: []string{"c1"}}, grpc.WithBalancerName(wrr.Name))
  155. conn, err := client.Dial(context.TODO(), "mockdiscovery://authority/main.test?cluster=c2")
  156. if err != nil {
  157. t.Fatalf("create client fail!err%s", err)
  158. }
  159. time.Sleep(time.Millisecond * 10)
  160. cli := pb.NewGreeterClient(conn)
  161. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  162. t.Fatalf("call sayhello fail! err: %s", err)
  163. }
  164. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  165. t.Fatalf("call sayhello fail! err: %s", err)
  166. }
  167. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  168. t.Fatalf("call sayhello fail! err: %s", err)
  169. }
  170. assert.Equal(t, 1, testServerMap["server1"].SayHelloCount)
  171. assert.Equal(t, 1, testServerMap["server2"].SayHelloCount)
  172. assert.Equal(t, 1, testServerMap["server3"].SayHelloCount)
  173. resetCount()
  174. }
  175. func TestNoClusterResolver(t *testing.T) {
  176. mockResolver := newMockDiscoveryBuilder()
  177. resolver.Set(mockResolver)
  178. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{"cluster": "c1"})
  179. mockResolver.registry(testAppID, "server2", "127.0.0.1:18082", map[string]string{"cluster": "c1"})
  180. mockResolver.registry(testAppID, "server3", "127.0.0.1:18083", map[string]string{"cluster": "c2"})
  181. mockResolver.registry(testAppID, "server4", "127.0.0.1:18084", map[string]string{})
  182. client := warden.NewClient(&warden.ClientConfig{}, grpc.WithBalancerName(wrr.Name))
  183. conn, err := client.Dial(context.TODO(), "mockdiscovery://authority/main.test")
  184. if err != nil {
  185. t.Fatalf("create client fail!err%s", err)
  186. }
  187. time.Sleep(time.Millisecond * 20)
  188. cli := pb.NewGreeterClient(conn)
  189. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  190. t.Fatalf("call sayhello fail! err: %s", err)
  191. }
  192. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  193. t.Fatalf("call sayhello fail! err: %s", err)
  194. }
  195. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  196. t.Fatalf("call sayhello fail! err: %s", err)
  197. }
  198. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  199. t.Fatalf("call sayhello fail! err: %s", err)
  200. }
  201. assert.Equal(t, 1, testServerMap["server1"].SayHelloCount)
  202. assert.Equal(t, 1, testServerMap["server2"].SayHelloCount)
  203. assert.Equal(t, 1, testServerMap["server3"].SayHelloCount)
  204. assert.Equal(t, 1, testServerMap["server4"].SayHelloCount)
  205. resetCount()
  206. }
  207. func TestZoneResolver(t *testing.T) {
  208. mockResolver := newMockDiscoveryBuilder()
  209. resolver.Set(mockResolver)
  210. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{})
  211. env.Zone = "testsh"
  212. mockResolver.registry(testAppID, "server2", "127.0.0.1:18082", map[string]string{})
  213. env.Zone = "hhhh"
  214. client := warden.NewClient(&warden.ClientConfig{Zone: "testsh"}, grpc.WithBalancerName(wrr.Name))
  215. conn, err := client.Dial(context.TODO(), "mockdiscovery://authority/main.test")
  216. if err != nil {
  217. t.Fatalf("create client fail!err%s", err)
  218. }
  219. time.Sleep(time.Millisecond * 10)
  220. cli := pb.NewGreeterClient(conn)
  221. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  222. t.Fatalf("call sayhello fail! err: %s", err)
  223. }
  224. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  225. t.Fatalf("call sayhello fail! err: %s", err)
  226. }
  227. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  228. t.Fatalf("call sayhello fail! err: %s", err)
  229. }
  230. assert.Equal(t, 0, testServerMap["server1"].SayHelloCount)
  231. assert.Equal(t, 3, testServerMap["server2"].SayHelloCount)
  232. resetCount()
  233. }
  234. func TestSubsetConn(t *testing.T) {
  235. mockResolver := newMockDiscoveryBuilder()
  236. resolver.Set(mockResolver)
  237. mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{})
  238. mockResolver.registry(testAppID, "server2", "127.0.0.1:18082", map[string]string{})
  239. mockResolver.registry(testAppID, "server3", "127.0.0.1:18083", map[string]string{})
  240. mockResolver.registry(testAppID, "server4", "127.0.0.1:18084", map[string]string{})
  241. mockResolver.registry(testAppID, "server5", "127.0.0.1:18085", map[string]string{})
  242. client := warden.NewClient(nil)
  243. conn, err := client.Dial(context.TODO(), "mockdiscovery://authority/main.test?subset=3")
  244. if err != nil {
  245. t.Fatalf("create client fail!err%s", err)
  246. }
  247. time.Sleep(time.Millisecond * 20)
  248. cli := pb.NewGreeterClient(conn)
  249. for i := 0; i < 6; i++ {
  250. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  251. t.Fatalf("call sayhello fail! err: %s", err)
  252. }
  253. }
  254. assert.Equal(t, 2, testServerMap["server2"].SayHelloCount)
  255. assert.Equal(t, 2, testServerMap["server5"].SayHelloCount)
  256. assert.Equal(t, 2, testServerMap["server4"].SayHelloCount)
  257. resetCount()
  258. mockResolver.cancel("server4")
  259. time.Sleep(time.Millisecond * 20)
  260. for i := 0; i < 6; i++ {
  261. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  262. t.Fatalf("call sayhello fail! err: %s", err)
  263. }
  264. }
  265. assert.Equal(t, 2, testServerMap["server5"].SayHelloCount)
  266. assert.Equal(t, 2, testServerMap["server2"].SayHelloCount)
  267. assert.Equal(t, 2, testServerMap["server3"].SayHelloCount)
  268. resetCount()
  269. mockResolver.registry(testAppID, "server4", "127.0.0.1:18084", map[string]string{})
  270. time.Sleep(time.Millisecond * 20)
  271. for i := 0; i < 6; i++ {
  272. if _, err := cli.SayHello(context.TODO(), &pb.HelloRequest{Age: 1, Name: "hello"}); err != nil {
  273. t.Fatalf("call sayhello fail! err: %s", err)
  274. }
  275. }
  276. assert.Equal(t, 2, testServerMap["server2"].SayHelloCount)
  277. assert.Equal(t, 2, testServerMap["server5"].SayHelloCount)
  278. assert.Equal(t, 2, testServerMap["server4"].SayHelloCount)
  279. }