forked from aiortc/aioquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_recovery.py
230 lines (185 loc) · 8.17 KB
/
test_recovery.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import math
from unittest import TestCase
from aioquic import tls
from aioquic.quic.packet import PACKET_TYPE_INITIAL, PACKET_TYPE_ONE_RTT
from aioquic.quic.packet_builder import QuicSentPacket
from aioquic.quic.rangeset import RangeSet
from aioquic.quic.recovery import (
QuicPacketPacer,
QuicPacketRecovery,
QuicPacketSpace,
QuicRttMonitor,
)
def send_probe():
pass
class QuicPacketPacerTest(TestCase):
def setUp(self):
self.pacer = QuicPacketPacer(max_datagram_size=1280)
def test_no_measurement(self):
self.assertIsNone(self.pacer.next_send_time(now=0.0))
self.pacer.update_after_send(now=0.0)
self.assertIsNone(self.pacer.next_send_time(now=0.0))
self.pacer.update_after_send(now=0.0)
def test_with_measurement(self):
self.assertIsNone(self.pacer.next_send_time(now=0.0))
self.pacer.update_after_send(now=0.0)
self.pacer.update_rate(congestion_window=1280000, smoothed_rtt=0.05)
self.assertEqual(self.pacer.bucket_max, 0.0008)
self.assertEqual(self.pacer.bucket_time, 0.0)
self.assertEqual(self.pacer.packet_time, 0.00005)
# 16 packets
for i in range(16):
self.assertIsNone(self.pacer.next_send_time(now=1.0))
self.pacer.update_after_send(now=1.0)
self.assertAlmostEqual(self.pacer.next_send_time(now=1.0), 1.00005)
# 2 packets
for i in range(2):
self.assertIsNone(self.pacer.next_send_time(now=1.00005))
self.pacer.update_after_send(now=1.00005)
self.assertAlmostEqual(self.pacer.next_send_time(now=1.00005), 1.0001)
# 1 packet
self.assertIsNone(self.pacer.next_send_time(now=1.0001))
self.pacer.update_after_send(now=1.0001)
self.assertAlmostEqual(self.pacer.next_send_time(now=1.0001), 1.00015)
# 2 packets
for i in range(2):
self.assertIsNone(self.pacer.next_send_time(now=1.00015))
self.pacer.update_after_send(now=1.00015)
self.assertAlmostEqual(self.pacer.next_send_time(now=1.00015), 1.0002)
class QuicPacketRecoveryTest(TestCase):
def setUp(self):
self.INITIAL_SPACE = QuicPacketSpace()
self.HANDSHAKE_SPACE = QuicPacketSpace()
self.ONE_RTT_SPACE = QuicPacketSpace()
self.recovery = QuicPacketRecovery(
initial_rtt=0.1,
max_datagram_size=1280,
peer_completed_address_validation=True,
send_probe=send_probe,
)
self.recovery.spaces = [
self.INITIAL_SPACE,
self.HANDSHAKE_SPACE,
self.ONE_RTT_SPACE,
]
def test_discard_space(self):
self.recovery.discard_space(self.INITIAL_SPACE)
def test_on_ack_received_ack_eliciting(self):
packet = QuicSentPacket(
epoch=tls.Epoch.ONE_RTT,
in_flight=True,
is_ack_eliciting=True,
is_crypto_packet=False,
packet_number=0,
packet_type=PACKET_TYPE_ONE_RTT,
sent_bytes=1280,
sent_time=0.0,
)
space = self.ONE_RTT_SPACE
# packet sent
self.recovery.on_packet_sent(packet, space)
self.assertEqual(self.recovery.bytes_in_flight, 1280)
self.assertEqual(space.ack_eliciting_in_flight, 1)
self.assertEqual(len(space.sent_packets), 1)
# packet ack'd
self.recovery.on_ack_received(
space, ack_rangeset=RangeSet([range(0, 1)]), ack_delay=0.0, now=10.0
)
self.assertEqual(self.recovery.bytes_in_flight, 0)
self.assertEqual(space.ack_eliciting_in_flight, 0)
self.assertEqual(len(space.sent_packets), 0)
# check RTT
self.assertTrue(self.recovery._rtt_initialized)
self.assertEqual(self.recovery._rtt_latest, 10.0)
self.assertEqual(self.recovery._rtt_min, 10.0)
self.assertEqual(self.recovery._rtt_smoothed, 10.0)
def test_on_ack_received_non_ack_eliciting(self):
packet = QuicSentPacket(
epoch=tls.Epoch.ONE_RTT,
in_flight=True,
is_ack_eliciting=False,
is_crypto_packet=False,
packet_number=0,
packet_type=PACKET_TYPE_ONE_RTT,
sent_bytes=1280,
sent_time=123.45,
)
space = self.ONE_RTT_SPACE
# packet sent
self.recovery.on_packet_sent(packet, space)
self.assertEqual(self.recovery.bytes_in_flight, 1280)
self.assertEqual(space.ack_eliciting_in_flight, 0)
self.assertEqual(len(space.sent_packets), 1)
# packet ack'd
self.recovery.on_ack_received(
space, ack_rangeset=RangeSet([range(0, 1)]), ack_delay=0.0, now=10.0
)
self.assertEqual(self.recovery.bytes_in_flight, 0)
self.assertEqual(space.ack_eliciting_in_flight, 0)
self.assertEqual(len(space.sent_packets), 0)
# check RTT
self.assertFalse(self.recovery._rtt_initialized)
self.assertEqual(self.recovery._rtt_latest, 0.0)
self.assertEqual(self.recovery._rtt_min, math.inf)
self.assertEqual(self.recovery._rtt_smoothed, 0.0)
def test_on_packet_lost_crypto(self):
packet = QuicSentPacket(
epoch=tls.Epoch.INITIAL,
in_flight=True,
is_ack_eliciting=True,
is_crypto_packet=True,
packet_number=0,
packet_type=PACKET_TYPE_INITIAL,
sent_bytes=1280,
sent_time=0.0,
)
space = self.INITIAL_SPACE
self.recovery.on_packet_sent(packet, space)
self.assertEqual(self.recovery.bytes_in_flight, 1280)
self.assertEqual(space.ack_eliciting_in_flight, 1)
self.assertEqual(len(space.sent_packets), 1)
self.recovery._detect_loss(space, now=1.0)
self.assertEqual(self.recovery.bytes_in_flight, 0)
self.assertEqual(space.ack_eliciting_in_flight, 0)
self.assertEqual(len(space.sent_packets), 0)
class QuicRttMonitorTest(TestCase):
def test_monitor(self):
monitor = QuicRttMonitor()
self.assertFalse(monitor.is_rtt_increasing(rtt=10, now=1000))
self.assertEqual(monitor._samples, [10, 0.0, 0.0, 0.0, 0.0])
self.assertFalse(monitor._ready)
# not taken into account
self.assertFalse(monitor.is_rtt_increasing(rtt=11, now=1000))
self.assertEqual(monitor._samples, [10, 0.0, 0.0, 0.0, 0.0])
self.assertFalse(monitor._ready)
self.assertFalse(monitor.is_rtt_increasing(rtt=11, now=1001))
self.assertEqual(monitor._samples, [10, 11, 0.0, 0.0, 0.0])
self.assertFalse(monitor._ready)
self.assertFalse(monitor.is_rtt_increasing(rtt=12, now=1002))
self.assertEqual(monitor._samples, [10, 11, 12, 0.0, 0.0])
self.assertFalse(monitor._ready)
self.assertFalse(monitor.is_rtt_increasing(rtt=13, now=1003))
self.assertEqual(monitor._samples, [10, 11, 12, 13, 0.0])
self.assertFalse(monitor._ready)
# we now have enough samples
self.assertFalse(monitor.is_rtt_increasing(rtt=14, now=1004))
self.assertEqual(monitor._samples, [10, 11, 12, 13, 14])
self.assertTrue(monitor._ready)
self.assertFalse(monitor.is_rtt_increasing(rtt=20, now=1005))
self.assertEqual(monitor._increases, 0)
self.assertFalse(monitor.is_rtt_increasing(rtt=30, now=1006))
self.assertEqual(monitor._increases, 0)
self.assertFalse(monitor.is_rtt_increasing(rtt=40, now=1007))
self.assertEqual(monitor._increases, 0)
self.assertFalse(monitor.is_rtt_increasing(rtt=50, now=1008))
self.assertEqual(monitor._increases, 0)
self.assertFalse(monitor.is_rtt_increasing(rtt=60, now=1009))
self.assertEqual(monitor._increases, 1)
self.assertFalse(monitor.is_rtt_increasing(rtt=70, now=1010))
self.assertEqual(monitor._increases, 2)
self.assertFalse(monitor.is_rtt_increasing(rtt=80, now=1011))
self.assertEqual(monitor._increases, 3)
self.assertFalse(monitor.is_rtt_increasing(rtt=90, now=1012))
self.assertEqual(monitor._increases, 4)
self.assertTrue(monitor.is_rtt_increasing(rtt=100, now=1013))
self.assertEqual(monitor._increases, 5)