github.com/tencent/goom@v1.0.1/internal/proxy/func_unexport_test.go (about) 1 // Package proxy_test 对 proxy 包的测试 2 package proxy_test 3 4 import ( 5 "errors" 6 "fmt" 7 "net" 8 "os" 9 "strconv" 10 "testing" 11 "time" 12 13 "github.com/tencent/goom/internal/proxy" 14 ) 15 16 // TestNetConnMock 测试 mock 网络连接 17 func TestNetConnMock(t *testing.T) { 18 // 原始函数 19 var connWrite func(c *conn, b []byte) (int, error) 20 21 // 使用 patch 进行切面 22 patch, err := proxy.FuncName("net.(*conn).Write", func(c *conn, b []byte) (int, error) { 23 n, _ := connWrite(c, b) 24 // 修改返回结果 25 return n, errors.New("mocked") 26 }, &connWrite) 27 28 if err != nil { 29 t.Error("mock print err:", err) 30 } 31 32 // 发起网络请求 33 host := "127.0.0.1" 34 port := 80 35 36 conn, err := net.Dial("tcp", host+":"+strconv.Itoa(port)) 37 fmt.Println("Connecting to " + host + ":" + strconv.Itoa(port)) 38 39 if err != nil { 40 fmt.Println("Error connecting:", err) 41 os.Exit(1) 42 } 43 defer conn.Close() 44 45 content := []byte{1, 2, 3} 46 _, err = conn.Write(content) 47 48 // 预期返回: err: mocked 49 t.Log("err:", err) 50 patch.Unpatch() 51 } 52 53 // // nolint 54 type conn struct { 55 // nolint 56 fd *netFD 57 } 58 59 // nolint 60 func (c *conn) Read([]byte) (n int, err error) { return 0, nil } 61 62 // nolint 63 func (c *conn) Write([]byte) (n int, err error) { return 0, nil } 64 65 // nolint 66 func (c *conn) Close() error { return nil } 67 68 // nolint 69 func (c *conn) LocalAddr() net.Addr { return nil } 70 71 // nolint 72 func (c *conn) RemoteAddr() net.Addr { return nil } 73 74 // nolint 75 func (c *conn) SetDeadline(time.Time) error { return nil } 76 77 // nolint 78 func (c *conn) SetReadDeadline(time.Time) error { return nil } 79 80 // nolint 81 func (c *conn) SetWriteDeadline(time.Time) error { return nil } 82 83 // nolint 84 // Network file descriptor. 85 type netFD struct { 86 // nolint 87 pfd FD 88 89 // immutable until Close 90 family int 91 // nolint 92 sotype int 93 isConnected bool // handshake completed or use of association with peer 94 net string 95 laddr net.Addr 96 raddr net.Addr 97 } 98 99 // nolint 100 // FD is a file descriptor. The net and os packages use this type as a 101 // field of a larger type representing a network connection or OS file. 102 type FD struct { 103 // Lock sysfd and serialize access to Read and Write methods. 104 fdmu fdMutex 105 106 // System file descriptor. Immutable until Close. 107 Sysfd int 108 109 // I/O poller. 110 pd pollDesc 111 112 // Writev cache. 113 iovecs *[]int 114 115 // Semaphore signaled when file is closed. 116 csema uint32 117 118 // Non-zero if this file has been set to blocking mode. 119 isBlocking uint32 120 121 // Whether this is a streaming descriptor, as opposed to a 122 // packet-based descriptor like a UDP socket. Immutable. 123 IsStream bool 124 125 // Whether a zero byte read indicates EOF. This is false for a 126 // message based socket connection. 127 ZeroReadIsEOF bool 128 129 // Whether this is a file rather than a network socket. 130 isFile bool 131 } 132 133 // nolint 134 type fdMutex struct { 135 state uint64 136 rsema uint32 137 wsema uint32 138 } 139 140 // nolint 141 type pollDesc struct { 142 runtimeCtx uintptr 143 }