github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/status/status_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package status 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 spb "google.golang.org/genproto/googleapis/rpc/status" 25 26 "github.com/cloudwego/kitex/internal/test" 27 "github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/codes" 28 ) 29 30 func TestStatus(t *testing.T) { 31 // test ok status 32 statusMsg := "test" 33 statusOk := Newf(codes.OK, statusMsg) 34 test.Assert(t, statusOk.Code() == codes.OK) 35 test.Assert(t, statusOk.Message() == statusMsg) 36 test.Assert(t, statusOk.Err() == nil) 37 38 details, err := statusOk.WithDetails() 39 test.Assert(t, err != nil, err) 40 test.Assert(t, details == nil) 41 42 okDetails := statusOk.Details() 43 test.Assert(t, len(okDetails) == 0) 44 45 // test empty status 46 statusEmpty := Status{} 47 test.Assert(t, statusEmpty.Code() == codes.OK) 48 test.Assert(t, statusEmpty.Message() == "") 49 emptyDetail := statusEmpty.Details() 50 test.Assert(t, emptyDetail == nil) 51 52 // test error status 53 notFoundErr := Errorf(codes.NotFound, statusMsg) 54 statusErr, ok := FromError(notFoundErr) 55 test.Assert(t, ok) 56 test.Assert(t, statusErr.Code() == codes.NotFound) 57 58 statusErrWithDetail, err := statusErr.WithDetails(&MockReq{}) 59 test.Assert(t, err == nil, err) 60 notFoundDetails := statusErrWithDetail.Details() 61 test.Assert(t, len(notFoundDetails) == 1) 62 63 statusNilErr, ok := FromError(nil) 64 test.Assert(t, ok) 65 test.Assert(t, statusNilErr == nil) 66 } 67 68 func TestError(t *testing.T) { 69 s := new(spb.Status) 70 s.Code = 1 71 s.Message = "test err" 72 73 er := &Error{s} 74 test.Assert(t, len(er.Error()) > 0) 75 76 status := er.GRPCStatus() 77 test.Assert(t, status.Message() == s.Message) 78 79 is := er.Is(context.Canceled) 80 test.Assert(t, !is) 81 82 is = er.Is(er) 83 test.Assert(t, is) 84 } 85 86 func TestFromContextError(t *testing.T) { 87 errDdl := context.DeadlineExceeded 88 errCanceled := context.Canceled 89 errUnknown := fmt.Errorf("unknown error") 90 91 testErr := fmt.Errorf("status unit test error") 92 93 test.Assert(t, FromContextError(nil) == nil, testErr) 94 test.Assert(t, FromContextError(errDdl).Code() == codes.DeadlineExceeded, testErr) 95 test.Assert(t, FromContextError(errCanceled).Code() == codes.Canceled, testErr) 96 test.Assert(t, FromContextError(errUnknown).Code() == codes.Unknown, testErr) 97 98 statusCanceled := Convert(context.Canceled) 99 test.Assert(t, statusCanceled != nil) 100 101 s := new(spb.Status) 102 s.Code = 1 103 s.Message = "test err" 104 grpcErr := &Error{s} 105 // grpc err 106 codeGrpcErr := Code(grpcErr) 107 test.Assert(t, codeGrpcErr == codes.Canceled) 108 109 // non-grpc err 110 codeCanceled := Code(errUnknown) 111 test.Assert(t, codeCanceled == codes.Unknown) 112 113 // no err 114 codeNil := Code(nil) 115 test.Assert(t, codeNil == codes.OK) 116 }