proxy_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package zrpc
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/zrpc/internal/mock"
  8. "google.golang.org/grpc"
  9. "google.golang.org/grpc/codes"
  10. "google.golang.org/grpc/status"
  11. )
  12. func TestProxy(t *testing.T) {
  13. tests := []struct {
  14. name string
  15. amount float32
  16. res *mock.DepositResponse
  17. errCode codes.Code
  18. errMsg string
  19. }{
  20. {
  21. "invalid request with negative amount",
  22. -1.11,
  23. nil,
  24. codes.InvalidArgument,
  25. fmt.Sprintf("cannot deposit %v", -1.11),
  26. },
  27. {
  28. "valid request with non negative amount",
  29. 0.00,
  30. &mock.DepositResponse{Ok: true},
  31. codes.OK,
  32. "",
  33. },
  34. }
  35. proxy := NewProxy("foo", WithDialOption(grpc.WithInsecure()),
  36. WithDialOption(grpc.WithContextDialer(dialer())))
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. conn, err := proxy.TakeConn(context.Background())
  40. assert.Nil(t, err)
  41. cli := mock.NewDepositServiceClient(conn)
  42. request := &mock.DepositRequest{Amount: tt.amount}
  43. response, err := cli.Deposit(context.Background(), request)
  44. if response != nil {
  45. assert.True(t, len(response.String()) > 0)
  46. if response.GetOk() != tt.res.GetOk() {
  47. t.Error("response: expected", tt.res.GetOk(), "received", response.GetOk())
  48. }
  49. }
  50. if err != nil {
  51. if e, ok := status.FromError(err); ok {
  52. if e.Code() != tt.errCode {
  53. t.Error("error code: expected", codes.InvalidArgument, "received", e.Code())
  54. }
  55. if e.Message() != tt.errMsg {
  56. t.Error("error message: expected", tt.errMsg, "received", e.Message())
  57. }
  58. }
  59. }
  60. })
  61. }
  62. }