client_test.go 4.6 KB

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