server_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/netx"
  9. "github.com/tal-tech/go-zero/core/service"
  10. "github.com/tal-tech/go-zero/core/stat"
  11. "github.com/tal-tech/go-zero/core/stores/redis"
  12. "github.com/tal-tech/go-zero/zrpc/internal"
  13. "github.com/tal-tech/go-zero/zrpc/internal/serverinterceptors"
  14. "google.golang.org/grpc"
  15. )
  16. func TestFigureOutListenOn(t *testing.T) {
  17. tests := []struct {
  18. input string
  19. expect string
  20. }{
  21. {
  22. input: "192.168.0.5:1234",
  23. expect: "192.168.0.5:1234",
  24. },
  25. {
  26. input: "0.0.0.0:8080",
  27. expect: netx.InternalIp() + ":8080",
  28. },
  29. {
  30. input: ":8080",
  31. expect: netx.InternalIp() + ":8080",
  32. },
  33. }
  34. for _, test := range tests {
  35. val := figureOutListenOn(test.input)
  36. assert.Equal(t, test.expect, val)
  37. }
  38. }
  39. func TestServer_setupInterceptors(t *testing.T) {
  40. server := new(mockedServer)
  41. err := setupInterceptors(server, RpcServerConf{
  42. Auth: true,
  43. Redis: redis.RedisKeyConf{
  44. RedisConf: redis.RedisConf{
  45. Host: "any",
  46. Type: redis.NodeType,
  47. },
  48. Key: "foo",
  49. },
  50. CpuThreshold: 10,
  51. Timeout: 100,
  52. }, new(stat.Metrics))
  53. assert.Nil(t, err)
  54. assert.Equal(t, 3, len(server.unaryInterceptors))
  55. assert.Equal(t, 1, len(server.streamInterceptors))
  56. }
  57. func TestServer(t *testing.T) {
  58. srv := MustNewServer(RpcServerConf{
  59. ServiceConf: service.ServiceConf{
  60. Log: logx.LogConf{
  61. ServiceName: "foo",
  62. Mode: "console",
  63. },
  64. },
  65. ListenOn: ":8080",
  66. Etcd: discov.EtcdConf{},
  67. Auth: false,
  68. Redis: redis.RedisKeyConf{},
  69. StrictControl: false,
  70. Timeout: 0,
  71. CpuThreshold: 0,
  72. }, func(server *grpc.Server) {
  73. })
  74. srv.AddOptions(grpc.ConnectionTimeout(time.Hour))
  75. srv.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor())
  76. srv.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
  77. go srv.Start()
  78. srv.Stop()
  79. }
  80. func TestServerError(t *testing.T) {
  81. _, err := NewServer(RpcServerConf{
  82. ServiceConf: service.ServiceConf{
  83. Log: logx.LogConf{
  84. ServiceName: "foo",
  85. Mode: "console",
  86. },
  87. },
  88. ListenOn: ":8080",
  89. Etcd: discov.EtcdConf{
  90. Hosts: []string{"localhost"},
  91. },
  92. Auth: true,
  93. Redis: redis.RedisKeyConf{},
  94. }, func(server *grpc.Server) {
  95. })
  96. assert.NotNil(t, err)
  97. }
  98. func TestServer_HasEtcd(t *testing.T) {
  99. srv := MustNewServer(RpcServerConf{
  100. ServiceConf: service.ServiceConf{
  101. Log: logx.LogConf{
  102. ServiceName: "foo",
  103. Mode: "console",
  104. },
  105. },
  106. ListenOn: ":8080",
  107. Etcd: discov.EtcdConf{
  108. Hosts: []string{"notexist"},
  109. Key: "any",
  110. },
  111. Redis: redis.RedisKeyConf{},
  112. }, func(server *grpc.Server) {
  113. })
  114. srv.AddOptions(grpc.ConnectionTimeout(time.Hour))
  115. srv.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor())
  116. srv.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
  117. go srv.Start()
  118. srv.Stop()
  119. }
  120. type mockedServer struct {
  121. unaryInterceptors []grpc.UnaryServerInterceptor
  122. streamInterceptors []grpc.StreamServerInterceptor
  123. }
  124. func (m *mockedServer) AddOptions(options ...grpc.ServerOption) {
  125. }
  126. func (m *mockedServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  127. m.streamInterceptors = append(m.streamInterceptors, interceptors...)
  128. }
  129. func (m *mockedServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  130. m.unaryInterceptors = append(m.unaryInterceptors, interceptors...)
  131. }
  132. func (m *mockedServer) SetName(s string) {
  133. }
  134. func (m *mockedServer) Start(register internal.RegisterFn) error {
  135. return nil
  136. }