server_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package zrpc
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/alicebob/miniredis/v2"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/zeromicro/go-zero/core/discov"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. "github.com/zeromicro/go-zero/core/service"
  10. "github.com/zeromicro/go-zero/core/stat"
  11. "github.com/zeromicro/go-zero/core/stores/redis"
  12. "github.com/zeromicro/go-zero/zrpc/internal"
  13. "github.com/zeromicro/go-zero/zrpc/internal/serverinterceptors"
  14. "google.golang.org/grpc"
  15. )
  16. func TestServer_setupInterceptors(t *testing.T) {
  17. rds, err := miniredis.Run()
  18. assert.NoError(t, err)
  19. defer rds.Close()
  20. server := new(mockedServer)
  21. conf := RpcServerConf{
  22. Auth: true,
  23. Redis: redis.RedisKeyConf{
  24. RedisConf: redis.RedisConf{
  25. Host: rds.Addr(),
  26. Type: redis.NodeType,
  27. },
  28. Key: "foo",
  29. },
  30. CpuThreshold: 10,
  31. Timeout: 100,
  32. Middlewares: ServerMiddlewaresConf{
  33. Trace: true,
  34. Recover: true,
  35. Stat: true,
  36. Prometheus: true,
  37. Breaker: true,
  38. },
  39. }
  40. err = setupInterceptors(server, conf, new(stat.Metrics))
  41. assert.Nil(t, err)
  42. assert.Equal(t, 3, len(server.unaryInterceptors))
  43. assert.Equal(t, 1, len(server.streamInterceptors))
  44. rds.SetError("mock error")
  45. err = setupInterceptors(server, conf, new(stat.Metrics))
  46. assert.Error(t, err)
  47. }
  48. func TestServer(t *testing.T) {
  49. DontLogContentForMethod("foo")
  50. SetServerSlowThreshold(time.Second)
  51. svr := MustNewServer(RpcServerConf{
  52. ServiceConf: service.ServiceConf{
  53. Log: logx.LogConf{
  54. ServiceName: "foo",
  55. Mode: "console",
  56. },
  57. },
  58. ListenOn: "localhost:8080",
  59. Etcd: discov.EtcdConf{},
  60. Auth: false,
  61. Redis: redis.RedisKeyConf{},
  62. StrictControl: false,
  63. Timeout: 0,
  64. CpuThreshold: 0,
  65. Middlewares: ServerMiddlewaresConf{
  66. Trace: true,
  67. Recover: true,
  68. Stat: true,
  69. Prometheus: true,
  70. Breaker: true,
  71. },
  72. }, func(server *grpc.Server) {
  73. })
  74. svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
  75. svr.AddUnaryInterceptors(serverinterceptors.UnaryRecoverInterceptor)
  76. svr.AddStreamInterceptors(serverinterceptors.StreamRecoverInterceptor)
  77. go svr.Start()
  78. svr.Stop()
  79. }
  80. func TestServerError(t *testing.T) {
  81. _, err := NewServer(RpcServerConf{
  82. ServiceConf: service.ServiceConf{
  83. Log: logx.LogConf{
  84. ServiceName: "foo",
  85. Mode: "console",
  86. },
  87. },
  88. ListenOn: "localhost:8080",
  89. Etcd: discov.EtcdConf{
  90. Hosts: []string{"localhost"},
  91. },
  92. Auth: true,
  93. Redis: redis.RedisKeyConf{},
  94. Middlewares: ServerMiddlewaresConf{
  95. Trace: true,
  96. Recover: true,
  97. Stat: true,
  98. Prometheus: true,
  99. Breaker: true,
  100. },
  101. }, func(server *grpc.Server) {
  102. })
  103. assert.NotNil(t, err)
  104. }
  105. func TestServer_HasEtcd(t *testing.T) {
  106. svr := MustNewServer(RpcServerConf{
  107. ServiceConf: service.ServiceConf{
  108. Log: logx.LogConf{
  109. ServiceName: "foo",
  110. Mode: "console",
  111. },
  112. },
  113. ListenOn: "localhost:8080",
  114. Etcd: discov.EtcdConf{
  115. Hosts: []string{"notexist"},
  116. Key: "any",
  117. },
  118. Redis: redis.RedisKeyConf{},
  119. Middlewares: ServerMiddlewaresConf{
  120. Trace: true,
  121. Recover: true,
  122. Stat: true,
  123. Prometheus: true,
  124. Breaker: true,
  125. },
  126. }, func(server *grpc.Server) {
  127. })
  128. svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
  129. svr.AddUnaryInterceptors(serverinterceptors.UnaryRecoverInterceptor)
  130. svr.AddStreamInterceptors(serverinterceptors.StreamRecoverInterceptor)
  131. go svr.Start()
  132. svr.Stop()
  133. }
  134. func TestServer_StartFailed(t *testing.T) {
  135. svr := MustNewServer(RpcServerConf{
  136. ServiceConf: service.ServiceConf{
  137. Log: logx.LogConf{
  138. ServiceName: "foo",
  139. Mode: "console",
  140. },
  141. },
  142. ListenOn: "localhost:aaa",
  143. Middlewares: ServerMiddlewaresConf{
  144. Trace: true,
  145. Recover: true,
  146. Stat: true,
  147. Prometheus: true,
  148. Breaker: true,
  149. },
  150. }, func(server *grpc.Server) {
  151. })
  152. assert.Panics(t, svr.Start)
  153. }
  154. type mockedServer struct {
  155. unaryInterceptors []grpc.UnaryServerInterceptor
  156. streamInterceptors []grpc.StreamServerInterceptor
  157. }
  158. func (m *mockedServer) AddOptions(_ ...grpc.ServerOption) {
  159. }
  160. func (m *mockedServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  161. m.streamInterceptors = append(m.streamInterceptors, interceptors...)
  162. }
  163. func (m *mockedServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  164. m.unaryInterceptors = append(m.unaryInterceptors, interceptors...)
  165. }
  166. func (m *mockedServer) SetName(_ string) {
  167. }
  168. func (m *mockedServer) Start(_ internal.RegisterFn) error {
  169. return nil
  170. }