client_test.go 5.1 KB

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