status_ext_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package status_test
  19. import (
  20. "errors"
  21. "testing"
  22. "github.com/golang/protobuf/proto"
  23. "google.golang.org/grpc/codes"
  24. "google.golang.org/grpc/internal/grpctest"
  25. "google.golang.org/grpc/status"
  26. "google.golang.org/grpc/test/grpc_testing"
  27. )
  28. type s struct {
  29. grpctest.Tester
  30. }
  31. func Test(t *testing.T) {
  32. grpctest.RunSubTests(t, s{})
  33. }
  34. func errWithDetails(t *testing.T, s *status.Status, details ...proto.Message) error {
  35. t.Helper()
  36. res, err := s.WithDetails(details...)
  37. if err != nil {
  38. t.Fatalf("(%v).WithDetails(%v) = %v, %v; want _, <nil>", s, details, res, err)
  39. }
  40. return res.Err()
  41. }
  42. func (s) TestErrorIs(t *testing.T) {
  43. // Test errors.
  44. testErr := status.Error(codes.Internal, "internal server error")
  45. testErrWithDetails := errWithDetails(t, status.New(codes.Internal, "internal server error"), &grpc_testing.Empty{})
  46. // Test cases.
  47. testCases := []struct {
  48. err1, err2 error
  49. want bool
  50. }{
  51. {err1: testErr, err2: nil, want: false},
  52. {err1: testErr, err2: status.Error(codes.Internal, "internal server error"), want: true},
  53. {err1: testErr, err2: status.Error(codes.Internal, "internal error"), want: false},
  54. {err1: testErr, err2: status.Error(codes.Unknown, "internal server error"), want: false},
  55. {err1: testErr, err2: errors.New("non-grpc error"), want: false},
  56. {err1: testErrWithDetails, err2: status.Error(codes.Internal, "internal server error"), want: false},
  57. {err1: testErrWithDetails, err2: errWithDetails(t, status.New(codes.Internal, "internal server error"), &grpc_testing.Empty{}), want: true},
  58. {err1: testErrWithDetails, err2: errWithDetails(t, status.New(codes.Internal, "internal server error"), &grpc_testing.Empty{}, &grpc_testing.Empty{}), want: false},
  59. }
  60. for _, tc := range testCases {
  61. isError, ok := tc.err1.(interface{ Is(target error) bool })
  62. if !ok {
  63. t.Errorf("(%v) does not implement is", tc.err1)
  64. continue
  65. }
  66. is := isError.Is(tc.err2)
  67. if is != tc.want {
  68. t.Errorf("(%v).Is(%v) = %t; want %t", tc.err1, tc.err2, is, tc.want)
  69. }
  70. }
  71. }