server_test.go 4.8 KB

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