2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-12-11 14:50:25 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gtcp_test
|
|
|
|
|
|
|
|
import (
|
2022-01-22 15:33:31 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/net/gtcp"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-12-11 14:50:25 +08:00
|
|
|
)
|
|
|
|
|
2019-12-11 21:22:41 +08:00
|
|
|
func Test_Pool_Package_Basic(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.RecvPkg()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.SendPkg(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// SendPkg
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
err := conn.SendPkg([]byte(gconv.String(i)))
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
err := conn.SendPkgWithTimeout([]byte(gconv.String(i)), time.Second)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// SendPkg with big data - failure.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := make([]byte, 65536)
|
|
|
|
err = conn.SendPkg(data)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-12-11 14:50:25 +08:00
|
|
|
})
|
|
|
|
// SendRecvPkg
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for i := 100; i < 200; i++ {
|
|
|
|
data := []byte(gconv.String(i))
|
|
|
|
result, err := conn.SendRecvPkg(data)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result, data)
|
2019-12-11 14:50:25 +08:00
|
|
|
}
|
|
|
|
for i := 100; i < 200; i++ {
|
|
|
|
data := []byte(gconv.String(i))
|
|
|
|
result, err := conn.SendRecvPkgWithTimeout(data, time.Second)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result, data)
|
2019-12-11 14:50:25 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// SendRecvPkg with big data - failure.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := make([]byte, 65536)
|
|
|
|
result, err := conn.SendRecvPkg(data)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(result, nil)
|
2019-12-11 14:50:25 +08:00
|
|
|
})
|
|
|
|
// SendRecvPkg with big data - success.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := make([]byte, 65500)
|
|
|
|
data[100] = byte(65)
|
|
|
|
data[65400] = byte(85)
|
|
|
|
result, err := conn.SendRecvPkg(data)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result, data)
|
2019-12-11 14:50:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-11 21:22:41 +08:00
|
|
|
func Test_Pool_Package_Timeout(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.RecvPkg()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2020-03-20 08:49:40 +08:00
|
|
|
gtest.Assert(conn.SendPkg(data), nil)
|
2019-12-11 14:50:25 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := []byte("10000")
|
|
|
|
result, err := conn.SendRecvPkgWithTimeout(data, time.Millisecond*500)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(result, nil)
|
2019-12-11 14:50:25 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := []byte("10000")
|
|
|
|
result, err := conn.SendRecvPkgWithTimeout(data, time.Second*2)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result, data)
|
2019-12-11 14:50:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-11 21:22:41 +08:00
|
|
|
func Test_Pool_Package_Option(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
option := gtcp.PkgOption{HeaderSize: 1}
|
|
|
|
for {
|
|
|
|
data, err := conn.RecvPkg(option)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2020-03-20 08:49:40 +08:00
|
|
|
gtest.Assert(conn.SendPkg(data, option), nil)
|
2019-12-11 14:50:25 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// SendRecvPkg with big data - failure.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := make([]byte, 0xFF+1)
|
|
|
|
result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 1})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(result, nil)
|
2019-12-11 14:50:25 +08:00
|
|
|
})
|
|
|
|
// SendRecvPkg with big data - success.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 14:50:25 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := make([]byte, 0xFF)
|
|
|
|
data[100] = byte(65)
|
|
|
|
data[200] = byte(85)
|
|
|
|
result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 1})
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result, data)
|
2019-12-11 14:50:25 +08:00
|
|
|
})
|
2022-05-23 22:45:12 +08:00
|
|
|
// SendRecvPkgWithTimeout with big data - failure.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
defer conn.Close()
|
|
|
|
data := make([]byte, 0xFF+1)
|
|
|
|
result, err := conn.SendRecvPkgWithTimeout(data, time.Second, gtcp.PkgOption{HeaderSize: 1})
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(result, nil)
|
|
|
|
})
|
|
|
|
// SendRecvPkgWithTimeout with big data - success.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(s.GetListenedAddress())
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
defer conn.Close()
|
|
|
|
data := make([]byte, 0xFF)
|
|
|
|
data[100] = byte(65)
|
|
|
|
data[200] = byte(85)
|
|
|
|
result, err := conn.SendRecvPkgWithTimeout(data, time.Second, gtcp.PkgOption{HeaderSize: 1})
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(result, data)
|
|
|
|
})
|
2019-12-11 14:50:25 +08:00
|
|
|
}
|