github.com/cloudwego/kitex@v0.9.0/internal/mocks/serviceinfo.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 mocks 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 24 "github.com/apache/thrift/lib/go/thrift" 25 26 "github.com/cloudwego/kitex/pkg/serviceinfo" 27 ) 28 29 // method name of mock 30 const ( 31 MockServiceName = "MockService" 32 MockService2Name = "MockService2" 33 MockService3Name = "MockService3" 34 MockMethod string = "mock" 35 Mock2Method string = "mock2" 36 MockExceptionMethod string = "mockException" 37 MockErrorMethod string = "mockError" 38 MockOnewayMethod string = "mockOneway" 39 ) 40 41 // ServiceInfo return mock serviceInfo 42 func ServiceInfo() *serviceinfo.ServiceInfo { 43 return myServiceServiceInfo 44 } 45 46 var myServiceServiceInfo = newServiceInfo() 47 48 func newServiceInfo() *serviceinfo.ServiceInfo { 49 methods := map[string]serviceinfo.MethodInfo{ 50 "mock": serviceinfo.NewMethodInfo(mockHandler, NewMockArgs, NewMockResult, false), 51 "mockException": serviceinfo.NewMethodInfo(mockExceptionHandler, NewMockArgs, newMockExceptionResult, false), 52 "mockError": serviceinfo.NewMethodInfo(mockErrorHandler, NewMockArgs, NewMockResult, false), 53 "mockOneway": serviceinfo.NewMethodInfo(mockOnewayHandler, NewMockArgs, nil, true), 54 "mockStreaming": serviceinfo.NewMethodInfo( 55 nil, nil, nil, false, 56 serviceinfo.WithStreamingMode(serviceinfo.StreamingBidirectional), 57 ), 58 } 59 60 svcInfo := &serviceinfo.ServiceInfo{ 61 ServiceName: MockServiceName, 62 Methods: methods, 63 Extra: map[string]interface{}{ 64 "PackageName": "mock", 65 }, 66 } 67 return svcInfo 68 } 69 70 // Service2Info return mock serviceInfo 71 func Service2Info() *serviceinfo.ServiceInfo { 72 return myServiceService2Info 73 } 74 75 var myServiceService2Info = newService2Info() 76 77 func newService2Info() *serviceinfo.ServiceInfo { 78 methods := map[string]serviceinfo.MethodInfo{ 79 "mock2": serviceinfo.NewMethodInfo(mock2Handler, NewMockArgs, NewMockResult, false), 80 } 81 82 svcInfo := &serviceinfo.ServiceInfo{ 83 ServiceName: MockService2Name, 84 Methods: methods, 85 Extra: map[string]interface{}{ 86 "PackageName": "mock2", 87 }, 88 } 89 return svcInfo 90 } 91 92 // Service3Info return mock serviceInfo 93 func Service3Info() *serviceinfo.ServiceInfo { 94 return myServiceService3Info 95 } 96 97 var myServiceService3Info = newService3Info() 98 99 func newService3Info() *serviceinfo.ServiceInfo { 100 methods := map[string]serviceinfo.MethodInfo{ 101 "mock": serviceinfo.NewMethodInfo(mockHandler, NewMockArgs, NewMockResult, false), 102 } 103 104 svcInfo := &serviceinfo.ServiceInfo{ 105 ServiceName: MockService3Name, 106 Methods: methods, 107 Extra: map[string]interface{}{ 108 "PackageName": "mock", 109 }, 110 } 111 return svcInfo 112 } 113 114 func mockHandler(ctx context.Context, handler, args, result interface{}) error { 115 a := args.(*myServiceMockArgs) 116 r := result.(*myServiceMockResult) 117 reply, err := handler.(MyService).Mock(ctx, a.Req) 118 if err != nil { 119 return err 120 } 121 r.Success = reply 122 return nil 123 } 124 125 func mock2Handler(ctx context.Context, handler, args, result interface{}) error { 126 a := args.(*myServiceMockArgs) 127 r := result.(*myServiceMockResult) 128 reply, err := handler.(MyService).Mock(ctx, a.Req) 129 if err != nil { 130 return err 131 } 132 r.Success = reply 133 return nil 134 } 135 136 func NewMockArgs() interface{} { 137 return &myServiceMockArgs{} 138 } 139 140 func NewMockResult() interface{} { 141 return &myServiceMockResult{} 142 } 143 144 func mockExceptionHandler(ctx context.Context, handler, args, result interface{}) error { 145 a := args.(*myServiceMockArgs) 146 r := result.(*myServiceMockExceptionResult) 147 reply, err := handler.(MyService).MockException(ctx, a.Req) 148 if err != nil { 149 switch v := err.(type) { 150 case *MyException: 151 r.MyException = v 152 default: 153 return err 154 } 155 } else { 156 r.Success = reply 157 } 158 return nil 159 } 160 161 func mockErrorHandler(ctx context.Context, handler, args, result interface{}) error { 162 a := args.(*myServiceMockArgs) 163 r := result.(*myServiceMockResult) 164 reply, err := handler.(MyService).MockError(ctx, a.Req) 165 if err != nil { 166 return err 167 } 168 r.Success = reply 169 return nil 170 } 171 172 func newMockExceptionResult() interface{} { 173 return &myServiceMockExceptionResult{} 174 } 175 176 func mockOnewayHandler(ctx context.Context, handler, args, result interface{}) error { 177 a := args.(*myServiceMockArgs) 178 err := handler.(MyService).MockOneway(ctx, a.Req) 179 if err != nil { 180 return err 181 } 182 return nil 183 } 184 185 // MyService . 186 type MyService interface { 187 Mock(ctx context.Context, req *MyRequest) (r *MyResponse, err error) 188 MockException(ctx context.Context, req *MyRequest) (r *MyResponse, err error) 189 MockError(ctx context.Context, req *MyRequest) (r *MyResponse, err error) 190 MockOneway(ctx context.Context, req *MyRequest) (err error) 191 Mock2(ctx context.Context, req *MyRequest) (r *MyResponse, err error) 192 } 193 194 type myServiceMockArgs struct { 195 Req *MyRequest `thrift:"req,1" json:"req"` 196 } 197 198 func (p *myServiceMockArgs) Read(iprot thrift.TProtocol) error { 199 return nil 200 } 201 202 func (p *myServiceMockArgs) Write(oprot thrift.TProtocol) error { 203 return nil 204 } 205 206 // MyRequest . 207 type MyRequest struct { 208 Name string `thrift:"Name,1" json:"Name"` 209 } 210 211 type myServiceMockResult struct { 212 Success *MyResponse `thrift:"success,0" json:"success,omitempty"` 213 } 214 215 func (p *myServiceMockResult) Read(iprot thrift.TProtocol) error { 216 return nil 217 } 218 219 func (p *myServiceMockResult) Write(oprot thrift.TProtocol) error { 220 return nil 221 } 222 223 // MyResponse . 224 type MyResponse struct { 225 Name string `thrift:"Name,1" json:"Name"` 226 } 227 228 type myServiceMockExceptionResult struct { 229 Success *MyResponse `thrift:"success,0" json:"success,omitempty"` 230 MyException *MyException `thrift:"stException,1" json:"stException,omitempty"` 231 } 232 233 func (p *myServiceMockExceptionResult) Read(iprot thrift.TProtocol) error { 234 return nil 235 } 236 237 func (p *myServiceMockExceptionResult) Write(oprot thrift.TProtocol) error { 238 return nil 239 } 240 241 // MyException . 242 type MyException struct { 243 Message string `thrift:"message,1" json:"message"` 244 } 245 246 func (p *MyException) Error() string { 247 return fmt.Sprintf("MyException(%+v)", *p) 248 } 249 250 // MyServiceHandler . 251 func MyServiceHandler() interface{} { 252 return &myServiceHandler{} 253 } 254 255 // MockFuncHandler . 256 func MockFuncHandler(mf func(ctx context.Context, req *MyRequest) (r *MyResponse, err error)) interface{} { 257 return &myServiceHandler{mf} 258 } 259 260 type myServiceHandler struct { 261 mockFunc func(ctx context.Context, req *MyRequest) (r *MyResponse, err error) 262 } 263 264 func (h *myServiceHandler) Mock(ctx context.Context, req *MyRequest) (r *MyResponse, err error) { 265 if h.mockFunc != nil { 266 return h.mockFunc(ctx, req) 267 } 268 return &MyResponse{Name: MockMethod}, nil 269 } 270 271 func (h *myServiceHandler) Mock2(ctx context.Context, req *MyRequest) (r *MyResponse, err error) { 272 if h.mockFunc != nil { 273 return h.mockFunc(ctx, req) 274 } 275 return &MyResponse{Name: Mock2Method}, nil 276 } 277 278 func (h *myServiceHandler) MockException(ctx context.Context, req *MyRequest) (r *MyResponse, err error) { 279 return &MyResponse{Name: MockExceptionMethod}, nil 280 } 281 282 func (h *myServiceHandler) MockError(ctx context.Context, req *MyRequest) (r *MyResponse, err error) { 283 return nil, errors.New(MockErrorMethod) 284 } 285 286 func (h *myServiceHandler) MockOneway(ctx context.Context, req *MyRequest) (err error) { 287 return nil 288 }