gf/net/gudp/gudp_unit_basic_test.go
2020-03-30 20:47:50 +08:00

107 lines
2.6 KiB
Go

// Copyright 2019 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 gudp_test
import (
"fmt"
"github.com/gogf/gf/net/gudp"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gconv"
"testing"
"time"
)
func Test_Basic(t *testing.T) {
p, _ := ports.PopRand()
s := gudp.NewServer(fmt.Sprintf("127.0.0.1:%d", p), func(conn *gudp.Conn) {
defer conn.Close()
for {
data, err := conn.Recv(-1)
if len(data) > 0 {
if err := conn.Send(append([]byte("> "), data...)); err != nil {
glog.Error(err)
}
}
if err != nil {
break
}
}
})
go s.Run()
defer s.Close()
time.Sleep(100 * time.Millisecond)
// gudp.Conn.Send
gtest.C(t, func(t *gtest.T) {
for i := 0; i < 100; i++ {
conn, err := gudp.NewConn(fmt.Sprintf("127.0.0.1:%d", p))
t.Assert(err, nil)
t.Assert(conn.Send([]byte(gconv.String(i))), nil)
conn.Close()
}
})
// gudp.Conn.SendRecv
gtest.C(t, func(t *gtest.T) {
for i := 0; i < 100; i++ {
conn, err := gudp.NewConn(fmt.Sprintf("127.0.0.1:%d", p))
t.Assert(err, nil)
result, err := conn.SendRecv([]byte(gconv.String(i)), -1)
t.Assert(err, nil)
t.Assert(string(result), fmt.Sprintf(`> %d`, i))
conn.Close()
}
})
// gudp.Send
gtest.C(t, func(t *gtest.T) {
for i := 0; i < 100; i++ {
err := gudp.Send(fmt.Sprintf("127.0.0.1:%d", p), []byte(gconv.String(i)))
t.Assert(err, nil)
}
})
// gudp.SendRecv
gtest.C(t, func(t *gtest.T) {
for i := 0; i < 100; i++ {
result, err := gudp.SendRecv(fmt.Sprintf("127.0.0.1:%d", p), []byte(gconv.String(i)), -1)
t.Assert(err, nil)
t.Assert(string(result), fmt.Sprintf(`> %d`, i))
}
})
}
// If the read buffer size is less than the sent package size,
// the rest data would be dropped.
func Test_Buffer(t *testing.T) {
p, _ := ports.PopRand()
s := gudp.NewServer(fmt.Sprintf("127.0.0.1:%d", p), func(conn *gudp.Conn) {
defer conn.Close()
for {
data, err := conn.Recv(1)
if len(data) > 0 {
if err := conn.Send(data); err != nil {
glog.Error(err)
}
}
if err != nil {
break
}
}
})
go s.Run()
defer s.Close()
time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
result, err := gudp.SendRecv(fmt.Sprintf("127.0.0.1:%d", p), []byte("123"), -1)
t.Assert(err, nil)
t.Assert(string(result), "1")
})
gtest.C(t, func(t *gtest.T) {
result, err := gudp.SendRecv(fmt.Sprintf("127.0.0.1:%d", p), []byte("456"), -1)
t.Assert(err, nil)
t.Assert(string(result), "4")
})
}