github.com/wangyougui/gf/v2@v2.6.5/net/gtcp/gtcp_z_unit_pool_pkg_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gtcp_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/wangyougui/gf/v2/net/gtcp" 14 "github.com/wangyougui/gf/v2/test/gtest" 15 "github.com/wangyougui/gf/v2/util/gconv" 16 ) 17 18 func Test_Pool_Package_Basic(t *testing.T) { 19 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 20 defer conn.Close() 21 for { 22 data, err := conn.RecvPkg() 23 if err != nil { 24 break 25 } 26 conn.SendPkg(data) 27 } 28 }) 29 go s.Run() 30 defer s.Close() 31 time.Sleep(100 * time.Millisecond) 32 // SendPkg 33 gtest.C(t, func(t *gtest.T) { 34 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 35 t.AssertNil(err) 36 defer conn.Close() 37 for i := 0; i < 100; i++ { 38 err := conn.SendPkg([]byte(gconv.String(i))) 39 t.AssertNil(err) 40 } 41 for i := 0; i < 100; i++ { 42 err := conn.SendPkgWithTimeout([]byte(gconv.String(i)), time.Second) 43 t.AssertNil(err) 44 } 45 }) 46 // SendPkg with big data - failure. 47 gtest.C(t, func(t *gtest.T) { 48 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 49 t.AssertNil(err) 50 defer conn.Close() 51 data := make([]byte, 65536) 52 err = conn.SendPkg(data) 53 t.AssertNE(err, nil) 54 }) 55 // SendRecvPkg 56 gtest.C(t, func(t *gtest.T) { 57 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 58 t.AssertNil(err) 59 defer conn.Close() 60 for i := 100; i < 200; i++ { 61 data := []byte(gconv.String(i)) 62 result, err := conn.SendRecvPkg(data) 63 t.AssertNil(err) 64 t.Assert(result, data) 65 } 66 for i := 100; i < 200; i++ { 67 data := []byte(gconv.String(i)) 68 result, err := conn.SendRecvPkgWithTimeout(data, time.Second) 69 t.AssertNil(err) 70 t.Assert(result, data) 71 } 72 }) 73 // SendRecvPkg with big data - failure. 74 gtest.C(t, func(t *gtest.T) { 75 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 76 t.AssertNil(err) 77 defer conn.Close() 78 data := make([]byte, 65536) 79 result, err := conn.SendRecvPkg(data) 80 t.AssertNE(err, nil) 81 t.Assert(result, nil) 82 }) 83 // SendRecvPkg with big data - success. 84 gtest.C(t, func(t *gtest.T) { 85 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 86 t.AssertNil(err) 87 defer conn.Close() 88 data := make([]byte, 65500) 89 data[100] = byte(65) 90 data[65400] = byte(85) 91 result, err := conn.SendRecvPkg(data) 92 t.AssertNil(err) 93 t.Assert(result, data) 94 }) 95 } 96 97 func Test_Pool_Package_Timeout(t *testing.T) { 98 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 99 defer conn.Close() 100 for { 101 data, err := conn.RecvPkg() 102 if err != nil { 103 break 104 } 105 time.Sleep(time.Second) 106 gtest.Assert(conn.SendPkg(data), nil) 107 } 108 }) 109 go s.Run() 110 defer s.Close() 111 time.Sleep(100 * time.Millisecond) 112 gtest.C(t, func(t *gtest.T) { 113 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 114 t.AssertNil(err) 115 defer conn.Close() 116 data := []byte("10000") 117 result, err := conn.SendRecvPkgWithTimeout(data, time.Millisecond*500) 118 t.AssertNE(err, nil) 119 t.Assert(result, nil) 120 }) 121 gtest.C(t, func(t *gtest.T) { 122 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 123 t.AssertNil(err) 124 defer conn.Close() 125 data := []byte("10000") 126 result, err := conn.SendRecvPkgWithTimeout(data, time.Second*2) 127 t.AssertNil(err) 128 t.Assert(result, data) 129 }) 130 } 131 132 func Test_Pool_Package_Option(t *testing.T) { 133 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 134 defer conn.Close() 135 option := gtcp.PkgOption{HeaderSize: 1} 136 for { 137 data, err := conn.RecvPkg(option) 138 if err != nil { 139 break 140 } 141 gtest.Assert(conn.SendPkg(data, option), nil) 142 } 143 }) 144 go s.Run() 145 defer s.Close() 146 time.Sleep(100 * time.Millisecond) 147 // SendRecvPkg with big data - failure. 148 gtest.C(t, func(t *gtest.T) { 149 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 150 t.AssertNil(err) 151 defer conn.Close() 152 data := make([]byte, 0xFF+1) 153 result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 1}) 154 t.AssertNE(err, nil) 155 t.Assert(result, nil) 156 }) 157 // SendRecvPkg with big data - success. 158 gtest.C(t, func(t *gtest.T) { 159 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 160 t.AssertNil(err) 161 defer conn.Close() 162 data := make([]byte, 0xFF) 163 data[100] = byte(65) 164 data[200] = byte(85) 165 result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 1}) 166 t.AssertNil(err) 167 t.Assert(result, data) 168 }) 169 // SendRecvPkgWithTimeout with big data - failure. 170 gtest.C(t, func(t *gtest.T) { 171 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 172 t.AssertNil(err) 173 defer conn.Close() 174 data := make([]byte, 0xFF+1) 175 result, err := conn.SendRecvPkgWithTimeout(data, time.Second, gtcp.PkgOption{HeaderSize: 1}) 176 t.AssertNE(err, nil) 177 t.Assert(result, nil) 178 }) 179 // SendRecvPkgWithTimeout with big data - success. 180 gtest.C(t, func(t *gtest.T) { 181 conn, err := gtcp.NewPoolConn(s.GetListenedAddress()) 182 t.AssertNil(err) 183 defer conn.Close() 184 data := make([]byte, 0xFF) 185 data[100] = byte(65) 186 data[200] = byte(85) 187 result, err := conn.SendRecvPkgWithTimeout(data, time.Second, gtcp.PkgOption{HeaderSize: 1}) 188 t.AssertNil(err) 189 t.Assert(result, data) 190 }) 191 }