server_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }, new(stat.Metrics))
  29. assert.Nil(t, err)
  30. assert.Equal(t, 3, len(server.unaryInterceptors))
  31. assert.Equal(t, 1, len(server.streamInterceptors))
  32. }
  33. func TestServer(t *testing.T) {
  34. DontLogContentForMethod("foo")
  35. SetServerSlowThreshold(time.Second)
  36. svr := MustNewServer(RpcServerConf{
  37. ServiceConf: service.ServiceConf{
  38. Log: logx.LogConf{
  39. ServiceName: "foo",
  40. Mode: "console",
  41. },
  42. },
  43. ListenOn: "localhost:8080",
  44. Etcd: discov.EtcdConf{},
  45. Auth: false,
  46. Redis: redis.RedisKeyConf{},
  47. StrictControl: false,
  48. Timeout: 0,
  49. CpuThreshold: 0,
  50. }, func(server *grpc.Server) {
  51. })
  52. svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
  53. svr.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor)
  54. svr.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
  55. go svr.Start()
  56. svr.Stop()
  57. }
  58. func TestServerError(t *testing.T) {
  59. _, err := NewServer(RpcServerConf{
  60. ServiceConf: service.ServiceConf{
  61. Log: logx.LogConf{
  62. ServiceName: "foo",
  63. Mode: "console",
  64. },
  65. },
  66. ListenOn: "localhost:8080",
  67. Etcd: discov.EtcdConf{
  68. Hosts: []string{"localhost"},
  69. },
  70. Auth: true,
  71. Redis: redis.RedisKeyConf{},
  72. }, func(server *grpc.Server) {
  73. })
  74. assert.NotNil(t, err)
  75. }
  76. func TestServer_HasEtcd(t *testing.T) {
  77. svr := MustNewServer(RpcServerConf{
  78. ServiceConf: service.ServiceConf{
  79. Log: logx.LogConf{
  80. ServiceName: "foo",
  81. Mode: "console",
  82. },
  83. },
  84. ListenOn: "localhost:8080",
  85. Etcd: discov.EtcdConf{
  86. Hosts: []string{"notexist"},
  87. Key: "any",
  88. },
  89. Redis: redis.RedisKeyConf{},
  90. }, func(server *grpc.Server) {
  91. })
  92. svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
  93. svr.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor)
  94. svr.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
  95. go svr.Start()
  96. svr.Stop()
  97. }
  98. func TestServer_StartFailed(t *testing.T) {
  99. svr := MustNewServer(RpcServerConf{
  100. ServiceConf: service.ServiceConf{
  101. Log: logx.LogConf{
  102. ServiceName: "foo",
  103. Mode: "console",
  104. },
  105. },
  106. ListenOn: "localhost:aaa",
  107. }, func(server *grpc.Server) {
  108. })
  109. assert.Panics(t, svr.Start)
  110. }
  111. type mockedServer struct {
  112. unaryInterceptors []grpc.UnaryServerInterceptor
  113. streamInterceptors []grpc.StreamServerInterceptor
  114. }
  115. func (m *mockedServer) AddOptions(_ ...grpc.ServerOption) {
  116. }
  117. func (m *mockedServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  118. m.streamInterceptors = append(m.streamInterceptors, interceptors...)
  119. }
  120. func (m *mockedServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  121. m.unaryInterceptors = append(m.unaryInterceptors, interceptors...)
  122. }
  123. func (m *mockedServer) SetName(_ string) {
  124. }
  125. func (m *mockedServer) Start(_ internal.RegisterFn) error {
  126. return nil
  127. }