client_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package zrpc
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net"
  7. "testing"
  8. "git.i2edu.net/i2/go-zero/core/logx"
  9. "git.i2edu.net/i2/go-zero/zrpc/internal/mock"
  10. "github.com/stretchr/testify/assert"
  11. "google.golang.org/grpc"
  12. "google.golang.org/grpc/codes"
  13. "google.golang.org/grpc/status"
  14. "google.golang.org/grpc/test/bufconn"
  15. )
  16. func init() {
  17. logx.Disable()
  18. }
  19. func dialer() func(context.Context, string) (net.Conn, error) {
  20. listener := bufconn.Listen(1024 * 1024)
  21. server := grpc.NewServer()
  22. mock.RegisterDepositServiceServer(server, &mock.DepositServer{})
  23. go func() {
  24. if err := server.Serve(listener); err != nil {
  25. log.Fatal(err)
  26. }
  27. }()
  28. return func(context.Context, string) (net.Conn, error) {
  29. return listener.Dial()
  30. }
  31. }
  32. func TestDepositServer_Deposit(t *testing.T) {
  33. tests := []struct {
  34. name string
  35. amount float32
  36. res *mock.DepositResponse
  37. errCode codes.Code
  38. errMsg string
  39. }{
  40. {
  41. "invalid request with negative amount",
  42. -1.11,
  43. nil,
  44. codes.InvalidArgument,
  45. fmt.Sprintf("cannot deposit %v", -1.11),
  46. },
  47. {
  48. "valid request with non negative amount",
  49. 0.00,
  50. &mock.DepositResponse{Ok: true},
  51. codes.OK,
  52. "",
  53. },
  54. }
  55. directClient := MustNewClient(
  56. RpcClientConf{
  57. Endpoints: []string{"foo"},
  58. App: "foo",
  59. Token: "bar",
  60. Timeout: 1000,
  61. },
  62. WithDialOption(grpc.WithInsecure()),
  63. WithDialOption(grpc.WithContextDialer(dialer())),
  64. WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  65. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  66. return invoker(ctx, method, req, reply, cc, opts...)
  67. }),
  68. )
  69. targetClient, err := NewClientWithTarget("foo", WithDialOption(grpc.WithInsecure()),
  70. WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
  71. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  72. invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  73. return invoker(ctx, method, req, reply, cc, opts...)
  74. }))
  75. assert.Nil(t, err)
  76. clients := []Client{
  77. directClient,
  78. targetClient,
  79. }
  80. for _, tt := range tests {
  81. for _, client := range clients {
  82. t.Run(tt.name, func(t *testing.T) {
  83. cli := mock.NewDepositServiceClient(client.Conn())
  84. request := &mock.DepositRequest{Amount: tt.amount}
  85. response, err := cli.Deposit(context.Background(), request)
  86. if response != nil {
  87. assert.True(t, len(response.String()) > 0)
  88. if response.GetOk() != tt.res.GetOk() {
  89. t.Error("response: expected", tt.res.GetOk(), "received", response.GetOk())
  90. }
  91. }
  92. if err != nil {
  93. if e, ok := status.FromError(err); ok {
  94. if e.Code() != tt.errCode {
  95. t.Error("error code: expected", codes.InvalidArgument, "received", e.Code())
  96. }
  97. if e.Message() != tt.errMsg {
  98. t.Error("error message: expected", tt.errMsg, "received", e.Message())
  99. }
  100. }
  101. }
  102. })
  103. }
  104. }
  105. }