github.com/cloudwego/kitex@v0.9.0/pkg/remote/codec/perrors/protocol_error_test.go (about) 1 /* 2 * Copyright 2021 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 perrors 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/cloudwego/kitex/internal/test" 24 ) 25 26 func TestNewProtocolError(t *testing.T) { 27 // 1. check extends 28 err1 := errors.New("test error") 29 err2 := NewProtocolError(err1) 30 test.Assert(t, errors.Is(err2, err1)) 31 32 err3 := NewProtocolError(err2) 33 test.Assert(t, errors.Is(err3, err1)) 34 35 // 2. check args 36 test.Assert(t, err3.Error() == err1.Error()) 37 test.Assert(t, err3.(interface{ String() string }).String() == err1.Error()) 38 test.Assert(t, err3.(ProtocolError).TypeId() == err2.(ProtocolError).TypeId()) 39 test.Assert(t, !errors.Is(err3, InvalidDataLength)) 40 41 // 3. check nil 42 err4 := NewProtocolError(nil) 43 test.Assert(t, err4 == nil) 44 } 45 46 func TestNewProtocolErrorWithType(t *testing.T) { 47 // check fail 48 msg := "test error" 49 err1 := errors.New(msg) 50 test.Assert(t, !IsProtocolError(err1)) 51 52 typeID := InvalidData 53 err2 := NewProtocolErrorWithType(typeID, msg) 54 test.Assert(t, !errors.Is(err2, InvalidDataLength)) 55 56 // check success 57 test.Assert(t, err2.Error() == msg) 58 test.Assert(t, IsProtocolError(err2)) 59 test.Assert(t, err2.TypeId() == typeID) 60 61 err3 := NewProtocolErrorWithType(typeID, err2.Error()) 62 test.Assert(t, err3.Error() == msg) 63 test.Assert(t, IsProtocolError(err3)) 64 test.Assert(t, err3.TypeId() == typeID) 65 } 66 67 func TestNewProtocolErrorWithMsg(t *testing.T) { 68 // check fail 69 msg := "test error" 70 err1 := errors.New(msg) 71 test.Assert(t, !IsProtocolError(err1)) 72 73 err2 := NewProtocolErrorWithMsg(msg) 74 test.Assert(t, !errors.Is(err2, InvalidDataLength)) 75 76 // check success 77 test.Assert(t, err2.Error() == msg) 78 test.Assert(t, IsProtocolError(err2)) 79 80 err3 := NewProtocolErrorWithMsg(msg) 81 test.Assert(t, err3.Error() == msg) 82 } 83 84 func TestNewProtocolErrorWithErrMsg(t *testing.T) { 85 // check fail 86 msg := "test error" 87 err1 := errors.New(msg) 88 test.Assert(t, !IsProtocolError(err1)) 89 err2 := NewProtocolErrorWithErrMsg(err1, msg) 90 test.Assert(t, !errors.Is(err2, InvalidDataLength)) 91 92 // check success 93 test.Assert(t, err2.Error() == msg) 94 test.Assert(t, IsProtocolError(err2)) 95 err3 := NewProtocolErrorWithErrMsg(err2, msg) 96 test.Assert(t, err3.Error() == msg) 97 98 // check nil 99 err4 := NewProtocolErrorWithErrMsg(nil, msg) 100 test.Assert(t, err4 == nil) 101 test.Assert(t, !IsProtocolError(err4)) 102 } 103 104 func TestIsProtocolError(t *testing.T) { 105 // check fail 106 err1 := errors.New("test error") 107 test.Assert(t, !IsProtocolError(err1)) 108 109 // check success 110 err2 := NewProtocolError(err1) 111 test.Assert(t, IsProtocolError(err2)) 112 err3 := NewProtocolErrorWithType(InvalidData, "test error") 113 test.Assert(t, IsProtocolError(err3)) 114 err4 := NewProtocolErrorWithMsg("test error") 115 test.Assert(t, IsProtocolError(err4)) 116 err5 := NewProtocolError(err4) 117 test.Assert(t, IsProtocolError(err5)) 118 119 // check nil 120 test.Assert(t, !IsProtocolError(nil)) 121 err6 := NewProtocolError(nil) 122 test.Assert(t, !IsProtocolError(err6)) 123 }