github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/gonet/server_handler_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 gonet 18 19 import ( 20 "context" 21 "errors" 22 "net" 23 "testing" 24 25 "github.com/cloudwego/kitex/internal/mocks" 26 "github.com/cloudwego/kitex/internal/test" 27 "github.com/cloudwego/kitex/pkg/remote" 28 ) 29 30 // TestOnActive test server_handler OnActive success 31 func TestOnActive(t *testing.T) { 32 conn := &MockNetConn{ 33 Conn: mocks.Conn{ 34 RemoteAddrFunc: func() (r net.Addr) { 35 return addr 36 }, 37 }, 38 } 39 pl := remote.NewTransPipeline(svrTransHdlr) 40 svrTransHdlr.SetPipeline(pl) 41 if setter, ok := svrTransHdlr.(remote.InvokeHandleFuncSetter); ok { 42 setter.SetInvokeHandleFunc(func(ctx context.Context, req, resp interface{}) (err error) { 43 return nil 44 }) 45 } 46 47 ctx := context.Background() 48 _, err := svrTransHdlr.OnActive(ctx, conn) 49 test.Assert(t, err == nil, err) 50 } 51 52 // TestOnRead test server_handler OnRead success 53 func TestOnRead(t *testing.T) { 54 // 1. prepare mock data 55 // var isWriteBufFlushed bool 56 // var isReaderBufReleased bool 57 var isInvoked bool 58 conn := &MockNetConn{ 59 Conn: mocks.Conn{ 60 RemoteAddrFunc: func() (r net.Addr) { 61 return addr 62 }, 63 }, 64 } 65 66 if setter, ok := svrTransHdlr.(remote.InvokeHandleFuncSetter); ok { 67 setter.SetInvokeHandleFunc(func(ctx context.Context, req, resp interface{}) (err error) { 68 isInvoked = true 69 return nil 70 }) 71 } 72 remote.NewTransPipeline(svrTransHdlr) 73 74 // 2. test 75 ctx := context.Background() 76 ctx, err := svrTransHdlr.OnActive(ctx, conn) 77 test.Assert(t, err == nil, err) 78 79 err = svrTransHdlr.OnRead(ctx, conn) 80 test.Assert(t, err == nil, err) 81 test.Assert(t, isInvoked) 82 } 83 84 // TestInvokeErr test server_handler invoke err 85 func TestInvokeErr(t *testing.T) { 86 // 1. prepare mock data 87 var isInvoked bool 88 conn := &MockNetConn{ 89 Conn: mocks.Conn{ 90 RemoteAddrFunc: func() (r net.Addr) { 91 return addr 92 }, 93 CloseFunc: func() (e error) { 94 return nil 95 }, 96 }, 97 IsActiveFunc: func() (r bool) { 98 return true 99 }, 100 } 101 remote.NewTransPipeline(svrTransHdlr) 102 103 // mock invoke err 104 if setter, ok := svrTransHdlr.(remote.InvokeHandleFuncSetter); ok { 105 setter.SetInvokeHandleFunc(func(ctx context.Context, req, resp interface{}) (err error) { 106 isInvoked = true 107 return errors.New("mock invoke test") 108 }) 109 } 110 111 // 2. test 112 ctx := context.Background() 113 ctx, err := svrTransHdlr.OnActive(ctx, conn) 114 test.Assert(t, err == nil, err) 115 116 err = svrTransHdlr.OnRead(ctx, conn) 117 test.Assert(t, err == nil, err) 118 test.Assert(t, isInvoked) 119 } 120 121 // TestPanicAfterRead test server_handler not panic after read 122 func TestPanicAfterRead(t *testing.T) { 123 // 1. prepare mock data 124 var isInvoked bool 125 var isClosed bool 126 conn := &MockNetConn{ 127 Conn: mocks.Conn{ 128 RemoteAddrFunc: func() (r net.Addr) { 129 return addr 130 }, 131 CloseFunc: func() (e error) { 132 isClosed = true 133 return nil 134 }, 135 }, 136 IsActiveFunc: func() (r bool) { 137 return true 138 }, 139 } 140 // pipeline nil panic 141 svrTransHdlr.SetPipeline(nil) 142 143 // 2. test 144 ctx := context.Background() 145 ctx, err := svrTransHdlr.OnActive(ctx, conn) 146 test.Assert(t, err == nil, err) 147 148 err = svrTransHdlr.OnRead(ctx, conn) 149 test.Assert(t, err != nil, err) 150 test.Assert(t, !isInvoked) 151 test.Assert(t, !isClosed) 152 } 153 154 // TestNoMethodInfo test server_handler without method info success 155 func TestNoMethodInfo(t *testing.T) { 156 // 1. prepare mock data 157 var isClosed bool 158 conn := &MockNetConn{ 159 Conn: mocks.Conn{ 160 RemoteAddrFunc: func() (r net.Addr) { 161 return addr 162 }, 163 CloseFunc: func() (e error) { 164 isClosed = true 165 return nil 166 }, 167 }, 168 IsActiveFunc: func() (r bool) { 169 return true 170 }, 171 } 172 remote.NewTransPipeline(svrTransHdlr) 173 174 svcInfo := svrOpt.TargetSvcInfo 175 delete(svcInfo.Methods, method) 176 177 // 2. test 178 ctx := context.Background() 179 ctx, err := svrTransHdlr.OnActive(ctx, conn) 180 test.Assert(t, err == nil, err) 181 182 err = svrTransHdlr.OnRead(ctx, conn) 183 test.Assert(t, err != nil, err) 184 test.Assert(t, !isClosed) 185 }