github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/mocks_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 trans 18 19 import ( 20 "context" 21 "net" 22 23 "github.com/cloudwego/kitex/pkg/remote" 24 "github.com/cloudwego/kitex/pkg/rpcinfo" 25 "github.com/cloudwego/kitex/pkg/serviceinfo" 26 ) 27 28 func newMockRPCInfo() rpcinfo.RPCInfo { 29 c := rpcinfo.NewEndpointInfo("", "", nil, nil) 30 s := rpcinfo.NewEndpointInfo("", "", nil, nil) 31 ink := rpcinfo.NewInvocation("", "") 32 ri := rpcinfo.NewRPCInfo(c, s, ink, nil, rpcinfo.NewRPCStats()) 33 return ri 34 } 35 36 var _ remote.Codec = &MockCodec{} 37 38 type MockCodec struct { 39 EncodeFunc func(ctx context.Context, msg remote.Message, out remote.ByteBuffer) error 40 DecodeFunc func(ctx context.Context, msg remote.Message, in remote.ByteBuffer) error 41 NameFunc func() string 42 } 43 44 func (m *MockCodec) Encode(ctx context.Context, msg remote.Message, out remote.ByteBuffer) error { 45 if m.EncodeFunc != nil { 46 return m.EncodeFunc(ctx, msg, out) 47 } 48 return nil 49 } 50 51 func (m *MockCodec) Decode(ctx context.Context, msg remote.Message, in remote.ByteBuffer) error { 52 if m.DecodeFunc != nil { 53 return m.DecodeFunc(ctx, msg, in) 54 } 55 return nil 56 } 57 58 func (m *MockCodec) Name() string { 59 if m.NameFunc != nil { 60 return m.NameFunc() 61 } 62 return "" 63 } 64 65 var _ Extension = &MockExtension{} 66 67 type MockExtension struct { 68 SetReadTimeoutFunc func(ctx context.Context, conn net.Conn, cfg rpcinfo.RPCConfig, role remote.RPCRole) 69 NewWriteByteBufferFunc func(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer 70 NewReadByteBufferFunc func(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer 71 ReleaseBufferFunc func(remote.ByteBuffer, error) error 72 IsTimeoutErrFunc func(error) bool 73 IsRemoteClosedErrFunc func(error) bool 74 } 75 76 func (m *MockExtension) SetReadTimeout(ctx context.Context, conn net.Conn, cfg rpcinfo.RPCConfig, role remote.RPCRole) { 77 if m.SetReadTimeoutFunc != nil { 78 m.SetReadTimeoutFunc(ctx, conn, cfg, role) 79 return 80 } 81 } 82 83 func (m *MockExtension) NewWriteByteBuffer(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer { 84 if m.NewWriteByteBufferFunc != nil { 85 return m.NewWriteByteBufferFunc(ctx, conn, msg) 86 } 87 return nil 88 } 89 90 func (m *MockExtension) NewReadByteBuffer(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer { 91 if m.NewReadByteBufferFunc != nil { 92 return m.NewReadByteBufferFunc(ctx, conn, msg) 93 } 94 return nil 95 } 96 97 func (m *MockExtension) ReleaseBuffer(buf remote.ByteBuffer, err error) error { 98 if m.ReleaseBufferFunc != nil { 99 return m.ReleaseBufferFunc(buf, err) 100 } 101 return nil 102 } 103 104 func (m *MockExtension) IsTimeoutErr(err error) bool { 105 if m.IsTimeoutErrFunc != nil { 106 return m.IsTimeoutErrFunc(err) 107 } 108 return false 109 } 110 111 func (m *MockExtension) IsRemoteClosedErr(err error) bool { 112 if m.IsRemoteClosedErrFunc != nil { 113 return m.IsRemoteClosedErrFunc(err) 114 } 115 return false 116 } 117 118 var _ remote.Message = &MockMessage{} 119 120 type MockMessage struct { 121 RPCInfoFunc func() rpcinfo.RPCInfo 122 ServiceInfoFunc func() *serviceinfo.ServiceInfo 123 SetServiceInfoFunc func(svcName, methodName string) (*serviceinfo.ServiceInfo, error) 124 DataFunc func() interface{} 125 NewDataFunc func(method string) (ok bool) 126 MessageTypeFunc func() remote.MessageType 127 SetMessageTypeFunc func(remote.MessageType) 128 RPCRoleFunc func() remote.RPCRole 129 PayloadLenFunc func() int 130 SetPayloadLenFunc func(size int) 131 TransInfoFunc func() remote.TransInfo 132 TagsFunc func() map[string]interface{} 133 ProtocolInfoFunc func() remote.ProtocolInfo 134 SetProtocolInfoFunc func(remote.ProtocolInfo) 135 PayloadCodecFunc func() remote.PayloadCodec 136 SetPayloadCodecFunc func(pc remote.PayloadCodec) 137 RecycleFunc func() 138 } 139 140 func (m *MockMessage) RPCInfo() rpcinfo.RPCInfo { 141 if m.RPCInfoFunc != nil { 142 return m.RPCInfoFunc() 143 } 144 return nil 145 } 146 147 func (m *MockMessage) ServiceInfo() (si *serviceinfo.ServiceInfo) { 148 if m.ServiceInfoFunc != nil { 149 return m.ServiceInfoFunc() 150 } 151 return 152 } 153 154 func (m *MockMessage) SpecifyServiceInfo(svcName, methodName string) (si *serviceinfo.ServiceInfo, err error) { 155 if m.SetServiceInfoFunc != nil { 156 return m.SetServiceInfoFunc(svcName, methodName) 157 } 158 return nil, nil 159 } 160 161 func (m *MockMessage) Data() interface{} { 162 if m.DataFunc != nil { 163 return m.DataFunc() 164 } 165 return nil 166 } 167 168 func (m *MockMessage) NewData(method string) (ok bool) { 169 if m.NewDataFunc != nil { 170 return m.NewDataFunc(method) 171 } 172 return false 173 } 174 175 func (m *MockMessage) MessageType() (mt remote.MessageType) { 176 if m.MessageTypeFunc != nil { 177 return m.MessageTypeFunc() 178 } 179 return 180 } 181 182 func (m *MockMessage) SetMessageType(mt remote.MessageType) { 183 if m.SetMessageTypeFunc != nil { 184 m.SetMessageTypeFunc(mt) 185 } 186 } 187 188 func (m *MockMessage) RPCRole() (r remote.RPCRole) { 189 if m.RPCRoleFunc != nil { 190 return m.RPCRoleFunc() 191 } 192 return 193 } 194 195 func (m *MockMessage) PayloadLen() int { 196 if m.PayloadLenFunc != nil { 197 return m.PayloadLenFunc() 198 } 199 return 0 200 } 201 202 func (m *MockMessage) SetPayloadLen(size int) { 203 if m.SetPayloadLenFunc != nil { 204 m.SetPayloadLenFunc(size) 205 } 206 } 207 208 func (m *MockMessage) TransInfo() remote.TransInfo { 209 if m.TransInfoFunc != nil { 210 return m.TransInfoFunc() 211 } 212 return nil 213 } 214 215 func (m *MockMessage) Tags() map[string]interface{} { 216 if m.TagsFunc != nil { 217 return m.TagsFunc() 218 } 219 return nil 220 } 221 222 func (m *MockMessage) ProtocolInfo() (pi remote.ProtocolInfo) { 223 if m.ProtocolInfoFunc != nil { 224 return m.ProtocolInfoFunc() 225 } 226 return 227 } 228 229 func (m *MockMessage) SetProtocolInfo(pi remote.ProtocolInfo) { 230 if m.SetProtocolInfoFunc != nil { 231 m.SetProtocolInfoFunc(pi) 232 } 233 } 234 235 func (m *MockMessage) PayloadCodec() remote.PayloadCodec { 236 if m.PayloadCodecFunc != nil { 237 return m.PayloadCodecFunc() 238 } 239 return nil 240 } 241 242 func (m *MockMessage) SetPayloadCodec(pc remote.PayloadCodec) { 243 if m.SetPayloadCodecFunc != nil { 244 m.SetPayloadCodecFunc(pc) 245 } 246 } 247 248 func (m *MockMessage) Recycle() { 249 if m.RecycleFunc != nil { 250 m.RecycleFunc() 251 } 252 }