2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-12-11 21:22:41 +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 (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2022-01-22 15:33:31 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/net/gtcp"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2022-11-01 20:12:21 +08:00
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2019-12-11 21:22:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Pool_Basic1(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2019-12-11 21:22:41 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.RecvPkg()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.SendPkg(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
2019-12-13 14:26:07 +08:00
|
|
|
defer s.Close()
|
2019-12-13 17:40:29 +08:00
|
|
|
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 21:22:41 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := []byte("9999")
|
|
|
|
err = conn.SendPkg(data)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 21:22:41 +08:00
|
|
|
err = conn.SendPkgWithTimeout(data, time.Second)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-11 21:22:41 +08:00
|
|
|
})
|
2022-05-23 22:45:12 +08:00
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2023-05-25 21:58:11 +08:00
|
|
|
_, err := gtcp.NewPoolConn("127.0.0.1:80")
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
})
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_Basic2(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2019-12-11 21:22:41 +08:00
|
|
|
conn.Close()
|
|
|
|
})
|
|
|
|
go s.Run()
|
2019-12-13 14:26:07 +08:00
|
|
|
defer s.Close()
|
2019-12-13 17:40:29 +08:00
|
|
|
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 21:22:41 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := []byte("9999")
|
|
|
|
err = conn.SendPkg(data)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-13 08:57:39 +08:00
|
|
|
//err = conn.SendPkgWithTimeout(data, time.Second)
|
2022-03-10 11:36:40 +08:00
|
|
|
//t.AssertNil(err)
|
2019-12-11 21:22:41 +08:00
|
|
|
|
|
|
|
_, err = conn.SendRecv(data, -1)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-12-11 21:22:41 +08:00
|
|
|
})
|
|
|
|
}
|
2022-05-23 22:45:12 +08:00
|
|
|
|
|
|
|
func Test_Pool_Send(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2022-05-23 22:45:12 +08:00
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
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 := []byte("9999")
|
|
|
|
err = conn.Send(data)
|
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := conn.Recv(-1)
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
t.Assert(result, data)
|
2022-05-23 22:45:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_Recv(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2022-05-23 22:45:12 +08:00
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
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 := []byte("9999")
|
|
|
|
err = conn.Send(data)
|
|
|
|
t.AssertNil(err)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := conn.Recv(-1)
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
t.Assert(result, data)
|
2022-05-23 22:45:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_RecvLine(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2022-05-23 22:45:12 +08:00
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
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 := []byte("9999\n")
|
|
|
|
err = conn.Send(data)
|
|
|
|
t.AssertNil(err)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := conn.RecvLine()
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
splitData := gstr.Split(string(data), "\n")
|
2022-10-08 21:45:21 +08:00
|
|
|
t.Assert(result, splitData[0])
|
2022-05-23 22:45:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_RecvTill(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2022-05-23 22:45:12 +08:00
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
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 := []byte("9999\n")
|
|
|
|
err = conn.Send(data)
|
|
|
|
t.AssertNil(err)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := conn.RecvTill([]byte("\n"))
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
t.Assert(result, data)
|
2022-05-23 22:45:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_RecvWithTimeout(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2022-05-23 22:45:12 +08:00
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
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 := []byte("9999")
|
|
|
|
err = conn.Send(data)
|
|
|
|
t.AssertNil(err)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := conn.RecvWithTimeout(-1, time.Millisecond*500)
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
t.Assert(data, result)
|
2022-05-23 22:45:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_SendWithTimeout(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2022-05-23 22:45:12 +08:00
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
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 := []byte("9999")
|
|
|
|
err = conn.SendWithTimeout(data, time.Millisecond*500)
|
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := conn.Recv(-1)
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
t.Assert(data, result)
|
2022-05-23 22:45:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_SendRecvWithTimeout(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) {
|
2022-05-23 22:45:12 +08:00
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
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 := []byte("9999")
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := conn.SendRecvWithTimeout(data, -1, time.Millisecond*500)
|
2022-05-23 22:45:12 +08:00
|
|
|
t.AssertNil(err)
|
2022-10-08 21:45:21 +08:00
|
|
|
t.Assert(data, result)
|
2022-05-23 22:45:12 +08:00
|
|
|
})
|
|
|
|
}
|