server_test.go 3.1 KB

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