File: rtt_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 (142 lines) | stat: -rw-r--r-- 3,363 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package self_test

import (
	"context"
	"fmt"
	"io"
	"net"
	"testing"
	"time"

	"github.com/quic-go/quic-go"
	quicproxy "github.com/quic-go/quic-go/integrationtests/tools/proxy"
	"github.com/stretchr/testify/require"
)

func runServerForRTTTest(t *testing.T) (net.Addr, <-chan error) {
	ln, err := quic.Listen(
		newUDPConnLocalhost(t),
		getTLSConfig(),
		getQuicConfig(nil),
	)
	require.NoError(t, err)
	t.Cleanup(func() { ln.Close() })

	errChan := make(chan error, 1)
	go func() {
		defer close(errChan)
		for {
			conn, err := ln.Accept(context.Background())
			if err != nil {
				errChan <- fmt.Errorf("accept error: %w", err)
				return
			}
			str, err := conn.OpenStream()
			if err != nil {
				errChan <- fmt.Errorf("open stream error: %w", err)
				return
			}
			_, err = str.Write(PRData)
			if err != nil {
				errChan <- fmt.Errorf("write error: %w", err)
				return
			}
			str.Close()
		}
	}()

	return ln.Addr(), errChan
}

func TestDownloadWithFixedRTT(t *testing.T) {
	addr, errChan := runServerForRTTTest(t)

	for _, rtt := range []time.Duration{
		10 * time.Millisecond,
		100 * time.Millisecond,
		250 * time.Millisecond,
	} {
		t.Run(fmt.Sprintf("RTT %s", rtt), func(t *testing.T) {
			t.Cleanup(func() {
				select {
				case err := <-errChan:
					t.Errorf("server error: %v", err)
				default:
				}
			})

			proxy := quicproxy.Proxy{
				Conn:        newUDPConnLocalhost(t),
				ServerAddr:  &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: addr.(*net.UDPAddr).Port},
				DelayPacket: func(quicproxy.Direction, net.Addr, net.Addr, []byte) time.Duration { return rtt / 2 },
			}
			require.NoError(t, proxy.Start())
			t.Cleanup(func() { 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, "")

			str, err := conn.AcceptStream(ctx)
			require.NoError(t, err)
			data, err := io.ReadAll(str)
			require.NoError(t, err)
			require.Equal(t, PRData, data)
		})
	}
}

func TestDownloadWithReordering(t *testing.T) {
	addr, errChan := runServerForRTTTest(t)

	for _, rtt := range []time.Duration{
		5 * time.Millisecond,
		30 * time.Millisecond,
	} {
		t.Run(fmt.Sprintf("RTT %s", rtt), func(t *testing.T) {
			t.Cleanup(func() {
				select {
				case err := <-errChan:
					t.Errorf("server error: %v", err)
				default:
				}
			})

			proxy := quicproxy.Proxy{
				Conn:       newUDPConnLocalhost(t),
				ServerAddr: &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: addr.(*net.UDPAddr).Port},
				DelayPacket: func(quicproxy.Direction, net.Addr, net.Addr, []byte) time.Duration {
					return randomDuration(rtt/2, rtt*3/2) / 2
				},
			}
			require.NoError(t, proxy.Start())
			t.Cleanup(func() { 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, "")

			str, err := conn.AcceptStream(ctx)
			require.NoError(t, err)
			data, err := io.ReadAll(str)
			require.NoError(t, err)
			require.Equal(t, PRData, data)
		})
	}
}