1
2
3
4
5
6
7 package quic
8
9 import (
10 "testing"
11 "time"
12 )
13
14 func TestRTTMinRTT(t *testing.T) {
15 var (
16 handshakeConfirmed = false
17 ackDelay = 0 * time.Millisecond
18 maxAckDelay = 25 * time.Millisecond
19 now = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
20 )
21 rtt := &rttState{}
22 rtt.init()
23
24
25
26 rtt.updateSample(now, handshakeConfirmed, initialSpace, 10*time.Millisecond, ackDelay, maxAckDelay)
27 if got, want := rtt.latestRTT, 10*time.Millisecond; got != want {
28 t.Errorf("on first sample: latest_rtt = %v, want %v", got, want)
29 }
30 if got, want := rtt.minRTT, 10*time.Millisecond; got != want {
31 t.Errorf("on first sample: min_rtt = %v, want %v", got, want)
32 }
33
34
35
36 rtt.updateSample(now, handshakeConfirmed, initialSpace, 20*time.Millisecond, ackDelay, maxAckDelay)
37 if got, want := rtt.latestRTT, 20*time.Millisecond; got != want {
38 t.Errorf("on increasing sample: latest_rtt = %v, want %v", got, want)
39 }
40 if got, want := rtt.minRTT, 10*time.Millisecond; got != want {
41 t.Errorf("on increasing sample: min_rtt = %v, want %v (no change)", got, want)
42 }
43
44 rtt.updateSample(now, handshakeConfirmed, initialSpace, 5*time.Millisecond, ackDelay, maxAckDelay)
45 if got, want := rtt.latestRTT, 5*time.Millisecond; got != want {
46 t.Errorf("on new minimum: latest_rtt = %v, want %v", got, want)
47 }
48 if got, want := rtt.minRTT, 5*time.Millisecond; got != want {
49 t.Errorf("on new minimum: min_rtt = %v, want %v", got, want)
50 }
51
52
53
54
55 rtt.updateSample(now, handshakeConfirmed, initialSpace, 15*time.Millisecond, ackDelay, maxAckDelay)
56 if got, want := rtt.latestRTT, 15*time.Millisecond; got != want {
57 t.Errorf("on increasing sample: latest_rtt = %v, want %v", got, want)
58 }
59 if got, want := rtt.minRTT, 5*time.Millisecond; got != want {
60 t.Errorf("on increasing sample: min_rtt = %v, want %v (no change)", got, want)
61 }
62 rtt.establishPersistentCongestion()
63 if got, want := rtt.minRTT, 15*time.Millisecond; got != want {
64 t.Errorf("after persistent congestion: min_rtt = %v, want %v", got, want)
65 }
66 }
67
68 func TestRTTInitialRTT(t *testing.T) {
69 var (
70 handshakeConfirmed = false
71 ackDelay = 0 * time.Millisecond
72 maxAckDelay = 25 * time.Millisecond
73 now = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
74 )
75 rtt := &rttState{}
76 rtt.init()
77
78
79
80
81 if got, want := rtt.smoothedRTT, 333*time.Millisecond; got != want {
82 t.Errorf("initial smoothed_rtt = %v, want %v", got, want)
83 }
84 if got, want := rtt.rttvar, 333*time.Millisecond/2; got != want {
85 t.Errorf("initial rttvar = %v, want %v", got, want)
86 }
87
88 rtt.updateSample(now, handshakeConfirmed, initialSpace, 10*time.Millisecond, ackDelay, maxAckDelay)
89 smoothedRTT := 10 * time.Millisecond
90 if got, want := rtt.smoothedRTT, smoothedRTT; got != want {
91 t.Errorf("after first rtt sample of 10ms, smoothed_rtt = %v, want %v", got, want)
92 }
93 rttvar := 5 * time.Millisecond
94 if got, want := rtt.rttvar, rttvar; got != want {
95 t.Errorf("after first rtt sample of 10ms, rttvar = %v, want %v", got, want)
96 }
97
98
99
100 ackDelay = 1 * time.Millisecond
101 rtt.updateSample(now, handshakeConfirmed, initialSpace, 10*time.Millisecond, ackDelay, maxAckDelay)
102 adjustedRTT := 10 * time.Millisecond
103 smoothedRTT = (7*smoothedRTT + adjustedRTT) / 8
104 if got, want := rtt.smoothedRTT, smoothedRTT; got != want {
105 t.Errorf("smoothed_rtt = %v, want %v", got, want)
106 }
107 rttvarSample := abs(smoothedRTT - adjustedRTT)
108 rttvar = (3*rttvar + rttvarSample) / 4
109 if got, want := rtt.rttvar, rttvar; got != want {
110 t.Errorf("rttvar = %v, want %v", got, want)
111 }
112
113
114
115 ackDelay = 30 * time.Millisecond
116 maxAckDelay = 25 * time.Millisecond
117 rtt.updateSample(now, handshakeConfirmed, handshakeSpace, 40*time.Millisecond, ackDelay, maxAckDelay)
118 adjustedRTT = 10 * time.Millisecond
119 smoothedRTT = (7*smoothedRTT + adjustedRTT) / 8
120 if got, want := rtt.smoothedRTT, smoothedRTT; got != want {
121 t.Errorf("smoothed_rtt = %v, want %v", got, want)
122 }
123 rttvarSample = abs(smoothedRTT - adjustedRTT)
124 rttvar = (3*rttvar + rttvarSample) / 4
125 if got, want := rtt.rttvar, rttvar; got != want {
126 t.Errorf("rttvar = %v, want %v", got, want)
127 }
128
129
130
131
132 ackDelay = 30 * time.Millisecond
133 maxAckDelay = 25 * time.Millisecond
134 handshakeConfirmed = true
135 rtt.updateSample(now, handshakeConfirmed, handshakeSpace, 40*time.Millisecond, ackDelay, maxAckDelay)
136 adjustedRTT = 15 * time.Millisecond
137 rttvarSample = abs(smoothedRTT - adjustedRTT)
138 rttvar = (3*rttvar + rttvarSample) / 4
139 if got, want := rtt.rttvar, rttvar; got != want {
140 t.Errorf("rttvar = %v, want %v", got, want)
141 }
142 smoothedRTT = (7*smoothedRTT + adjustedRTT) / 8
143 if got, want := rtt.smoothedRTT, smoothedRTT; got != want {
144 t.Errorf("smoothed_rtt = %v, want %v", got, want)
145 }
146
147
148
149
150 ackDelay = 25 * time.Millisecond
151 maxAckDelay = 25 * time.Millisecond
152 handshakeConfirmed = true
153 rtt.updateSample(now, handshakeConfirmed, handshakeSpace, 30*time.Millisecond, ackDelay, maxAckDelay)
154 if got, want := rtt.minRTT, 10*time.Millisecond; got != want {
155 t.Errorf("min_rtt = %v, want %v", got, want)
156 }
157
158 adjustedRTT = 30 * time.Millisecond
159 rttvarSample = abs(smoothedRTT - adjustedRTT)
160 rttvar = (3*rttvar + rttvarSample) / 4
161 if got, want := rtt.rttvar, rttvar; got != want {
162 t.Errorf("rttvar = %v, want %v", got, want)
163 }
164 smoothedRTT = (7*smoothedRTT + adjustedRTT) / 8
165 if got, want := rtt.smoothedRTT, smoothedRTT; got != want {
166 t.Errorf("smoothed_rtt = %v, want %v", got, want)
167 }
168 }
169
View as plain text