server_test.go 4.2 KB

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