client_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.WithInsecure()),
  72. WithDialOption(grpc.WithContextDialer(dialer())),
  73. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  74. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  75. return invoker(ctx, method, req, reply, cc, opts...)
  76. }),
  77. )
  78. tarConfClient := MustNewClient(
  79. RpcClientConf{
  80. Target: "foo",
  81. App: "foo",
  82. Token: "bar",
  83. Timeout: 1000,
  84. },
  85. WithDialOption(grpc.WithInsecure()),
  86. WithDialOption(grpc.WithContextDialer(dialer())),
  87. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  88. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  89. return invoker(ctx, method, req, reply, cc, opts...)
  90. }),
  91. )
  92. targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()),
  93. WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
  94. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  95. invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  96. return invoker(ctx, method, req, reply, cc, opts...)
  97. }), WithTimeout(1000*time.Millisecond))
  98. assert.Nil(t, err)
  99. clients := []Client{
  100. directClient,
  101. tarConfClient,
  102. targetClient,
  103. }
  104. for _, tt := range tests {
  105. tt := tt
  106. for _, client := range clients {
  107. client := client
  108. t.Run(tt.name, func(t *testing.T) {
  109. t.Parallel()
  110. cli := mock.NewDepositServiceClient(client.Conn())
  111. request := &mock.DepositRequest{Amount: tt.amount}
  112. response, err := cli.Deposit(context.Background(), request)
  113. if response != nil {
  114. assert.True(t, len(response.String()) > 0)
  115. if response.GetOk() != tt.res.GetOk() {
  116. t.Error("response: expected", tt.res.GetOk(), "received", response.GetOk())
  117. }
  118. }
  119. if err != nil {
  120. if e, ok := status.FromError(err); ok {
  121. if e.Code() != tt.errCode {
  122. t.Error("error code: expected", codes.InvalidArgument, "received", e.Code())
  123. }
  124. if e.Message() != tt.errMsg {
  125. t.Error("error message: expected", tt.errMsg, "received", e.Message())
  126. }
  127. }
  128. }
  129. })
  130. }
  131. }
  132. }
  133. func TestNewClientWithError(t *testing.T) {
  134. _, err := NewClient(
  135. RpcClientConf{
  136. App: "foo",
  137. Token: "bar",
  138. Timeout: 1000,
  139. },
  140. WithDialOption(grpc.WithInsecure()),
  141. WithDialOption(grpc.WithContextDialer(dialer())),
  142. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  143. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  144. return invoker(ctx, method, req, reply, cc, opts...)
  145. }),
  146. )
  147. assert.NotNil(t, err)
  148. _, err = NewClient(
  149. RpcClientConf{
  150. Etcd: discov.EtcdConf{
  151. Hosts: []string{"localhost:2379"},
  152. Key: "mock",
  153. },
  154. App: "foo",
  155. Token: "bar",
  156. Timeout: 1,
  157. },
  158. WithDialOption(grpc.WithInsecure()),
  159. WithDialOption(grpc.WithContextDialer(dialer())),
  160. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  161. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  162. return invoker(ctx, method, req, reply, cc, opts...)
  163. }),
  164. )
  165. assert.NotNil(t, err)
  166. }