notify/v2cs_test.go

171 lines
3.9 KiB
Go
Raw Normal View History

//go:build notify_manual_soak
// +build notify_manual_soak
2021-11-12 16:04:39 +08:00
package notify
import (
"fmt"
"net"
//_ "net/http/pprof"
"sync/atomic"
"testing"
"time"
)
// This file contains long-running manual soak/stress tests.
//
// They intentionally use fixed ports, background loops and coarse sleeps, so
// they are excluded from the default go test/go vet release gate. Run them
// explicitly with:
//
// go test -tags notify_manual_soak -run 'Test_ServerTuAndClientCommon|Test_normal|Test_normal_udp'
2021-11-12 16:04:39 +08:00
func Test_ServerTuAndClientCommon(t *testing.T) {
//go http.ListenAndServe("0.0.0.0:8888", nil)
noEn := func(key, bn []byte) []byte {
return bn
}
2022-04-27 13:45:22 +08:00
_ = noEn
2021-11-12 16:04:39 +08:00
server := NewServer()
if err := UseModernPSKServer(server, integrationSharedSecret, integrationModernPSKOptions()); err != nil {
t.Fatal(err)
}
2022-04-27 13:45:22 +08:00
//server.SetDefaultCommDecode(noEn)
//server.SetDefaultCommEncode(noEn)
2021-11-12 16:04:39 +08:00
err := server.Listen("tcp", "127.0.0.1:12345")
if err != nil {
panic(err)
}
server.SetLink("notify", notify)
2022-04-27 13:45:22 +08:00
for i := 1; i <= 100; i++ {
2021-11-12 16:04:39 +08:00
go func() {
client := NewClient()
if err := UseModernPSKClient(client, integrationSharedSecret, integrationModernPSKOptions()); err != nil {
t.Fatal(err)
return
}
2022-04-27 13:45:22 +08:00
//client.SetMsgEn(noEn)
//client.SetMsgDe(noEn)
//client.SetSkipExchangeKey(true)
2021-11-12 16:04:39 +08:00
err = client.Connect("tcp", "127.0.0.1:12345")
if err != nil {
2022-04-27 13:45:22 +08:00
t.Fatal(err)
2021-11-12 16:04:39 +08:00
time.Sleep(time.Second * 2)
return
}
//client.SetLink("notify", notify)
for {
//nowd = time.Now().UnixNano()
2022-04-27 13:45:22 +08:00
client.SendWait("notify", []byte("client hello"), time.Second*15)
//client.Send("notify", []byte("client hello"))
2021-11-12 16:04:39 +08:00
//time.Sleep(time.Millisecond)
//fmt.Println("finished:", float64(time.Now().UnixNano()-nowd)/1000000)
//client.Send("notify", []byte("client"))
}
}()
}
time.Sleep(time.Second)
go func() {
time.Sleep(time.Second * 10)
server.Stop()
}()
<-server.StopMonitorChan()
fmt.Println(count2)
}
var count2 int64
func notify(msg *Message) {
//fmt.Println(string(msg.Msg.Value))
//fmt.Println("called:", float64(time.Now().UnixNano()-nowd)/1000000)
if msg.NetType == NET_SERVER {
atomic.AddInt64(&count2, 1)
msg.Reply([]byte("server reply"))
}
}
func Test_normal(t *testing.T) {
2022-04-27 13:45:22 +08:00
server, err := net.Listen("tcp", "127.0.0.1:12345")
if err != nil {
t.Fatal(err)
}
2021-11-12 16:04:39 +08:00
go func() {
for {
conn, err := server.Accept()
if err == nil {
go func() {
for {
buf := make([]byte, 1024)
_, err := conn.Read(buf)
//fmt.Println("S RECV", string(buf[:i]))
atomic.AddInt64(&count2, 1)
if err == nil {
conn.Write([]byte("hello world server"))
}
}
}()
}
}
}()
time.Sleep(time.Second * 5)
for i := 1; i <= 100; i++ {
go func() {
2022-04-27 13:45:22 +08:00
conn, err := net.Dial("tcp", "127.0.0.1:12345")
2021-11-12 16:04:39 +08:00
if err != nil {
panic(err)
}
for {
//nowd = time.Now().UnixNano()
_, err := conn.Write([]byte("hello world client"))
if err != nil {
fmt.Println(err)
}
buf := make([]byte, 1024)
conn.Read(buf)
continue
}
}()
}
time.Sleep(time.Second * 10)
fmt.Println(count2)
}
func Test_normal_udp(t *testing.T) {
ludp, _ := net.ResolveUDPAddr("udp", "127.0.0.1:12345")
conn, _ := net.ListenUDP("udp", ludp)
go func() {
for {
buf := make([]byte, 1024)
_, addr, err := conn.ReadFromUDP(buf)
fmt.Println(time.Now(), "S RECV", addr.String())
atomic.AddInt64(&count2, 1)
if err == nil {
conn.WriteToUDP([]byte("hello world server"), addr)
}
}
}()
for i := 1; i <= 100; i++ {
go func() {
conn, err := net.Dial("udp", "127.0.0.1:12345")
if err != nil {
panic(err)
}
for {
//nowd = time.Now().UnixNano()
_, err := conn.Write([]byte("hello world client"))
if err != nil {
fmt.Println(err)
}
buf := make([]byte, 1024)
conn.Read(buf)
fmt.Println(time.Now(), "C RECV")
continue
}
}()
}
time.Sleep(time.Second * 10)
fmt.Println(count2)
}