client_test.go 5.3 KB

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