Files
kcp-go/kcp_test.go
T
xtaci 582575ef06 Squashed commit of the following:
commit a8ae293a9ddee042c35e2501f93fa5dfa2425302
Merge: bdaf81e 1a84985
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 22:48:13 2020 +0800

    Merge branch 'master' into autotune

commit bdaf81e88e
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 22:38:31 2020 +0800

    add loc

commit ce66a98f95
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 19:53:42 2020 +0800

    lint

commit ad7383d8a3
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 19:05:35 2020 +0800

    fix typo

commit dc0fc2364c
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:47:14 2020 +0800

    adjust tests

commit ec45b72c39
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:45:17 2020 +0800

    remove obsolete decoder in listener

commit 827aa70e29
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:42:42 2020 +0800

    use hierarchical defensive receiving strategy

commit f5ca6b0e3a
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:34:30 2020 +0800

    optimize condition

commit ff34f7683f
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:18:00 2020 +0800

    fix test

commit a08b274651
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:13:20 2020 +0800

    solve compability issue with non FEC sender

commit ab7596783b
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:02:34 2020 +0800

    implements autotune in fec decoder

commit 260f61b3eb
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 14:34:48 2020 +0800

    hack 16-bit to identify FEC from packet

commit 105bfec567
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 2 21:38:47 2020 +0800

    add autotune
2020-10-09 22:52:11 +08:00

136 lines
2.9 KiB
Go

package kcp
import (
"io"
"net"
"sync"
"testing"
"time"
"github.com/xtaci/lossyconn"
)
const repeat = 16
func TestLossyConn1(t *testing.T) {
t.Log("testing loss rate 10%, rtt 200ms")
t.Log("testing link with nodelay parameters:1 10 2 1")
client, err := lossyconn.NewLossyConn(0.1, 100)
if err != nil {
t.Fatal(err)
}
server, err := lossyconn.NewLossyConn(0.1, 100)
if err != nil {
t.Fatal(err)
}
testlink(t, client, server, 1, 10, 2, 1)
}
func TestLossyConn2(t *testing.T) {
t.Log("testing loss rate 20%, rtt 200ms")
t.Log("testing link with nodelay parameters:1 10 2 1")
client, err := lossyconn.NewLossyConn(0.2, 100)
if err != nil {
t.Fatal(err)
}
server, err := lossyconn.NewLossyConn(0.2, 100)
if err != nil {
t.Fatal(err)
}
testlink(t, client, server, 1, 10, 2, 1)
}
func TestLossyConn3(t *testing.T) {
t.Log("testing loss rate 30%, rtt 200ms")
t.Log("testing link with nodelay parameters:1 10 2 1")
client, err := lossyconn.NewLossyConn(0.3, 100)
if err != nil {
t.Fatal(err)
}
server, err := lossyconn.NewLossyConn(0.3, 100)
if err != nil {
t.Fatal(err)
}
testlink(t, client, server, 1, 10, 2, 1)
}
func TestLossyConn4(t *testing.T) {
t.Log("testing loss rate 10%, rtt 200ms")
t.Log("testing link with nodelay parameters:1 10 2 0")
client, err := lossyconn.NewLossyConn(0.1, 100)
if err != nil {
t.Fatal(err)
}
server, err := lossyconn.NewLossyConn(0.1, 100)
if err != nil {
t.Fatal(err)
}
testlink(t, client, server, 1, 10, 2, 0)
}
func testlink(t *testing.T, client *lossyconn.LossyConn, server *lossyconn.LossyConn, nodelay, interval, resend, nc int) {
t.Log("testing with nodelay parameters:", nodelay, interval, resend, nc)
sess, _ := NewConn2(server.LocalAddr(), nil, 0, 0, client)
listener, _ := ServeConn(nil, 0, 0, server)
echoServer := func(l *Listener) {
for {
conn, err := l.AcceptKCP()
if err != nil {
return
}
go func() {
conn.SetNoDelay(nodelay, interval, resend, nc)
buf := make([]byte, 65536)
for {
n, err := conn.Read(buf)
if err != nil {
return
}
conn.Write(buf[:n])
}
}()
}
}
echoTester := func(s *UDPSession, raddr net.Addr) {
s.SetNoDelay(nodelay, interval, resend, nc)
buf := make([]byte, 64)
var rtt time.Duration
for i := 0; i < repeat; i++ {
start := time.Now()
s.Write(buf)
io.ReadFull(s, buf)
rtt += time.Since(start)
}
t.Log("client:", client)
t.Log("server:", server)
t.Log("avg rtt:", rtt/repeat)
t.Logf("total time: %v for %v round trip:", rtt, repeat)
}
go echoServer(listener)
echoTester(sess, server.LocalAddr())
}
func BenchmarkFlush(b *testing.B) {
kcp := NewKCP(1, func(buf []byte, size int) {})
kcp.snd_buf = make([]segment, 1024)
for k := range kcp.snd_buf {
kcp.snd_buf[k].xmit = 1
kcp.snd_buf[k].resendts = currentMs() + 10000
}
b.ResetTimer()
b.ReportAllocs()
var mu sync.Mutex
for i := 0; i < b.N; i++ {
mu.Lock()
kcp.flush(false)
mu.Unlock()
}
}