client_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package zrpc
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/tal-tech/go-zero/core/discov"
  11. "github.com/tal-tech/go-zero/core/logx"
  12. "github.com/tal-tech/go-zero/zrpc/internal/mock"
  13. "google.golang.org/grpc"
  14. "google.golang.org/grpc/codes"
  15. "google.golang.org/grpc/status"
  16. "google.golang.org/grpc/test/bufconn"
  17. )
  18. func init() {
  19. logx.Disable()
  20. }
  21. func dialer() func(context.Context, string) (net.Conn, error) {
  22. listener := bufconn.Listen(1024 * 1024)
  23. server := grpc.NewServer()
  24. mock.RegisterDepositServiceServer(server, &mock.DepositServer{})
  25. go func() {
  26. if err := server.Serve(listener); err != nil {
  27. log.Fatal(err)
  28. }
  29. }()
  30. return func(context.Context, string) (net.Conn, error) {
  31. return listener.Dial()
  32. }
  33. }
  34. func TestDepositServer_Deposit(t *testing.T) {
  35. tests := []struct {
  36. name string
  37. amount float32
  38. res *mock.DepositResponse
  39. errCode codes.Code
  40. errMsg string
  41. }{
  42. {
  43. "invalid request with negative amount",
  44. -1.11,
  45. nil,
  46. codes.InvalidArgument,
  47. fmt.Sprintf("cannot deposit %v", -1.11),
  48. },
  49. {
  50. "valid request with non negative amount",
  51. 0.00,
  52. &mock.DepositResponse{Ok: true},
  53. codes.OK,
  54. "",
  55. },
  56. {
  57. "valid request with long handling time",
  58. 2000.00,
  59. nil,
  60. codes.DeadlineExceeded,
  61. "context deadline exceeded",
  62. },
  63. }
  64. directClient := MustNewClient(
  65. RpcClientConf{
  66. Endpoints: []string{"foo"},
  67. App: "foo",
  68. Token: "bar",
  69. Timeout: 1000,
  70. },
  71. WithDialOption(grpc.WithContextDialer(dialer())),
  72. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  73. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  74. return invoker(ctx, method, req, reply, cc, opts...)
  75. }),
  76. )
  77. nonBlockClient := MustNewClient(
  78. RpcClientConf{
  79. Endpoints: []string{"foo"},
  80. App: "foo",
  81. Token: "bar",
  82. Timeout: 1000,
  83. NonBlock: true,
  84. },
  85. WithDialOption(grpc.WithContextDialer(dialer())),
  86. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  87. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  88. return invoker(ctx, method, req, reply, cc, opts...)
  89. }),
  90. )
  91. retryClient := MustNewClient(
  92. RpcClientConf{
  93. Endpoints: []string{"foo"},
  94. App: "foo",
  95. Token: "bar",
  96. Timeout: 1000,
  97. Retry: true,
  98. },
  99. WithDialOption(grpc.WithContextDialer(dialer())),
  100. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  101. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  102. return invoker(ctx, method, req, reply, cc, opts...)
  103. }),
  104. )
  105. tarConfClient := MustNewClient(
  106. RpcClientConf{
  107. Target: "foo",
  108. App: "foo",
  109. Token: "bar",
  110. Timeout: 1000,
  111. },
  112. WithDialOption(grpc.WithInsecure()),
  113. WithDialOption(grpc.WithContextDialer(dialer())),
  114. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  115. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  116. return invoker(ctx, method, req, reply, cc, opts...)
  117. }),
  118. )
  119. targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()),
  120. WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
  121. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  122. invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  123. return invoker(ctx, method, req, reply, cc, opts...)
  124. }), WithTimeout(1000*time.Millisecond))
  125. assert.Nil(t, err)
  126. clients := []Client{
  127. directClient,
  128. nonBlockClient,
  129. retryClient,
  130. tarConfClient,
  131. targetClient,
  132. }
  133. SetClientSlowThreshold(time.Second)
  134. for _, tt := range tests {
  135. tt := tt
  136. for _, client := range clients {
  137. client := client
  138. t.Run(tt.name, func(t *testing.T) {
  139. t.Parallel()
  140. cli := mock.NewDepositServiceClient(client.Conn())
  141. request := &mock.DepositRequest{Amount: tt.amount}
  142. response, err := cli.Deposit(context.Background(), request)
  143. if response != nil {
  144. assert.True(t, len(response.String()) > 0)
  145. if response.GetOk() != tt.res.GetOk() {
  146. t.Error("response: expected", tt.res.GetOk(), "received", response.GetOk())
  147. }
  148. }
  149. if err != nil {
  150. if e, ok := status.FromError(err); ok {
  151. if e.Code() != tt.errCode {
  152. t.Error("error code: expected", codes.InvalidArgument, "received", e.Code())
  153. }
  154. if e.Message() != tt.errMsg {
  155. t.Error("error message: expected", tt.errMsg, "received", e.Message())
  156. }
  157. }
  158. }
  159. })
  160. }
  161. }
  162. }
  163. func TestNewClientWithError(t *testing.T) {
  164. _, err := NewClient(
  165. RpcClientConf{
  166. App: "foo",
  167. Token: "bar",
  168. Timeout: 1000,
  169. },
  170. WithDialOption(grpc.WithInsecure()),
  171. WithDialOption(grpc.WithContextDialer(dialer())),
  172. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  173. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  174. return invoker(ctx, method, req, reply, cc, opts...)
  175. }),
  176. )
  177. assert.NotNil(t, err)
  178. _, err = NewClient(
  179. RpcClientConf{
  180. Etcd: discov.EtcdConf{
  181. Hosts: []string{"localhost:2379"},
  182. Key: "mock",
  183. },
  184. App: "foo",
  185. Token: "bar",
  186. Timeout: 1,
  187. },
  188. WithDialOption(grpc.WithInsecure()),
  189. WithDialOption(grpc.WithContextDialer(dialer())),
  190. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  191. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  192. return invoker(ctx, method, req, reply, cc, opts...)
  193. }),
  194. )
  195. assert.NotNil(t, err)
  196. }