client_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. },
  98. WithDialOption(grpc.WithContextDialer(dialer())),
  99. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  100. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  101. return invoker(ctx, method, req, reply, cc, opts...)
  102. }),
  103. )
  104. tarConfClient := MustNewClient(
  105. RpcClientConf{
  106. Target: "foo",
  107. App: "foo",
  108. Token: "bar",
  109. Timeout: 1000,
  110. },
  111. WithDialOption(grpc.WithInsecure()),
  112. WithDialOption(grpc.WithContextDialer(dialer())),
  113. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  114. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  115. return invoker(ctx, method, req, reply, cc, opts...)
  116. }),
  117. )
  118. targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()),
  119. WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
  120. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  121. invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  122. return invoker(ctx, method, req, reply, cc, opts...)
  123. }), WithTimeout(1000*time.Millisecond))
  124. assert.Nil(t, err)
  125. clients := []Client{
  126. directClient,
  127. nonBlockClient,
  128. retryClient,
  129. tarConfClient,
  130. targetClient,
  131. }
  132. SetClientSlowThreshold(time.Second)
  133. for _, tt := range tests {
  134. tt := tt
  135. for _, client := range clients {
  136. client := client
  137. t.Run(tt.name, func(t *testing.T) {
  138. t.Parallel()
  139. cli := mock.NewDepositServiceClient(client.Conn())
  140. request := &mock.DepositRequest{Amount: tt.amount}
  141. response, err := cli.Deposit(context.Background(), request)
  142. if response != nil {
  143. assert.True(t, len(response.String()) > 0)
  144. if response.GetOk() != tt.res.GetOk() {
  145. t.Error("response: expected", tt.res.GetOk(), "received", response.GetOk())
  146. }
  147. }
  148. if err != nil {
  149. if e, ok := status.FromError(err); ok {
  150. if e.Code() != tt.errCode {
  151. t.Error("error code: expected", codes.InvalidArgument, "received", e.Code())
  152. }
  153. if e.Message() != tt.errMsg {
  154. t.Error("error message: expected", tt.errMsg, "received", e.Message())
  155. }
  156. }
  157. }
  158. })
  159. }
  160. }
  161. }
  162. func TestNewClientWithError(t *testing.T) {
  163. _, err := NewClient(
  164. RpcClientConf{
  165. App: "foo",
  166. Token: "bar",
  167. Timeout: 1000,
  168. },
  169. WithDialOption(grpc.WithInsecure()),
  170. WithDialOption(grpc.WithContextDialer(dialer())),
  171. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  172. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  173. return invoker(ctx, method, req, reply, cc, opts...)
  174. }),
  175. )
  176. assert.NotNil(t, err)
  177. _, err = NewClient(
  178. RpcClientConf{
  179. Etcd: discov.EtcdConf{
  180. Hosts: []string{"localhost:2379"},
  181. Key: "mock",
  182. },
  183. App: "foo",
  184. Token: "bar",
  185. Timeout: 1,
  186. },
  187. WithDialOption(grpc.WithInsecure()),
  188. WithDialOption(grpc.WithContextDialer(dialer())),
  189. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  190. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  191. return invoker(ctx, method, req, reply, cc, opts...)
  192. }),
  193. )
  194. assert.NotNil(t, err)
  195. }