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