forked from quic-go/quic-go
use synctest for the packet drop test (#5393)
This commit is contained in:
@@ -19,21 +19,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type droppingRouter struct {
|
|
||||||
simnet.PerfectRouter
|
|
||||||
|
|
||||||
Drop func(simnet.Packet) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *droppingRouter) SendPacket(p simnet.Packet) error {
|
|
||||||
if d.Drop(p) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return d.PerfectRouter.SendPacket(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ simnet.Router = &droppingRouter{}
|
|
||||||
|
|
||||||
func TestConnectionCloseRetransmission(t *testing.T) {
|
func TestConnectionCloseRetransmission(t *testing.T) {
|
||||||
synctest.Test(t, func(t *testing.T) {
|
synctest.Test(t, func(t *testing.T) {
|
||||||
const rtt = 10 * time.Millisecond
|
const rtt = 10 * time.Millisecond
|
||||||
|
|||||||
@@ -9,39 +9,50 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/quic-go/quic-go"
|
"github.com/quic-go/quic-go"
|
||||||
quicproxy "github.com/quic-go/quic-go/integrationtests/tools/proxy"
|
"github.com/quic-go/quic-go/internal/protocol"
|
||||||
|
"github.com/quic-go/quic-go/internal/synctest"
|
||||||
"github.com/quic-go/quic-go/internal/wire"
|
"github.com/quic-go/quic-go/internal/wire"
|
||||||
|
"github.com/quic-go/quic-go/testutils/simnet"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDropTests(t *testing.T) {
|
func TestPacketDrops(t *testing.T) {
|
||||||
for _, direction := range []quicproxy.Direction{quicproxy.DirectionIncoming, quicproxy.DirectionOutgoing} {
|
for _, direction := range []protocol.Perspective{protocol.PerspectiveClient, protocol.PerspectiveServer} {
|
||||||
t.Run(fmt.Sprintf("in %s direction", direction), func(t *testing.T) {
|
t.Run(fmt.Sprintf("from %s", direction), func(t *testing.T) {
|
||||||
const numMessages = 15
|
testPacketDrops(t, direction)
|
||||||
const rtt = 10 * time.Millisecond
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
messageInterval := randomDuration(10*time.Millisecond, 100*time.Millisecond)
|
func testPacketDrops(t *testing.T, direction protocol.Perspective) {
|
||||||
dropDuration := randomDuration(messageInterval*3/2, 2*time.Second)
|
synctest.Test(t, func(t *testing.T) {
|
||||||
dropDelay := randomDuration(25*time.Millisecond, numMessages*messageInterval/2)
|
const numMessages = 50
|
||||||
t.Logf("sending a message every %s, %d times", messageInterval, numMessages)
|
const rtt = 10 * time.Millisecond
|
||||||
t.Logf("dropping packets for %s, after a delay of %s", dropDuration, dropDelay)
|
|
||||||
startTime := time.Now()
|
|
||||||
|
|
||||||
ln, err := quic.Listen(newUDPConnLocalhost(t), getTLSConfig(), getQuicConfig(nil))
|
addrClient := &net.UDPAddr{IP: net.ParseIP("1.0.0.1"), Port: 9001}
|
||||||
require.NoError(t, err)
|
addrServer := &net.UDPAddr{IP: net.ParseIP("1.0.0.2"), Port: 9002}
|
||||||
defer ln.Close()
|
|
||||||
|
|
||||||
var numDroppedPackets atomic.Int32
|
var numDroppedPackets atomic.Int32
|
||||||
proxy := &quicproxy.Proxy{
|
messageInterval := randomDuration(10*time.Millisecond, 100*time.Millisecond)
|
||||||
Conn: newUDPConnLocalhost(t),
|
dropDuration := randomDuration(messageInterval*3, 2*time.Second)
|
||||||
ServerAddr: ln.Addr().(*net.UDPAddr),
|
dropDelay := randomDuration(25*time.Millisecond, numMessages*messageInterval/2)
|
||||||
DelayPacket: func(quicproxy.Direction, net.Addr, net.Addr, []byte) time.Duration { return rtt / 2 },
|
|
||||||
DropPacket: func(d quicproxy.Direction, _, _ net.Addr, b []byte) bool {
|
startTime := time.Now()
|
||||||
if !d.Is(direction) {
|
n := &simnet.Simnet{
|
||||||
return false
|
Router: &droppingRouter{
|
||||||
|
Drop: func(p simnet.Packet) bool {
|
||||||
|
switch p.To {
|
||||||
|
case addrClient:
|
||||||
|
if direction == protocol.PerspectiveClient {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
case addrServer:
|
||||||
|
if direction == protocol.PerspectiveServer {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if wire.IsLongHeaderPacket(b[0]) { // don't interfere with the handshake
|
if wire.IsLongHeaderPacket(p.Data[0]) { // don't interfere with the handshake
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
drop := time.Now().After(startTime.Add(dropDelay)) && time.Now().Before(startTime.Add(dropDelay).Add(dropDuration))
|
drop := time.Now().After(startTime.Add(dropDelay)) && time.Now().Before(startTime.Add(dropDelay).Add(dropDuration))
|
||||||
@@ -50,43 +61,60 @@ func TestDropTests(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return drop
|
return drop
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
require.NoError(t, proxy.Start())
|
}
|
||||||
defer proxy.Close()
|
settings := simnet.NodeBiDiLinkSettings{
|
||||||
|
Downlink: simnet.LinkSettings{BitsPerSecond: 1e8, Latency: rtt / 4},
|
||||||
|
Uplink: simnet.LinkSettings{BitsPerSecond: 1e8, Latency: rtt / 4},
|
||||||
|
}
|
||||||
|
clientPacketConn := n.NewEndpoint(addrClient, settings)
|
||||||
|
defer clientPacketConn.Close()
|
||||||
|
serverPacketConn := n.NewEndpoint(addrServer, settings)
|
||||||
|
defer serverPacketConn.Close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
require.NoError(t, n.Start())
|
||||||
defer cancel()
|
defer n.Close()
|
||||||
conn, err := quic.Dial(ctx, newUDPConnLocalhost(t), proxy.LocalAddr(), getTLSClientConfig(), getQuicConfig(nil))
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer conn.CloseWithError(0, "")
|
|
||||||
|
|
||||||
serverConn, err := ln.Accept(ctx)
|
t.Logf("sending a message every %s, %d times", messageInterval, numMessages)
|
||||||
require.NoError(t, err)
|
t.Logf("dropping packets for %s, after a delay of %s", dropDuration, dropDelay)
|
||||||
serverStr, err := serverConn.OpenUniStream()
|
|
||||||
require.NoError(t, err)
|
ln, err := quic.Listen(serverPacketConn, getTLSConfig(), getQuicConfig(nil))
|
||||||
errChan := make(chan error, 1)
|
require.NoError(t, err)
|
||||||
go func() {
|
defer ln.Close()
|
||||||
for i := uint8(1); i <= numMessages; i++ {
|
|
||||||
if _, err := serverStr.Write([]byte{i}); err != nil {
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
errChan <- err
|
defer cancel()
|
||||||
return
|
conn, err := quic.Dial(ctx, clientPacketConn, ln.Addr().(*net.UDPAddr), getTLSClientConfig(), getQuicConfig(nil))
|
||||||
}
|
require.NoError(t, err)
|
||||||
time.Sleep(messageInterval)
|
defer conn.CloseWithError(0, "")
|
||||||
|
|
||||||
|
serverConn, err := ln.Accept(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer serverConn.CloseWithError(0, "")
|
||||||
|
serverStr, err := serverConn.OpenUniStream()
|
||||||
|
require.NoError(t, err)
|
||||||
|
errChan := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
for i := range numMessages {
|
||||||
|
time.Sleep(messageInterval)
|
||||||
|
if _, err := serverStr.Write([]byte{uint8(i + 1)}); err != nil {
|
||||||
|
errChan <- err
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
str, err := conn.AcceptUniStream(ctx)
|
|
||||||
require.NoError(t, err)
|
|
||||||
for i := uint8(1); i <= numMessages; i++ {
|
|
||||||
b := []byte{0}
|
|
||||||
n, err := str.Read(b)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, 1, n)
|
|
||||||
require.Equal(t, i, b[0])
|
|
||||||
}
|
}
|
||||||
numDropped := numDroppedPackets.Load()
|
}()
|
||||||
t.Logf("dropped %d packets", numDropped)
|
|
||||||
require.NotZero(t, numDropped)
|
str, err := conn.AcceptUniStream(ctx)
|
||||||
})
|
require.NoError(t, err)
|
||||||
}
|
for i := range numMessages {
|
||||||
|
b := []byte{0}
|
||||||
|
n, err := str.Read(b)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 1, n)
|
||||||
|
require.Equal(t, byte(i+1), b[0])
|
||||||
|
}
|
||||||
|
numDropped := numDroppedPackets.Load()
|
||||||
|
t.Logf("dropped %d packets", numDropped)
|
||||||
|
require.NotZero(t, numDropped)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
18
integrationtests/self/simnet_helper_test.go
Normal file
18
integrationtests/self/simnet_helper_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package self_test
|
||||||
|
|
||||||
|
import "github.com/quic-go/quic-go/testutils/simnet"
|
||||||
|
|
||||||
|
type droppingRouter struct {
|
||||||
|
simnet.PerfectRouter
|
||||||
|
|
||||||
|
Drop func(simnet.Packet) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *droppingRouter) SendPacket(p simnet.Packet) error {
|
||||||
|
if d.Drop(p) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return d.PerfectRouter.SendPacket(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ simnet.Router = &droppingRouter{}
|
||||||
Reference in New Issue
Block a user