rpcserver_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package internal
  2. import (
  3. "context"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/wuntsong-org/go-zero-plus/core/proc"
  9. "github.com/wuntsong-org/go-zero-plus/core/stat"
  10. "github.com/wuntsong-org/go-zero-plus/internal/mock"
  11. "google.golang.org/grpc"
  12. )
  13. func TestRpcServer(t *testing.T) {
  14. metrics := stat.NewMetrics("foo")
  15. server := NewRpcServer("localhost:54321", ServerMiddlewaresConf{
  16. Trace: true,
  17. Recover: true,
  18. Stat: true,
  19. Prometheus: true,
  20. Breaker: true,
  21. }, WithMetrics(metrics), WithRpcHealth(true))
  22. server.SetName("mock")
  23. var wg, wgDone sync.WaitGroup
  24. var grpcServer *grpc.Server
  25. var lock sync.Mutex
  26. wg.Add(1)
  27. wgDone.Add(1)
  28. go func() {
  29. err := server.Start(func(server *grpc.Server) {
  30. lock.Lock()
  31. mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
  32. grpcServer = server
  33. lock.Unlock()
  34. wg.Done()
  35. })
  36. assert.Nil(t, err)
  37. wgDone.Done()
  38. }()
  39. wg.Wait()
  40. time.Sleep(100 * time.Millisecond)
  41. lock.Lock()
  42. grpcServer.GracefulStop()
  43. lock.Unlock()
  44. proc.Shutdown()
  45. wgDone.Wait()
  46. }
  47. func TestRpcServer_WithBadAddress(t *testing.T) {
  48. server := NewRpcServer("localhost:111111", ServerMiddlewaresConf{
  49. Trace: true,
  50. Recover: true,
  51. Stat: true,
  52. Prometheus: true,
  53. Breaker: true,
  54. }, WithRpcHealth(true))
  55. server.SetName("mock")
  56. err := server.Start(func(server *grpc.Server) {
  57. mock.RegisterDepositServiceServer(server, new(mock.DepositServer))
  58. })
  59. assert.NotNil(t, err)
  60. proc.WrapUp()
  61. }
  62. func TestRpcServer_buildUnaryInterceptor(t *testing.T) {
  63. tests := []struct {
  64. name string
  65. r *rpcServer
  66. len int
  67. }{
  68. {
  69. name: "empty",
  70. r: &rpcServer{
  71. baseRpcServer: &baseRpcServer{},
  72. },
  73. len: 0,
  74. },
  75. {
  76. name: "custom",
  77. r: &rpcServer{
  78. baseRpcServer: &baseRpcServer{
  79. unaryInterceptors: []grpc.UnaryServerInterceptor{
  80. func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  81. handler grpc.UnaryHandler) (interface{}, error) {
  82. return nil, nil
  83. },
  84. },
  85. },
  86. },
  87. len: 1,
  88. },
  89. {
  90. name: "middleware",
  91. r: &rpcServer{
  92. baseRpcServer: &baseRpcServer{
  93. unaryInterceptors: []grpc.UnaryServerInterceptor{
  94. func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  95. handler grpc.UnaryHandler) (interface{}, error) {
  96. return nil, nil
  97. },
  98. },
  99. },
  100. middlewares: ServerMiddlewaresConf{
  101. Trace: true,
  102. Recover: true,
  103. Stat: true,
  104. Prometheus: true,
  105. Breaker: true,
  106. },
  107. },
  108. len: 6,
  109. },
  110. }
  111. for _, test := range tests {
  112. t.Run(test.name, func(t *testing.T) {
  113. assert.Equal(t, test.len, len(test.r.buildUnaryInterceptors()))
  114. })
  115. }
  116. }
  117. func TestRpcServer_buildStreamInterceptor(t *testing.T) {
  118. tests := []struct {
  119. name string
  120. r *rpcServer
  121. len int
  122. }{
  123. {
  124. name: "empty",
  125. r: &rpcServer{
  126. baseRpcServer: &baseRpcServer{},
  127. },
  128. len: 0,
  129. },
  130. {
  131. name: "custom",
  132. r: &rpcServer{
  133. baseRpcServer: &baseRpcServer{
  134. streamInterceptors: []grpc.StreamServerInterceptor{
  135. func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo,
  136. handler grpc.StreamHandler) error {
  137. return nil
  138. },
  139. },
  140. },
  141. },
  142. len: 1,
  143. },
  144. {
  145. name: "middleware",
  146. r: &rpcServer{
  147. baseRpcServer: &baseRpcServer{
  148. streamInterceptors: []grpc.StreamServerInterceptor{
  149. func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo,
  150. handler grpc.StreamHandler) error {
  151. return nil
  152. },
  153. },
  154. },
  155. middlewares: ServerMiddlewaresConf{
  156. Trace: true,
  157. Recover: true,
  158. Breaker: true,
  159. },
  160. },
  161. len: 4,
  162. },
  163. }
  164. for _, test := range tests {
  165. t.Run(test.name, func(t *testing.T) {
  166. assert.Equal(t, test.len, len(test.r.buildStreamInterceptors()))
  167. })
  168. }
  169. }