status_equal_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 testutils
  19. import (
  20. "testing"
  21. anypb "github.com/golang/protobuf/ptypes/any"
  22. spb "google.golang.org/genproto/googleapis/rpc/status"
  23. "google.golang.org/grpc/codes"
  24. "google.golang.org/grpc/internal/grpctest"
  25. "google.golang.org/grpc/status"
  26. )
  27. type s struct {
  28. grpctest.Tester
  29. }
  30. func Test(t *testing.T) {
  31. grpctest.RunSubTests(t, s{})
  32. }
  33. var statusErr = status.ErrorProto(&spb.Status{
  34. Code: int32(codes.DataLoss),
  35. Message: "error for testing",
  36. Details: []*anypb.Any{{
  37. TypeUrl: "url",
  38. Value: []byte{6, 0, 0, 6, 1, 3},
  39. }},
  40. })
  41. func (s) TestStatusErrEqual(t *testing.T) {
  42. tests := []struct {
  43. name string
  44. err1 error
  45. err2 error
  46. wantEqual bool
  47. }{
  48. {"nil errors", nil, nil, true},
  49. {"equal OK status", status.New(codes.OK, "").Err(), status.New(codes.OK, "").Err(), true},
  50. {"equal status errors", statusErr, statusErr, true},
  51. {"different status errors", statusErr, status.New(codes.OK, "").Err(), false},
  52. }
  53. for _, test := range tests {
  54. if gotEqual := StatusErrEqual(test.err1, test.err2); gotEqual != test.wantEqual {
  55. t.Errorf("%v: StatusErrEqual(%v, %v) = %v, want %v", test.name, test.err1, test.err2, gotEqual, test.wantEqual)
  56. }
  57. }
  58. }