2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-11 16:08:17 +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 gudp_test
|
|
|
|
|
|
|
|
import (
|
2021-09-28 19:04:36 +08:00
|
|
|
"context"
|
2020-03-11 16:08:17 +08:00
|
|
|
"fmt"
|
2021-11-13 23:23:55 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/net/gudp"
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2020-03-11 16:08:17 +08:00
|
|
|
)
|
|
|
|
|
2022-06-13 22:01:20 +08:00
|
|
|
var (
|
|
|
|
simpleTimeout = time.Millisecond * 100
|
|
|
|
sendData = []byte("hello")
|
|
|
|
)
|
|
|
|
|
2022-10-08 21:45:21 +08:00
|
|
|
func startUDPServer(addr string) *gudp.Server {
|
2022-06-13 22:01:20 +08:00
|
|
|
s := gudp.NewServer(addr, func(conn *gudp.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
time.Sleep(simpleTimeout)
|
2022-10-08 21:45:21 +08:00
|
|
|
return s
|
2022-06-13 22:01:20 +08:00
|
|
|
}
|
|
|
|
|
2020-03-11 16:08:17 +08:00
|
|
|
func Test_Basic(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
var ctx = context.TODO()
|
|
|
|
s := gudp.NewServer(gudp.FreePortAddress, func(conn *gudp.Conn) {
|
2020-03-11 16:08:17 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if len(data) > 0 {
|
|
|
|
if err := conn.Send(append([]byte("> "), data...)); err != nil {
|
2021-09-28 19:04:36 +08:00
|
|
|
glog.Error(ctx, err)
|
2020-03-11 16:08:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-03-11 23:54:35 +08:00
|
|
|
break
|
2020-03-11 16:08:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go s.Run()
|
|
|
|
defer s.Close()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// gudp.Conn.Send
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-11 16:08:17 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gudp.NewConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(conn.Send([]byte(gconv.String(i))), nil)
|
2022-06-13 22:01:20 +08:00
|
|
|
t.AssertNil(conn.RemoteAddr())
|
|
|
|
result, err := conn.Recv(-1)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.AssertNE(conn.RemoteAddr(), nil)
|
|
|
|
t.Assert(string(result), fmt.Sprintf(`> %d`, i))
|
2020-03-11 16:08:17 +08:00
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// gudp.Conn.SendRecv
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-11 16:08:17 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gudp.NewConn(s.GetListenedAddress())
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2021-11-14 11:36:59 +08:00
|
|
|
_, err = conn.SendRecv([]byte(gconv.String(i)), -1)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2021-11-14 11:36:59 +08:00
|
|
|
//t.Assert(string(result), fmt.Sprintf(`> %d`, i))
|
2020-03-11 16:08:17 +08:00
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
2022-06-13 22:01:20 +08:00
|
|
|
// gudp.Conn.SendWithTimeout
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gudp.NewConn(s.GetListenedAddress())
|
2022-06-13 22:01:20 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
err = conn.SendWithTimeout([]byte(gconv.String(i)), time.Second)
|
|
|
|
t.AssertNil(err)
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// gudp.Conn.RecvWithTimeout
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gudp.NewConn(s.GetListenedAddress())
|
2022-06-13 22:01:20 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
err = conn.Send([]byte(gconv.String(i)))
|
|
|
|
t.AssertNil(err)
|
|
|
|
conn.SetRecvBufferWait(time.Millisecond * 100)
|
|
|
|
result, err := conn.RecvWithTimeout(-1, time.Second)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(string(result), fmt.Sprintf(`> %d`, i))
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// gudp.Conn.SendRecvWithTimeout
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gudp.NewConn(s.GetListenedAddress())
|
2022-06-13 22:01:20 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
result, err := conn.SendRecvWithTimeout([]byte(gconv.String(i)), -1, time.Second)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(string(result), fmt.Sprintf(`> %d`, i))
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
2020-03-11 16:08:17 +08:00
|
|
|
// gudp.Send
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-11 16:08:17 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2022-10-08 21:45:21 +08:00
|
|
|
err := gudp.Send(s.GetListenedAddress(), []byte(gconv.String(i)))
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-11 16:08:17 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// gudp.SendRecv
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-11 16:08:17 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2022-10-08 21:45:21 +08:00
|
|
|
result, err := gudp.SendRecv(s.GetListenedAddress(), []byte(gconv.String(i)), -1)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(string(result), fmt.Sprintf(`> %d`, i))
|
2020-03-11 16:08:17 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-11 23:54:35 +08:00
|
|
|
|
|
|
|
// If the read buffer size is less than the sent package size,
|
|
|
|
// the rest data would be dropped.
|
|
|
|
func Test_Buffer(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
var ctx = context.TODO()
|
|
|
|
s := gudp.NewServer(gudp.FreePortAddress, func(conn *gudp.Conn) {
|
2020-03-11 23:54:35 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.Recv(1)
|
|
|
|
if len(data) > 0 {
|
|
|
|
if err := conn.Send(data); err != nil {
|
2021-09-28 19:04:36 +08:00
|
|
|
glog.Error(ctx, err)
|
2020-03-11 23:54:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
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
|
|
|
result, err := gudp.SendRecv(s.GetListenedAddress(), []byte("123"), -1)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(string(result), "1")
|
2020-03-11 23:54:35 +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
|
|
|
result, err := gudp.SendRecv(s.GetListenedAddress(), []byte("456"), -1)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(string(result), "4")
|
2020-03-11 23:54:35 +08:00
|
|
|
})
|
|
|
|
}
|
2022-06-13 22:01:20 +08:00
|
|
|
|
|
|
|
func Test_NewConn(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
s := startUDPServer(gudp.FreePortAddress)
|
2022-06-13 22:01:20 +08:00
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gudp.NewConn(s.GetListenedAddress(), fmt.Sprintf("127.0.0.1:%d", gudp.MustGetFreePort()))
|
2022-06-13 22:01:20 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
conn.SetDeadline(time.Now().Add(time.Second))
|
|
|
|
t.Assert(conn.Send(sendData), nil)
|
|
|
|
conn.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
conn, err := gudp.NewConn(s.GetListenedAddress(), fmt.Sprintf("127.0.0.1:%d", 99999))
|
2022-06-13 22:01:20 +08:00
|
|
|
t.AssertNil(conn)
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
conn, err := gudp.NewConn(fmt.Sprintf("127.0.0.1:%d", 99999))
|
|
|
|
t.AssertNil(conn)
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GetFreePorts(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ports, err := gudp.GetFreePorts(2)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.AssertEQ(len(ports), 2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Server(t *testing.T) {
|
2022-10-08 21:45:21 +08:00
|
|
|
gudp.NewServer(gudp.FreePortAddress, func(conn *gudp.Conn) {
|
2022-06-13 22:01:20 +08:00
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.Recv(1)
|
|
|
|
if len(data) > 0 {
|
|
|
|
conn.Send(data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "GoFrameUDPServer")
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
server := gudp.GetServer("GoFrameUDPServer")
|
|
|
|
t.AssertNE(server, nil)
|
|
|
|
server = gudp.GetServer("TestUDPServer")
|
|
|
|
t.AssertNE(server, nil)
|
|
|
|
server.SetAddress("127.0.0.1:8888")
|
|
|
|
server.SetHandler(func(conn *gudp.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
conn.Send([]byte("OtherHandle"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|