File: drop_test.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (92 lines) | stat: -rw-r--r-- 2,898 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package self_test

import (
	"context"
	"fmt"
	"net"
	"sync/atomic"
	"testing"
	"time"

	"github.com/quic-go/quic-go"
	quicproxy "github.com/quic-go/quic-go/integrationtests/tools/proxy"
	"github.com/quic-go/quic-go/internal/wire"

	"github.com/stretchr/testify/require"
)

func TestDropTests(t *testing.T) {
	for _, direction := range []quicproxy.Direction{quicproxy.DirectionIncoming, quicproxy.DirectionOutgoing} {
		t.Run(fmt.Sprintf("in %s direction", direction), func(t *testing.T) {
			const numMessages = 15
			const rtt = 10 * time.Millisecond

			messageInterval := randomDuration(10*time.Millisecond, 100*time.Millisecond)
			dropDuration := randomDuration(messageInterval*3/2, 2*time.Second)
			dropDelay := randomDuration(25*time.Millisecond, numMessages*messageInterval/2)
			t.Logf("sending a message every %s, %d times", messageInterval, numMessages)
			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))
			require.NoError(t, err)
			defer ln.Close()

			var numDroppedPackets atomic.Int32
			proxy := &quicproxy.Proxy{
				Conn:        newUDPConnLocalhost(t),
				ServerAddr:  ln.Addr().(*net.UDPAddr),
				DelayPacket: func(quicproxy.Direction, net.Addr, net.Addr, []byte) time.Duration { return rtt / 2 },
				DropPacket: func(d quicproxy.Direction, _, _ net.Addr, b []byte) bool {
					if !d.Is(direction) {
						return false
					}
					if wire.IsLongHeaderPacket(b[0]) { // don't interfere with the handshake
						return false
					}
					drop := time.Now().After(startTime.Add(dropDelay)) && time.Now().Before(startTime.Add(dropDelay).Add(dropDuration))
					if drop {
						numDroppedPackets.Add(1)
					}
					return drop
				},
			}
			require.NoError(t, proxy.Start())
			defer proxy.Close()

			ctx, cancel := context.WithTimeout(context.Background(), time.Second)
			defer cancel()
			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)
			require.NoError(t, err)
			serverStr, err := serverConn.OpenUniStream()
			require.NoError(t, err)
			errChan := make(chan error, 1)
			go func() {
				for i := uint8(1); i <= numMessages; i++ {
					if _, err := serverStr.Write([]byte{i}); err != nil {
						errChan <- err
						return
					}
					time.Sleep(messageInterval)
				}
			}()

			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)
		})
	}
}