github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/grpc/mocks_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 grpc 18 19 import ( 20 "net" 21 "time" 22 23 "github.com/cloudwego/netpoll" 24 25 "github.com/cloudwego/kitex/pkg/utils" 26 ) 27 28 var mockAddr0 = "127.0.0.1:33452" 29 30 func newMockNpConn(address string) *mockNetpollConn { 31 mc := &mockNetpollConn{ 32 mockConn: mockConn{ 33 RemoteAddrFunc: func() net.Addr { return utils.NewNetAddr("tcp", address) }, 34 WriteFunc: func(b []byte) (n int, err error) { 35 // mock write preface 36 return len(b), nil 37 }, 38 }, 39 } 40 return mc 41 } 42 43 var _ netpoll.Connection = &mockNetpollConn{} 44 45 // mockNetpollConn implements netpoll.Connection. 46 type mockNetpollConn struct { 47 mockConn 48 } 49 50 func (m *mockNetpollConn) Reader() netpoll.Reader { 51 panic("implement me") 52 } 53 54 func (m *mockNetpollConn) Writer() netpoll.Writer { 55 panic("implement me") 56 } 57 58 func (m *mockNetpollConn) IsActive() bool { 59 panic("implement me") 60 } 61 62 func (m *mockNetpollConn) SetReadTimeout(timeout time.Duration) error { 63 return nil 64 } 65 66 func (m *mockNetpollConn) SetWriteTimeout(timeout time.Duration) error { 67 return nil 68 } 69 70 func (m *mockNetpollConn) SetIdleTimeout(timeout time.Duration) error { 71 return nil 72 } 73 74 func (m *mockNetpollConn) SetOnRequest(on netpoll.OnRequest) error { 75 return nil 76 } 77 78 func (m *mockNetpollConn) AddCloseCallback(callback netpoll.CloseCallback) error { 79 return nil 80 } 81 82 func (m *mockNetpollConn) WriteFrame(hdr, data []byte) (n int, err error) { 83 return 84 } 85 86 func (m *mockNetpollConn) ReadFrame() (hdr, data []byte, err error) { 87 return 88 } 89 90 var _ net.Conn = &mockConn{} 91 92 // mockConn implements the net.Conn interface. 93 type mockConn struct { 94 ReadFunc func(b []byte) (n int, err error) 95 WriteFunc func(b []byte) (n int, err error) 96 CloseFunc func() (e error) 97 LocalAddrFunc func() (r net.Addr) 98 RemoteAddrFunc func() (r net.Addr) 99 SetDeadlineFunc func(t time.Time) (e error) 100 SetReadDeadlineFunc func(t time.Time) (e error) 101 SetWriteDeadlineFunc func(t time.Time) (e error) 102 } 103 104 // Read implements the net.Conn interface. 105 func (m mockConn) Read(b []byte) (n int, err error) { 106 if m.ReadFunc != nil { 107 return m.ReadFunc(b) 108 } 109 return 110 } 111 112 // Write implements the net.Conn interface. 113 func (m mockConn) Write(b []byte) (n int, err error) { 114 if m.WriteFunc != nil { 115 return m.WriteFunc(b) 116 } 117 return 118 } 119 120 // Close implements the net.Conn interface. 121 func (m mockConn) Close() (e error) { 122 if m.CloseFunc != nil { 123 return m.CloseFunc() 124 } 125 return 126 } 127 128 // LocalAddr implements the net.Conn interface. 129 func (m mockConn) LocalAddr() (r net.Addr) { 130 if m.LocalAddrFunc != nil { 131 return m.LocalAddrFunc() 132 } 133 return 134 } 135 136 // RemoteAddr implements the net.Conn interface. 137 func (m mockConn) RemoteAddr() (r net.Addr) { 138 if m.RemoteAddrFunc != nil { 139 return m.RemoteAddrFunc() 140 } 141 return 142 } 143 144 // SetDeadline implements the net.Conn interface. 145 func (m mockConn) SetDeadline(t time.Time) (e error) { 146 if m.SetDeadlineFunc != nil { 147 return m.SetDeadlineFunc(t) 148 } 149 return 150 } 151 152 // SetReadDeadline implements the net.Conn interface. 153 func (m mockConn) SetReadDeadline(t time.Time) (e error) { 154 if m.SetReadDeadlineFunc != nil { 155 return m.SetReadDeadlineFunc(t) 156 } 157 return 158 } 159 160 // SetWriteDeadline implements the net.Conn interface. 161 func (m mockConn) SetWriteDeadline(t time.Time) (e error) { 162 if m.SetWriteDeadlineFunc != nil { 163 return m.SetWriteDeadlineFunc(t) 164 } 165 return 166 }