github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/grpc/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   * This file may have been modified by CloudWeGo authors. All CloudWeGo
    18   * Modifications are Copyright 2021 CloudWeGo Authors.
    19   */
    20  
    21  package testutils
    22  
    23  import (
    24  	"testing"
    25  
    26  	spb "google.golang.org/genproto/googleapis/rpc/status"
    27  	"google.golang.org/protobuf/types/known/anypb"
    28  
    29  	"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/codes"
    30  	"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/status"
    31  )
    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  
    42  func TestStatusErrEqual(t *testing.T) {
    43  	tests := []struct {
    44  		name      string
    45  		err1      error
    46  		err2      error
    47  		wantEqual bool
    48  	}{
    49  		{"nil errors", nil, nil, true},
    50  		{"equal OK status", status.New(codes.OK, "").Err(), status.New(codes.OK, "").Err(), true},
    51  		{"equal status errors", statusErr, statusErr, true},
    52  		{"different status errors", statusErr, status.New(codes.OK, "").Err(), false},
    53  	}
    54  
    55  	for _, test := range tests {
    56  		if gotEqual := StatusErrEqual(test.err1, test.err2); gotEqual != test.wantEqual {
    57  			t.Errorf("%v: StatusErrEqual(%v, %v) = %v, want %v", test.name, test.err1, test.err2, gotEqual, test.wantEqual)
    58  		}
    59  	}
    60  }