server_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package zrpc
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/discov"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. "github.com/tal-tech/go-zero/core/service"
  9. "github.com/tal-tech/go-zero/core/stat"
  10. "github.com/tal-tech/go-zero/core/stores/redis"
  11. "github.com/tal-tech/go-zero/zrpc/internal"
  12. "github.com/tal-tech/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. SetServerSlowThreshold(time.Second)
  35. srv := MustNewServer(RpcServerConf{
  36. ServiceConf: service.ServiceConf{
  37. Log: logx.LogConf{
  38. ServiceName: "foo",
  39. Mode: "console",
  40. },
  41. },
  42. ListenOn: "localhost:8080",
  43. Etcd: discov.EtcdConf{},
  44. Auth: false,
  45. Redis: redis.RedisKeyConf{},
  46. StrictControl: false,
  47. Timeout: 0,
  48. CpuThreshold: 0,
  49. }, func(server *grpc.Server) {
  50. })
  51. srv.AddOptions(grpc.ConnectionTimeout(time.Hour))
  52. srv.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor)
  53. srv.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
  54. go srv.Start()
  55. srv.Stop()
  56. }
  57. func TestServerError(t *testing.T) {
  58. _, err := NewServer(RpcServerConf{
  59. ServiceConf: service.ServiceConf{
  60. Log: logx.LogConf{
  61. ServiceName: "foo",
  62. Mode: "console",
  63. },
  64. },
  65. ListenOn: "localhost:8080",
  66. Etcd: discov.EtcdConf{
  67. Hosts: []string{"localhost"},
  68. },
  69. Auth: true,
  70. Redis: redis.RedisKeyConf{},
  71. }, func(server *grpc.Server) {
  72. })
  73. assert.NotNil(t, err)
  74. }
  75. func TestServer_HasEtcd(t *testing.T) {
  76. srv := MustNewServer(RpcServerConf{
  77. ServiceConf: service.ServiceConf{
  78. Log: logx.LogConf{
  79. ServiceName: "foo",
  80. Mode: "console",
  81. },
  82. },
  83. ListenOn: "localhost:8080",
  84. Etcd: discov.EtcdConf{
  85. Hosts: []string{"notexist"},
  86. Key: "any",
  87. },
  88. Redis: redis.RedisKeyConf{},
  89. }, func(server *grpc.Server) {
  90. })
  91. srv.AddOptions(grpc.ConnectionTimeout(time.Hour))
  92. srv.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor)
  93. srv.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
  94. go srv.Start()
  95. srv.Stop()
  96. }
  97. type mockedServer struct {
  98. unaryInterceptors []grpc.UnaryServerInterceptor
  99. streamInterceptors []grpc.StreamServerInterceptor
  100. }
  101. func (m *mockedServer) AddOptions(options ...grpc.ServerOption) {
  102. }
  103. func (m *mockedServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  104. m.streamInterceptors = append(m.streamInterceptors, interceptors...)
  105. }
  106. func (m *mockedServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  107. m.unaryInterceptors = append(m.unaryInterceptors, interceptors...)
  108. }
  109. func (m *mockedServer) SetName(s string) {
  110. }
  111. func (m *mockedServer) Start(register internal.RegisterFn) error {
  112. return nil
  113. }