12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085 |
- // Copyright © 2021 Jeffrey H. Johnson <trnsz@pobox.com>.
- // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
- // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
- // Copyright © 2021 Gridfinity, LLC. <admin@gridfinity.com>.
- //
- // All rights reserved.
- //
- // All use of this code is governed by the MIT license.
- // The complete license is available in the LICENSE file.
- package gfcp_test
- import (
- "fmt"
- "io"
- "log"
- "net"
- "net/http"
- _ "net/http/pprof"
- "runtime"
- "sync"
- "testing"
- "time"
- "github.com/johnsonjh/gfcp"
- u "github.com/johnsonjh/leaktestfe"
- )
- const (
- portEcho = "127.0.0.1:9079"
- portSink = "127.0.0.1:19609"
- portTinyBufferEcho = "127.0.0.1:29609"
- portListerner = "127.0.0.1:9078"
- )
- func init() {
- go func() {
- log.Println(
- http.ListenAndServe(
- "127.0.0.1:8881",
- nil,
- ),
- )
- }()
- go echoServer()
- go sinkServer()
- go tinyBufferEchoServer()
- }
- func dialEcho() (
- *gfcp.UDPSession,
- error,
- ) {
- sess, err := gfcp.DialWithOptions(
- portEcho,
- 10,
- 3,
- )
- if err != nil {
- panic(
- err,
- )
- }
- sess.SetStreamMode(
- true,
- )
- sess.SetStreamMode(
- false,
- )
- sess.SetStreamMode(
- true,
- )
- sess.SetWindowSize(
- 1380,
- 1380,
- )
- sess.SetReadBuffer(
- 64 * 1024 * 1024,
- )
- sess.SetWriteBuffer(
- 64 * 1024 * 1024,
- )
- sess.SetStreamMode(
- true,
- )
- sess.SetNoDelay(
- 1,
- 10,
- 2,
- 1,
- )
- sess.SetMtu(
- 1400,
- )
- sess.SetMtu(
- 9000,
- )
- sess.SetMtu(
- 1400,
- )
- sess.SetACKNoDelay(
- true,
- )
- sess.SetACKNoDelay(
- false,
- )
- sess.SetDeadline(
- time.Now().Add(
- time.Minute,
- ),
- )
- return sess, err
- }
- func dialSink() (
- *gfcp.UDPSession,
- error,
- ) {
- sess, err := gfcp.DialWithOptions(
- portSink,
- 0,
- 0,
- )
- if err != nil {
- panic(
- err,
- )
- }
- sess.SetStreamMode(
- true,
- )
- sess.SetWindowSize(
- 1380,
- 1380,
- )
- sess.SetReadBuffer(
- 64 * 1024 * 1024,
- )
- sess.SetWriteBuffer(
- 64 * 1024 * 1024,
- )
- sess.SetStreamMode(
- true,
- )
- sess.SetNoDelay(
- 1,
- 10,
- 2,
- 1,
- )
- sess.SetMtu(
- 1400,
- )
- sess.SetACKNoDelay(
- false,
- )
- sess.SetDeadline(
- time.Now().Add(
- time.Minute,
- ),
- )
- return sess, err
- }
- func dialTinyBufferEcho() (
- *gfcp.UDPSession,
- error,
- ) {
- sess, err := gfcp.DialWithOptions(
- portTinyBufferEcho,
- 10,
- 3,
- )
- if err != nil {
- panic(
- err,
- )
- }
- return sess, err
- }
- func listenEcho() (
- net.Listener,
- error,
- ) {
- return gfcp.ListenWithOptions(
- portEcho,
- 10,
- 3,
- )
- }
- func listenTinyBufferEcho() (
- net.Listener,
- error,
- ) {
- return gfcp.ListenWithOptions(
- portTinyBufferEcho,
- 10,
- 3,
- )
- }
- func listenSink() (
- net.Listener,
- error,
- ) {
- return gfcp.ListenWithOptions(
- portSink,
- 0,
- 0,
- )
- }
- func echoServer() {
- l, err := listenEcho()
- if err != nil {
- panic(
- err,
- )
- }
- go func() {
- GFcplistener := l.(*gfcp.Listener)
- GFcplistener.SetReadBuffer(
- 64 * 1024 * 1024,
- )
- GFcplistener.SetWriteBuffer(
- 64 * 1024 * 1024,
- )
- GFcplistener.SetDSCP(
- 46,
- )
- for {
- s, err := l.Accept()
- if err != nil {
- return
- }
- s.(*gfcp.UDPSession).SetReadBuffer(
- 512 * 1024 * 1024,
- )
- s.(*gfcp.UDPSession).SetWriteBuffer(
- 512 * 1024 * 1024,
- )
- go handleEcho(s.(*gfcp.UDPSession))
- }
- }()
- }
- func sinkServer() {
- l, err := listenSink()
- if err != nil {
- panic(
- err,
- )
- }
- go func() {
- GFcplistener := l.(*gfcp.Listener)
- GFcplistener.SetReadBuffer(
- 64 * 1024 * 1024,
- )
- GFcplistener.SetWriteBuffer(
- 64 * 1024 * 1024,
- )
- GFcplistener.SetDSCP(
- 46,
- )
- for {
- s, err := l.Accept()
- if err != nil {
- return
- }
- go handleSink(s.(*gfcp.UDPSession))
- }
- }()
- }
- func tinyBufferEchoServer() {
- l, err := listenTinyBufferEcho()
- if err != nil {
- panic(
- err,
- )
- }
- go func() {
- for {
- s, err := l.Accept()
- if err != nil {
- return
- }
- go handleTinyBufferEcho(s.(*gfcp.UDPSession))
- }
- }()
- }
- func handleEcho(
- conn *gfcp.UDPSession,
- ) {
- conn.SetStreamMode(
- true,
- )
- conn.SetWindowSize(
- 8192,
- 8192,
- )
- conn.SetNoDelay(
- 1,
- 10,
- 2,
- 1,
- )
- conn.SetDSCP(
- 46,
- )
- conn.SetMtu(
- 1480,
- )
- conn.SetACKNoDelay(
- false,
- )
- conn.SetReadDeadline(
- time.Now().Add(
- time.Minute,
- ),
- )
- conn.SetWriteDeadline(
- time.Now().Add(
- time.Minute,
- ),
- )
- buf := make(
- []byte,
- 65536,
- )
- for {
- n, err := conn.Read(
- buf,
- )
- if err != nil {
- panic(
- err,
- )
- }
- conn.Write(
- buf[:n],
- )
- }
- }
- func handleSink(
- conn *gfcp.UDPSession,
- ) {
- conn.SetStreamMode(
- true,
- )
- conn.SetWindowSize(
- 8192,
- 8192,
- )
- conn.SetNoDelay(
- 1,
- 10,
- 2,
- 1,
- )
- conn.SetDSCP(
- 46,
- )
- conn.SetMtu(
- 1400,
- )
- conn.SetACKNoDelay(
- false,
- )
- conn.SetReadDeadline(
- time.Now().Add(
- time.Minute,
- ),
- )
- conn.SetWriteDeadline(
- time.Now().Add(
- time.Minute,
- ),
- )
- buf := make(
- []byte,
- 65536,
- )
- for {
- _, err := conn.Read(
- buf,
- )
- if err != nil {
- panic(
- err,
- )
- }
- }
- }
- func handleTinyBufferEcho(
- conn *gfcp.UDPSession,
- ) {
- conn.SetStreamMode(
- true,
- )
- buf := make(
- []byte,
- 2,
- )
- for {
- n, err := conn.Read(
- buf,
- )
- if err != nil {
- panic(
- err,
- )
- }
- conn.Write(
- buf[:n],
- )
- }
- }
- func TestTimeout(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- buf := make(
- []byte,
- 10,
- )
- cli.SetDeadline(
- time.Now().Add(
- time.Second,
- ),
- )
- <-time.After(
- 2 * time.Second,
- )
- n, err := cli.Read(
- buf,
- )
- if n != 0 || err == nil {
- t.Fail()
- }
- cli.Close()
- }
- func TestSendRecv(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- cli.SetWriteDelay(
- true,
- )
- cli.SetDUP(
- 1,
- )
- const (
- N = 100
- )
- buf := make(
- []byte,
- 10,
- )
- for i := 0; i < N; i++ {
- msg := fmt.Sprintf(
- "hello%v",
- i,
- )
- cli.Write(
- []byte(
- msg,
- ),
- )
- if n, err := cli.Read(
- buf,
- ); err == nil {
- if string(
- buf[:n],
- ) != msg {
- t.Fail()
- }
- } else {
- panic(
- err,
- )
- }
- }
- cli.Close()
- }
- func TestSendVector(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- cli.SetWriteDelay(
- false,
- )
- const N = 100
- buf := make(
- []byte,
- 20,
- )
- v := make(
- [][]byte,
- 2,
- )
- for i := 0; i < N; i++ {
- v[0] = []byte(
- fmt.Sprintf(
- "holas%v",
- i,
- ))
- v[1] = []byte(
- fmt.Sprintf(
- "amigo%v",
- i,
- ))
- msg :=
- fmt.Sprintf(
- "holas%vamigo%v",
- i,
- i,
- )
- cli.WriteBuffers(
- v,
- )
- if n, err := cli.Read(
- buf,
- ); err == nil {
- if string(
- buf[:n],
- ) != msg {
- t.Error(
- string(
- buf[:n],
- ),
- msg,
- )
- }
- } else {
- panic(
- err,
- )
- }
- }
- cli.Close()
- }
- func TestTinyBufferReceiver(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialTinyBufferEcho()
- if err != nil {
- panic(
- err,
- )
- }
- const (
- N = 100
- )
- snd := byte(
- 0,
- )
- fillBuffer := func(
- buf []byte,
- ) {
- for i := 0; i < len(
- buf,
- ); i++ {
- buf[i] = snd
- snd++
- }
- }
- rcv := byte(
- 0,
- )
- check := func(
- buf []byte,
- ) bool {
- for i := 0; i < len(
- buf,
- ); i++ {
- if buf[i] != rcv {
- return false
- }
- rcv++
- }
- return true
- }
- sndbuf := make(
- []byte,
- 7,
- )
- rcvbuf := make(
- []byte,
- 7,
- )
- for i := 0; i < N; i++ {
- fillBuffer(
- sndbuf,
- )
- cli.Write(
- sndbuf,
- )
- if n, err := io.ReadFull(
- cli,
- rcvbuf,
- ); err == nil {
- if !check(
- rcvbuf[:n],
- ) {
- t.Fail()
- }
- } else {
- panic(
- err,
- )
- }
- }
- cli.Close()
- }
- func TestClose(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- buf := make(
- []byte,
- 10,
- )
- cli.Close()
- if cli.Close() == nil {
- t.Fail()
- }
- n, err := cli.Write(
- buf,
- )
- if n != 0 || err == nil {
- t.Fail()
- }
- n, err = cli.Read(
- buf,
- )
- if n != 0 || err == nil {
- t.Fail()
- }
- cli.Close()
- }
- func TestParallel(
- t *testing.T,
- ) {
- concurrent := 128
- if runtime.GOOS == "darwin" {
- t.Log(
- "\n--- WARN: Running on macOS: Retargetting concurrency:\t32",
- )
- concurrent = 32
- }
- if runtime.GOOS == "windows" {
- t.Log(
- "\n--- WARN: Running on Windows: Retargetting concurrency:\t8",
- )
- concurrent = 8
- }
- t.Log(
- fmt.Sprintf(
- "\n--- INFO: Target concurrency:\t%v",
- concurrent,
- ),
- )
- t.Parallel()
- t.Log(
- fmt.Sprintf(
- "\tStage 1/2:\tGoroutines:\t%v",
- runtime.NumGoroutine(),
- ),
- )
- defer u.Leakplug(
- t,
- )
- var wg sync.WaitGroup
- wg.Add(
- concurrent,
- )
- for i := 0; i < concurrent; i++ {
- go parallelClient(
- &wg,
- )
- }
- t.Log(
- fmt.Sprintf(
- "\tStage 2/2:\tGoroutines:\t%v",
- runtime.NumGoroutine(),
- ),
- )
- wg.Wait()
- t.Log(
- fmt.Sprintf(
- "\tStage 2/3:\tGoroutines:\t%v",
- runtime.NumGoroutine(),
- ),
- )
- }
- func parallelClient(
- wg *sync.WaitGroup,
- ) (
- err error,
- ) {
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- err = echoTester(
- cli,
- 64,
- 64,
- )
- wg.Done()
- return
- }
- func BenchmarkEchoSpeed1K(
- b *testing.B,
- ) {
- speedclient(
- b,
- 1*1000,
- )
- }
- func BenchmarkEchoSpeed4K(
- b *testing.B,
- ) {
- speedclient(
- b,
- 4*1000,
- )
- }
- func BenchmarkEchoSpeed64K(
- b *testing.B,
- ) {
- speedclient(
- b,
- 64*1000,
- )
- }
- func BenchmarkEchoSpeed256K(
- b *testing.B,
- ) {
- speedclient(
- b,
- 256*1000,
- )
- }
- func BenchmarkEchoSpeed512K(
- b *testing.B,
- ) {
- speedclient(
- b,
- 512*1000,
- )
- }
- func BenchmarkEchoSpeed1M(
- b *testing.B,
- ) {
- speedclient(
- b,
- 1*1000*1000,
- )
- }
- func BenchmarkEchoSpeed4M(
- b *testing.B,
- ) {
- speedclient(
- b,
- 4*1000*1000,
- )
- }
- func BenchmarkEchoSpeed8M(
- b *testing.B,
- ) {
- speedclient(
- b,
- 8*1000*1000,
- )
- }
- func speedclient(
- b *testing.B,
- nbytes int,
- ) {
- b.ReportAllocs()
- cli, err := dialEcho()
- if err != nil {
- panic(
- err,
- )
- }
- if err := echoTester(
- cli,
- nbytes,
- b.N,
- ); err != nil {
- b.Fail()
- }
- b.SetBytes(
- int64(
- nbytes,
- ),
- )
- }
- func BenchmarkSinkSpeed1K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 1*1000,
- )
- }
- func BenchmarkSinkSpeed4K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 4*1000,
- )
- }
- func BenchmarkSinkSpeed64K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 64*1000,
- )
- }
- func BenchmarkSinkSpeed256K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 256*1000,
- )
- }
- func BenchmarkSinkSpeed512K(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 512*1000,
- )
- }
- func BenchmarkSinkSpeed1M(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 1*1000*1000,
- )
- }
- func BenchmarkSinkSpeed4M(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 4*1000*1000,
- )
- }
- func BenchmarkSinkSpeed8M(
- b *testing.B,
- ) {
- sinkclient(
- b,
- 8*1000*1000,
- )
- }
- func sinkclient(
- b *testing.B,
- nbytes int,
- ) {
- b.ReportAllocs()
- cli, err := dialSink()
- if err != nil {
- panic(
- err,
- )
- }
- sinkTester(
- cli,
- nbytes,
- b.N,
- )
- b.SetBytes(
- int64(
- nbytes,
- ),
- )
- }
- func echoTester(
- cli net.Conn,
- msglen,
- msgcount int,
- ) error {
- buf := make(
- []byte,
- msglen,
- )
- for i := 0; i < msgcount; i++ {
- if _, err := cli.Write(
- buf,
- ); err != nil {
- return err
- }
- nrecv := 0
- for {
- n, err := cli.Read(
- buf,
- )
- if err != nil {
- return err
- }
- nrecv += n
- if nrecv == msglen {
- break
- }
- }
- }
- return nil
- }
- func sinkTester(
- cli *gfcp.UDPSession,
- msglen,
- msgcount int,
- ) error {
- buf := make(
- []byte,
- msglen,
- )
- for i := 0; i < msgcount; i++ {
- if _, err := cli.Write(
- buf,
- ); err != nil {
- return err
- }
- }
- return nil
- }
- func TestSnsi(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- t.Log(
- *gfcp.DefaultSnsi.Copy(),
- )
- t.Log(
- gfcp.DefaultSnsi.Header(),
- )
- t.Log(
- gfcp.DefaultSnsi.ToSlice(),
- )
- t.Log(
- "Resetting Snsi counters",
- )
- gfcp.DefaultSnsi.Reset()
- t.Log(
- gfcp.DefaultSnsi.ToSlice(),
- )
- }
- func TestListenerClose(
- t *testing.T,
- ) {
- defer u.Leakplug(
- t,
- )
- l, err := gfcp.ListenWithOptions(
- portListerner,
- 10,
- 3,
- )
- if err != nil {
- t.Fail()
- }
- l.SetReadDeadline(
- time.Now().Add(
- 3 * time.Second,
- ),
- )
- l.SetWriteDeadline(
- time.Now().Add(
- 3 * time.Second,
- ),
- )
- l.SetDeadline(
- time.Now().Add(
- 3 * time.Second,
- ),
- )
- time.Sleep(
- 1 * time.Millisecond,
- )
- if _, err := l.Accept(); err == nil {
- t.Fail()
- }
- l.Close()
- fakeaddr, _ := net.ResolveUDPAddr(
- "udp6",
- "127.0.0.1:7162",
- )
- if l.CloseSession(
- fakeaddr,
- ) {
- t.Fail()
- }
- }
|