github.com/songzhibin97/gkit@v1.2.13/net/tcp/conn_test.go (about) 1 package tcp 2 3 import ( 4 "net" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestConn_Send(t *testing.T) { 11 var conn net.Conn 12 ok := make(chan struct{}, 1) 13 // run server 14 go func() { 15 l, err := net.Listen("tcp", "127.0.0.1:8999") 16 assert.NoError(t, err) 17 defer l.Close() 18 ok <- struct{}{} 19 conn, err = l.Accept() 20 assert.NoError(t, err) 21 }() 22 <-ok 23 // client 24 mockConn, err := NewConn("127.0.0.1:8999", nil) 25 assert.NoError(t, err) 26 defer mockConn.Close() 27 body := []byte("hello world") 28 err = mockConn.Send(body, nil) 29 assert.NoError(t, err) 30 31 readBody := make([]byte, len(body)) 32 for conn == nil { 33 } 34 n, err := conn.Read(readBody) 35 assert.NoError(t, err) 36 assert.Equal(t, n, len(body)) 37 assert.Equal(t, body, readBody) 38 39 t.Log("send:", string(body), "->", "read:", string(readBody)) 40 } 41 42 func TestConn_Recv(t *testing.T) { 43 var conn net.Conn 44 ok := make(chan struct{}, 1) 45 DefaultReadBuffer = 16 46 // run server 47 go func() { 48 l, err := net.Listen("tcp", "127.0.0.1:8999") 49 assert.NoError(t, err) 50 defer l.Close() 51 ok <- struct{}{} 52 conn, err = l.Accept() 53 assert.NoError(t, err) 54 }() 55 <-ok 56 // client 57 mockConn, err := NewConn("127.0.0.1:8999", nil) 58 assert.NoError(t, err) 59 defer mockConn.Close() 60 61 body := []byte("hello world") 62 for conn == nil { 63 } 64 { 65 // 正常一次收发 66 n, err := conn.Write(body) 67 assert.NoError(t, err) 68 assert.Equal(t, n, len(body)) 69 70 readBody, err := mockConn.Recv(0, nil) 71 assert.NoError(t, err) 72 assert.Equal(t, body, readBody) 73 74 t.Log("send:", string(body), "->", "read:", string(readBody)) 75 } 76 77 { 78 // 指定长度 79 n, err := conn.Write(body) 80 assert.NoError(t, err) 81 assert.Equal(t, n, len(body)) 82 83 readBody, err := mockConn.Recv(8, nil) 84 assert.NoError(t, err) 85 assert.Equal(t, body[:8], readBody) 86 87 t.Log("send:", string(body[:8]), "->", "read:", string(readBody)) 88 _, _ = mockConn.Recv(0, nil) 89 } 90 91 { 92 body2 := []byte("012345678910") 93 // 指定长度 94 95 n, err := conn.Write(body) 96 assert.NoError(t, err) 97 assert.Equal(t, n, len(body)) 98 99 n, err = conn.Write(body2) 100 assert.NoError(t, err) 101 assert.Equal(t, n, len(body2)) 102 103 readBody, err := mockConn.Recv(20, nil) 104 assert.NoError(t, err) 105 106 assert.Equal(t, []byte("hello world012345678"), readBody) 107 108 t.Log("send:", "hello world012345678", "->", "read:", string(readBody)) 109 } 110 111 { 112 readBody, err := mockConn.Recv(-1, nil) 113 assert.NoError(t, err) 114 assert.Equal(t, []byte("910"), readBody) 115 t.Log("send:", "910", "->", "read:", string(readBody)) 116 117 } 118 } 119 120 func TestConn_RecvLine(t *testing.T) { 121 var conn net.Conn 122 ok := make(chan struct{}, 1) 123 DefaultReadBuffer = 16 124 // run server 125 go func() { 126 l, err := net.Listen("tcp", "127.0.0.1:8999") 127 assert.NoError(t, err) 128 defer l.Close() 129 ok <- struct{}{} 130 conn, err = l.Accept() 131 assert.NoError(t, err) 132 }() 133 <-ok 134 135 // client 136 mockConn, err := NewConn("127.0.0.1:8999", nil) 137 assert.NoError(t, err) 138 defer mockConn.Close() 139 140 body := `测试readline 141 测试Gkit 142 测试Debug 143 ` 144 for conn == nil { 145 } 146 n, err := conn.Write([]byte(body)) 147 assert.NoError(t, err) 148 assert.Equal(t, n, len(body)) 149 { 150 readBody, err := mockConn.RecvLine(nil) 151 assert.NoError(t, err) 152 assert.Equal(t, []byte("测试readline"), readBody) 153 t.Log("send:", "测试readline", "->", "read:", string(readBody)) 154 } 155 { 156 readBody, err := mockConn.RecvLine(nil) 157 assert.NoError(t, err) 158 assert.Equal(t, []byte("测试Gkit"), readBody) 159 t.Log("send:", "测试Gkit", "->", "read:", string(readBody)) 160 } 161 { 162 readBody, err := mockConn.RecvLine(nil) 163 assert.NoError(t, err) 164 assert.Equal(t, []byte("测试Debug"), readBody) 165 t.Log("send:", "测试Debug", "->", "read:", string(readBody)) 166 } 167 }