2019-12-11 21:22:41 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/net/gtcp"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Pool_Basic1(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-12-11 21:22:41 +08:00
|
|
|
s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
|
|
|
|
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) {
|
2019-12-11 21:22:41 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-12-11 21:22:41 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := []byte("9999")
|
|
|
|
err = conn.SendPkg(data)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-12-11 21:22:41 +08:00
|
|
|
err = conn.SendPkgWithTimeout(data, time.Second)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-12-11 21:22:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Pool_Basic2(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-12-11 21:22:41 +08:00
|
|
|
s := gtcp.NewServer(fmt.Sprintf(`:%d`, p), func(conn *gtcp.Conn) {
|
|
|
|
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) {
|
2019-12-11 21:22:41 +08:00
|
|
|
conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", p))
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-12-11 21:22:41 +08:00
|
|
|
defer conn.Close()
|
|
|
|
data := []byte("9999")
|
|
|
|
err = conn.SendPkg(data)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-12-13 08:57:39 +08:00
|
|
|
//err = conn.SendPkgWithTimeout(data, time.Second)
|
2020-03-19 22:56:12 +08:00
|
|
|
//t.Assert(err, nil)
|
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
|
|
|
})
|
|
|
|
}
|