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
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Basic(t *testing.T) {
|
2021-09-28 19:04:36 +08:00
|
|
|
var (
|
|
|
|
ctx = context.TODO()
|
|
|
|
)
|
2022-01-22 15:33:31 +08:00
|
|
|
p, _ := gudp.GetFreePort()
|
2020-03-11 16:08:17 +08:00
|
|
|
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 {
|
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++ {
|
|
|
|
conn, err := gudp.NewConn(fmt.Sprintf("127.0.0.1:%d", p))
|
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)
|
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++ {
|
|
|
|
conn, err := gudp.NewConn(fmt.Sprintf("127.0.0.1:%d", p))
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// 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++ {
|
|
|
|
err := gudp.Send(fmt.Sprintf("127.0.0.1:%d", p), []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++ {
|
|
|
|
result, err := gudp.SendRecv(fmt.Sprintf("127.0.0.1:%d", p), []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) {
|
2021-09-28 19:04:36 +08:00
|
|
|
var (
|
|
|
|
ctx = context.TODO()
|
|
|
|
)
|
2022-01-22 15:33:31 +08:00
|
|
|
p, _ := gudp.GetFreePort()
|
2020-03-11 23:54:35 +08:00
|
|
|
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 {
|
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) {
|
2020-03-11 23:54:35 +08:00
|
|
|
result, err := gudp.SendRecv(fmt.Sprintf("127.0.0.1:%d", p), []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) {
|
2020-03-11 23:54:35 +08:00
|
|
|
result, err := gudp.SendRecv(fmt.Sprintf("127.0.0.1:%d", p), []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
|
|
|
})
|
|
|
|
}
|