github.com/cloudwego/kitex@v0.9.0/internal/mocks/transhandlerserver.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 "net" 22 23 "github.com/cloudwego/kitex/pkg/kerrors" 24 "github.com/cloudwego/kitex/pkg/klog" 25 "github.com/cloudwego/kitex/pkg/remote" 26 "github.com/cloudwego/kitex/pkg/utils" 27 ) 28 29 type mockSvrTransHandlerFactory struct { 30 hdlr *MockSvrTransHandler 31 } 32 33 // NewMockSvrTransHandlerFactory . 34 func NewMockSvrTransHandlerFactory(hdrl *MockSvrTransHandler) remote.ServerTransHandlerFactory { 35 return &mockSvrTransHandlerFactory{hdrl} 36 } 37 38 func (f *mockSvrTransHandlerFactory) NewTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) { 39 f.hdlr.Opt = opt 40 return f.hdlr, nil 41 } 42 43 // MockSvrTransHandler . 44 type MockSvrTransHandler struct { 45 Opt *remote.ServerOption 46 transPipe *remote.TransPipeline 47 48 OnReadFunc func(ctx context.Context, conn net.Conn) error 49 50 WriteFunc func(ctx context.Context, conn net.Conn, send remote.Message) (nctx context.Context, err error) 51 52 ReadFunc func(ctx context.Context, conn net.Conn, msg remote.Message) (nctx context.Context, err error) 53 } 54 55 // OnRead implements the remote.TransHandler interface. 56 func (t *MockSvrTransHandler) OnRead(ctx context.Context, conn net.Conn) (err error) { 57 if t.OnReadFunc != nil { 58 return t.OnReadFunc(ctx, conn) 59 } 60 return 61 } 62 63 // Write implements the remote.TransHandler interface. 64 func (t *MockSvrTransHandler) Write(ctx context.Context, conn net.Conn, send remote.Message) (nctx context.Context, err error) { 65 if t.WriteFunc != nil { 66 return t.WriteFunc(ctx, conn, send) 67 } 68 return 69 } 70 71 // Read implements the remote.TransHandler interface. 72 func (t *MockSvrTransHandler) Read(ctx context.Context, conn net.Conn, msg remote.Message) (nctx context.Context, err error) { 73 if t.ReadFunc != nil { 74 return t.ReadFunc(ctx, conn, msg) 75 } 76 return 77 } 78 79 // OnMessage implements the remote.TransHandler interface. 80 func (t *MockSvrTransHandler) OnMessage(ctx context.Context, args, result remote.Message) (context.Context, error) { 81 // do nothing 82 return ctx, nil 83 } 84 85 // OnActive implements the remote.TransHandler interface. 86 func (t *MockSvrTransHandler) OnActive(ctx context.Context, conn net.Conn) (context.Context, error) { 87 // ineffective now and do nothing 88 return ctx, nil 89 } 90 91 // OnInactive implements the remote.TransHandler interface. 92 func (t *MockSvrTransHandler) OnInactive(ctx context.Context, conn net.Conn) { 93 // ineffective now and do nothing 94 } 95 96 // OnError implements the remote.TransHandler interface. 97 func (t *MockSvrTransHandler) OnError(ctx context.Context, err error, conn net.Conn) { 98 if pe, ok := err.(*kerrors.DetailedError); ok { 99 klog.CtxErrorf(ctx, "KITEX: send request error, remote=%s, error=%s\nstack=%s", conn.RemoteAddr(), err.Error(), pe.Stack()) 100 } else { 101 klog.CtxErrorf(ctx, "KITEX: send request error, remote=%s, error=%s", conn.RemoteAddr(), err.Error()) 102 } 103 } 104 105 // SetPipeline implements the remote.TransHandler interface. 106 func (t *MockSvrTransHandler) SetPipeline(p *remote.TransPipeline) { 107 t.transPipe = p 108 } 109 110 // MockTransServerFactory . 111 type MockTransServerFactory struct { 112 transSvr *MockTransServer 113 } 114 115 // NewMockTransServerFactory . 116 func NewMockTransServerFactory(transSvr *MockTransServer) remote.TransServerFactory { 117 return &MockTransServerFactory{transSvr} 118 } 119 120 // NewTransServer . 121 func (t *MockTransServerFactory) NewTransServer(opt *remote.ServerOption, transHdlr remote.ServerTransHandler) remote.TransServer { 122 t.transSvr.opt = opt 123 t.transSvr.transHdlr = transHdlr 124 return t.transSvr 125 } 126 127 // MockTransServer . 128 type MockTransServer struct { 129 opt *remote.ServerOption 130 transHdlr remote.ServerTransHandler 131 132 CreateListenerFunc func(net.Addr) (net.Listener, error) 133 BootstrapServerFunc func(net.Listener) (err error) 134 ShutdownFunc func() (err error) 135 ConnCountFunc func() utils.AtomicInt 136 } 137 138 // CreateListener . 139 func (t *MockTransServer) CreateListener(addr net.Addr) (ln net.Listener, err error) { 140 if t.CreateListenerFunc != nil { 141 return t.CreateListenerFunc(addr) 142 } 143 return 144 } 145 146 // BootstrapServer . 147 func (t *MockTransServer) BootstrapServer(ln net.Listener) (err error) { 148 if t.BootstrapServerFunc != nil { 149 return t.BootstrapServerFunc(ln) 150 } 151 return 152 } 153 154 // Shutdown . 155 func (t *MockTransServer) Shutdown() (err error) { 156 if t.ShutdownFunc != nil { 157 return t.ShutdownFunc() 158 } 159 return 160 } 161 162 // ConnCount . 163 func (t *MockTransServer) ConnCount() (r utils.AtomicInt) { 164 if t.ConnCountFunc != nil { 165 return t.ConnCountFunc() 166 } 167 return 168 }