sent_packet_handler_test.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. package ackhandler
  2. import (
  3. "time"
  4. "github.com/golang/mock/gomock"
  5. "github.com/lucas-clemente/quic-go/internal/congestion"
  6. "github.com/lucas-clemente/quic-go/internal/mocks"
  7. "github.com/lucas-clemente/quic-go/internal/protocol"
  8. "github.com/lucas-clemente/quic-go/internal/utils"
  9. "github.com/lucas-clemente/quic-go/internal/wire"
  10. . "github.com/onsi/ginkgo"
  11. . "github.com/onsi/gomega"
  12. )
  13. func retransmittablePacket(p *Packet) *Packet {
  14. if p.EncryptionLevel == protocol.EncryptionUnspecified {
  15. p.EncryptionLevel = protocol.Encryption1RTT
  16. }
  17. if p.Length == 0 {
  18. p.Length = 1
  19. }
  20. if p.SendTime.IsZero() {
  21. p.SendTime = time.Now()
  22. }
  23. p.Frames = []wire.Frame{&wire.PingFrame{}}
  24. return p
  25. }
  26. func nonRetransmittablePacket(p *Packet) *Packet {
  27. p = retransmittablePacket(p)
  28. p.Frames = []wire.Frame{
  29. &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}},
  30. }
  31. return p
  32. }
  33. func cryptoPacket(p *Packet) *Packet {
  34. p = retransmittablePacket(p)
  35. p.EncryptionLevel = protocol.EncryptionInitial
  36. return p
  37. }
  38. var _ = Describe("SentPacketHandler", func() {
  39. var (
  40. handler *sentPacketHandler
  41. streamFrame wire.StreamFrame
  42. )
  43. BeforeEach(func() {
  44. rttStats := &congestion.RTTStats{}
  45. handler = NewSentPacketHandler(42, rttStats, utils.DefaultLogger).(*sentPacketHandler)
  46. handler.SetHandshakeComplete()
  47. streamFrame = wire.StreamFrame{
  48. StreamID: 5,
  49. Data: []byte{0x13, 0x37},
  50. }
  51. })
  52. getPacket := func(pn protocol.PacketNumber, encLevel protocol.EncryptionLevel) *Packet {
  53. if el, ok := handler.getPacketNumberSpace(encLevel).history.packetMap[pn]; ok {
  54. return &el.Value
  55. }
  56. return nil
  57. }
  58. losePacket := func(pn protocol.PacketNumber, encLevel protocol.EncryptionLevel) {
  59. p := getPacket(pn, encLevel)
  60. ExpectWithOffset(1, p).ToNot(BeNil())
  61. handler.queuePacketForRetransmission(p, handler.getPacketNumberSpace(encLevel))
  62. if p.includedInBytesInFlight {
  63. p.includedInBytesInFlight = false
  64. handler.bytesInFlight -= p.Length
  65. }
  66. r := handler.DequeuePacketForRetransmission()
  67. ExpectWithOffset(1, r).ToNot(BeNil())
  68. ExpectWithOffset(1, r.PacketNumber).To(Equal(pn))
  69. }
  70. expectInPacketHistory := func(expected []protocol.PacketNumber, encLevel protocol.EncryptionLevel) {
  71. pnSpace := handler.getPacketNumberSpace(encLevel)
  72. ExpectWithOffset(1, pnSpace.history.Len()).To(Equal(len(expected)))
  73. for _, p := range expected {
  74. ExpectWithOffset(1, pnSpace.history.packetMap).To(HaveKey(p))
  75. }
  76. }
  77. updateRTT := func(rtt time.Duration) {
  78. handler.rttStats.UpdateRTT(rtt, 0, time.Now())
  79. ExpectWithOffset(1, handler.rttStats.SmoothedRTT()).To(Equal(rtt))
  80. }
  81. Context("registering sent packets", func() {
  82. It("accepts two consecutive packets", func() {
  83. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, EncryptionLevel: protocol.EncryptionHandshake}))
  84. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2, EncryptionLevel: protocol.EncryptionHandshake}))
  85. Expect(handler.handshakePackets.largestSent).To(Equal(protocol.PacketNumber(2)))
  86. expectInPacketHistory([]protocol.PacketNumber{1, 2}, protocol.EncryptionHandshake)
  87. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2)))
  88. })
  89. It("accepts packet number 0", func() {
  90. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 0, EncryptionLevel: protocol.Encryption1RTT}))
  91. Expect(handler.oneRTTPackets.largestSent).To(BeZero())
  92. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, EncryptionLevel: protocol.Encryption1RTT}))
  93. Expect(handler.oneRTTPackets.largestSent).To(Equal(protocol.PacketNumber(1)))
  94. expectInPacketHistory([]protocol.PacketNumber{0, 1}, protocol.Encryption1RTT)
  95. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2)))
  96. })
  97. It("stores the sent time", func() {
  98. sendTime := time.Now().Add(-time.Minute)
  99. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: sendTime}))
  100. Expect(handler.lastSentRetransmittablePacketTime).To(Equal(sendTime))
  101. })
  102. It("stores the sent time of crypto packets", func() {
  103. sendTime := time.Now().Add(-time.Minute)
  104. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: sendTime, EncryptionLevel: protocol.EncryptionInitial}))
  105. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2, SendTime: sendTime.Add(time.Hour), EncryptionLevel: protocol.Encryption1RTT}))
  106. Expect(handler.lastSentCryptoPacketTime).To(Equal(sendTime))
  107. })
  108. It("does not store non-retransmittable packets", func() {
  109. handler.SentPacket(nonRetransmittablePacket(&Packet{PacketNumber: 1, EncryptionLevel: protocol.Encryption1RTT}))
  110. Expect(handler.oneRTTPackets.history.Len()).To(BeZero())
  111. Expect(handler.lastSentRetransmittablePacketTime).To(BeZero())
  112. Expect(handler.bytesInFlight).To(BeZero())
  113. })
  114. })
  115. Context("ACK processing", func() {
  116. BeforeEach(func() {
  117. for i := protocol.PacketNumber(0); i < 10; i++ {
  118. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: i}))
  119. }
  120. // Increase RTT, because the tests would be flaky otherwise
  121. updateRTT(time.Hour)
  122. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(10)))
  123. })
  124. Context("ACK validation", func() {
  125. It("accepts ACKs sent in packet 0", func() {
  126. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 5}}}
  127. err := handler.ReceivedAck(ack, 0, protocol.Encryption1RTT, time.Now())
  128. Expect(err).ToNot(HaveOccurred())
  129. Expect(handler.oneRTTPackets.largestAcked).To(Equal(protocol.PacketNumber(5)))
  130. })
  131. It("accepts multiple ACKs sent in the same packet", func() {
  132. ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 3}}}
  133. ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 4}}}
  134. Expect(handler.ReceivedAck(ack1, 1337, protocol.Encryption1RTT, time.Now())).To(Succeed())
  135. Expect(handler.oneRTTPackets.largestAcked).To(Equal(protocol.PacketNumber(3)))
  136. // this wouldn't happen in practice
  137. // for testing purposes, we pretend send a different ACK frame in a duplicated packet, to be able to verify that it actually doesn't get processed
  138. Expect(handler.ReceivedAck(ack2, 1337, protocol.Encryption1RTT, time.Now())).To(Succeed())
  139. Expect(handler.oneRTTPackets.largestAcked).To(Equal(protocol.PacketNumber(4)))
  140. })
  141. It("rejects ACKs with a too high LargestAcked packet number", func() {
  142. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 9999}}}
  143. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  144. Expect(err).To(MatchError("PROTOCOL_VIOLATION: Received ACK for an unsent packet"))
  145. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(10)))
  146. })
  147. It("ignores repeated ACKs", func() {
  148. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 3}}}
  149. Expect(handler.ReceivedAck(ack, 1337, protocol.Encryption1RTT, time.Now())).To(Succeed())
  150. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(7)))
  151. Expect(handler.ReceivedAck(ack, 1337+1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  152. Expect(handler.oneRTTPackets.largestAcked).To(Equal(protocol.PacketNumber(3)))
  153. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(7)))
  154. })
  155. })
  156. Context("acks and nacks the right packets", func() {
  157. It("adjusts the LargestAcked, and adjusts the bytes in flight", func() {
  158. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 5}}}
  159. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  160. Expect(handler.oneRTTPackets.largestAcked).To(Equal(protocol.PacketNumber(5)))
  161. expectInPacketHistory([]protocol.PacketNumber{6, 7, 8, 9}, protocol.Encryption1RTT)
  162. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(4)))
  163. })
  164. It("acks packet 0", func() {
  165. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 0, Largest: 0}}}
  166. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  167. Expect(getPacket(0, protocol.Encryption1RTT)).To(BeNil())
  168. expectInPacketHistory([]protocol.PacketNumber{1, 2, 3, 4, 5, 6, 7, 8, 9}, protocol.Encryption1RTT)
  169. })
  170. It("handles an ACK frame with one missing packet range", func() {
  171. ack := &wire.AckFrame{ // lose 4 and 5
  172. AckRanges: []wire.AckRange{
  173. {Smallest: 6, Largest: 9},
  174. {Smallest: 1, Largest: 3},
  175. },
  176. }
  177. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  178. expectInPacketHistory([]protocol.PacketNumber{0, 4, 5}, protocol.Encryption1RTT)
  179. })
  180. It("does not ack packets below the LowestAcked", func() {
  181. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 3, Largest: 8}}}
  182. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  183. expectInPacketHistory([]protocol.PacketNumber{0, 1, 2, 9}, protocol.Encryption1RTT)
  184. })
  185. It("handles an ACK with multiple missing packet ranges", func() {
  186. ack := &wire.AckFrame{ // packets 2, 4 and 5, and 8 were lost
  187. AckRanges: []wire.AckRange{
  188. {Smallest: 9, Largest: 9},
  189. {Smallest: 6, Largest: 7},
  190. {Smallest: 3, Largest: 3},
  191. {Smallest: 1, Largest: 1},
  192. },
  193. }
  194. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  195. expectInPacketHistory([]protocol.PacketNumber{0, 2, 4, 5, 8}, protocol.Encryption1RTT)
  196. })
  197. It("processes an ACK frame that would be sent after a late arrival of a packet", func() {
  198. ack1 := &wire.AckFrame{ // 3 lost
  199. AckRanges: []wire.AckRange{
  200. {Smallest: 4, Largest: 6},
  201. {Smallest: 1, Largest: 2},
  202. },
  203. }
  204. Expect(handler.ReceivedAck(ack1, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  205. expectInPacketHistory([]protocol.PacketNumber{0, 3, 7, 8, 9}, protocol.Encryption1RTT)
  206. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(5)))
  207. ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 6}}} // now ack 3
  208. Expect(handler.ReceivedAck(ack2, 2, protocol.Encryption1RTT, time.Now())).To(Succeed())
  209. expectInPacketHistory([]protocol.PacketNumber{0, 7, 8, 9}, protocol.Encryption1RTT)
  210. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(4)))
  211. })
  212. It("processes an ACK frame that would be sent after a late arrival of a packet and another packet", func() {
  213. ack1 := &wire.AckFrame{
  214. AckRanges: []wire.AckRange{
  215. {Smallest: 4, Largest: 6},
  216. {Smallest: 0, Largest: 2},
  217. },
  218. }
  219. Expect(handler.ReceivedAck(ack1, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  220. expectInPacketHistory([]protocol.PacketNumber{3, 7, 8, 9}, protocol.Encryption1RTT)
  221. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(4)))
  222. ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 7}}}
  223. Expect(handler.ReceivedAck(ack2, 2, protocol.Encryption1RTT, time.Now())).To(Succeed())
  224. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2)))
  225. expectInPacketHistory([]protocol.PacketNumber{8, 9}, protocol.Encryption1RTT)
  226. })
  227. It("processes an ACK that contains old ACK ranges", func() {
  228. ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 6}}}
  229. Expect(handler.ReceivedAck(ack1, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  230. expectInPacketHistory([]protocol.PacketNumber{0, 7, 8, 9}, protocol.Encryption1RTT)
  231. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(4)))
  232. ack2 := &wire.AckFrame{
  233. AckRanges: []wire.AckRange{
  234. {Smallest: 8, Largest: 8},
  235. {Smallest: 3, Largest: 3},
  236. {Smallest: 1, Largest: 1},
  237. },
  238. }
  239. Expect(handler.ReceivedAck(ack2, 2, protocol.Encryption1RTT, time.Now())).To(Succeed())
  240. expectInPacketHistory([]protocol.PacketNumber{0, 7, 9}, protocol.Encryption1RTT)
  241. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(3)))
  242. })
  243. })
  244. Context("calculating RTT", func() {
  245. It("computes the RTT", func() {
  246. now := time.Now()
  247. // First, fake the sent times of the first, second and last packet
  248. getPacket(1, protocol.Encryption1RTT).SendTime = now.Add(-10 * time.Minute)
  249. getPacket(2, protocol.Encryption1RTT).SendTime = now.Add(-5 * time.Minute)
  250. getPacket(6, protocol.Encryption1RTT).SendTime = now.Add(-1 * time.Minute)
  251. // Now, check that the proper times are used when calculating the deltas
  252. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}
  253. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  254. Expect(err).NotTo(HaveOccurred())
  255. Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 10*time.Minute, 1*time.Second))
  256. ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 2}}}
  257. err = handler.ReceivedAck(ack, 2, protocol.Encryption1RTT, time.Now())
  258. Expect(err).NotTo(HaveOccurred())
  259. Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 5*time.Minute, 1*time.Second))
  260. ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 6}}}
  261. err = handler.ReceivedAck(ack, 3, protocol.Encryption1RTT, time.Now())
  262. Expect(err).NotTo(HaveOccurred())
  263. Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 1*time.Minute, 1*time.Second))
  264. })
  265. It("uses the DelayTime in the ACK frame", func() {
  266. now := time.Now()
  267. // make sure the rttStats have a min RTT, so that the delay is used
  268. handler.rttStats.UpdateRTT(5*time.Minute, 0, time.Now())
  269. getPacket(1, protocol.Encryption1RTT).SendTime = now.Add(-10 * time.Minute)
  270. ack := &wire.AckFrame{
  271. AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}},
  272. DelayTime: 5 * time.Minute,
  273. }
  274. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  275. Expect(err).NotTo(HaveOccurred())
  276. Expect(handler.rttStats.LatestRTT()).To(BeNumerically("~", 5*time.Minute, 1*time.Second))
  277. })
  278. })
  279. Context("determining which ACKs we have received an ACK for", func() {
  280. BeforeEach(func() {
  281. ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 80, Largest: 100}}}
  282. ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 50, Largest: 200}}}
  283. morePackets := []*Packet{
  284. {PacketNumber: 13, Frames: []wire.Frame{ack1, &streamFrame}, Length: 1, EncryptionLevel: protocol.Encryption1RTT},
  285. {PacketNumber: 14, Frames: []wire.Frame{ack2, &streamFrame}, Length: 1, EncryptionLevel: protocol.Encryption1RTT},
  286. {PacketNumber: 15, Frames: []wire.Frame{&streamFrame}, Length: 1, EncryptionLevel: protocol.Encryption1RTT},
  287. }
  288. for _, packet := range morePackets {
  289. handler.SentPacket(packet)
  290. }
  291. })
  292. It("determines which ACK we have received an ACK for", func() {
  293. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 13, Largest: 15}}}
  294. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  295. Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(201)))
  296. })
  297. It("doesn't do anything when the acked packet didn't contain an ACK", func() {
  298. ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 13, Largest: 13}}}
  299. ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 15, Largest: 15}}}
  300. Expect(handler.ReceivedAck(ack1, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  301. Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(101)))
  302. Expect(handler.ReceivedAck(ack2, 2, protocol.Encryption1RTT, time.Now())).To(Succeed())
  303. Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(101)))
  304. })
  305. It("doesn't decrease the value", func() {
  306. ack1 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 14, Largest: 14}}}
  307. ack2 := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 13, Largest: 13}}}
  308. Expect(handler.ReceivedAck(ack1, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  309. Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(201)))
  310. Expect(handler.ReceivedAck(ack2, 2, protocol.Encryption1RTT, time.Now())).To(Succeed())
  311. Expect(handler.GetLowestPacketNotConfirmedAcked()).To(Equal(protocol.PacketNumber(201)))
  312. })
  313. })
  314. })
  315. Context("ACK processing, for retransmitted packets", func() {
  316. It("sends a packet as retransmission", func() {
  317. // packet 5 was retransmitted as packet 6
  318. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 5, Length: 10, EncryptionLevel: protocol.Encryption1RTT}))
  319. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(10)))
  320. losePacket(5, protocol.Encryption1RTT)
  321. Expect(handler.bytesInFlight).To(BeZero())
  322. handler.SentPacketsAsRetransmission([]*Packet{retransmittablePacket(&Packet{PacketNumber: 6, Length: 11})}, 5)
  323. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(11)))
  324. })
  325. It("removes a packet when it is acked", func() {
  326. // packet 5 was retransmitted as packet 6
  327. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 5, Length: 10}))
  328. losePacket(5, protocol.Encryption1RTT)
  329. handler.SentPacketsAsRetransmission([]*Packet{retransmittablePacket(&Packet{PacketNumber: 6, Length: 11})}, 5)
  330. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(11)))
  331. // ack 5
  332. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 5, Largest: 5}}}
  333. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  334. Expect(err).ToNot(HaveOccurred())
  335. expectInPacketHistory([]protocol.PacketNumber{6}, protocol.Encryption1RTT)
  336. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(11)))
  337. })
  338. It("handles ACKs that ack the original packet as well as the retransmission", func() {
  339. // packet 5 was retransmitted as packet 7
  340. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 5, Length: 10}))
  341. losePacket(5, protocol.Encryption1RTT)
  342. handler.SentPacketsAsRetransmission([]*Packet{retransmittablePacket(&Packet{PacketNumber: 7, Length: 11})}, 5)
  343. // ack 5 and 7
  344. ack := &wire.AckFrame{
  345. AckRanges: []wire.AckRange{
  346. {Smallest: 7, Largest: 7},
  347. {Smallest: 5, Largest: 5},
  348. },
  349. }
  350. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  351. Expect(err).ToNot(HaveOccurred())
  352. Expect(handler.oneRTTPackets.history.Len()).To(BeZero())
  353. Expect(handler.bytesInFlight).To(BeZero())
  354. })
  355. })
  356. It("does not dequeue a packet if no ACK has been received", func() {
  357. handler.SentPacket(&Packet{PacketNumber: 1, EncryptionLevel: protocol.Encryption1RTT, SendTime: time.Now().Add(-time.Hour)})
  358. Expect(handler.DequeuePacketForRetransmission()).To(BeNil())
  359. })
  360. Context("congestion", func() {
  361. var cong *mocks.MockSendAlgorithm
  362. BeforeEach(func() {
  363. cong = mocks.NewMockSendAlgorithm(mockCtrl)
  364. handler.congestion = cong
  365. })
  366. It("should call OnSent", func() {
  367. cong.EXPECT().OnPacketSent(
  368. gomock.Any(),
  369. protocol.ByteCount(42),
  370. protocol.PacketNumber(1),
  371. protocol.ByteCount(42),
  372. true,
  373. )
  374. cong.EXPECT().TimeUntilSend(gomock.Any())
  375. handler.SentPacket(&Packet{
  376. PacketNumber: 1,
  377. Length: 42,
  378. Frames: []wire.Frame{&wire.PingFrame{}},
  379. EncryptionLevel: protocol.Encryption1RTT,
  380. })
  381. })
  382. It("should call MaybeExitSlowStart and OnPacketAcked", func() {
  383. rcvTime := time.Now().Add(-5 * time.Second)
  384. cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(3)
  385. cong.EXPECT().TimeUntilSend(gomock.Any()).Times(3)
  386. gomock.InOrder(
  387. cong.EXPECT().MaybeExitSlowStart(), // must be called before packets are acked
  388. cong.EXPECT().OnPacketAcked(protocol.PacketNumber(1), protocol.ByteCount(1), protocol.ByteCount(3), rcvTime),
  389. cong.EXPECT().OnPacketAcked(protocol.PacketNumber(2), protocol.ByteCount(1), protocol.ByteCount(3), rcvTime),
  390. )
  391. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1}))
  392. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2}))
  393. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 3}))
  394. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 2}}}
  395. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, rcvTime)
  396. Expect(err).NotTo(HaveOccurred())
  397. })
  398. It("doesn't call OnPacketAcked when a retransmitted packet is acked", func() {
  399. cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2)
  400. cong.EXPECT().TimeUntilSend(gomock.Any()).Times(2)
  401. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)}))
  402. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2}))
  403. // lose packet 1
  404. gomock.InOrder(
  405. cong.EXPECT().MaybeExitSlowStart(),
  406. cong.EXPECT().OnPacketAcked(protocol.PacketNumber(2), protocol.ByteCount(1), protocol.ByteCount(2), gomock.Any()),
  407. cong.EXPECT().OnPacketLost(protocol.PacketNumber(1), protocol.ByteCount(1), protocol.ByteCount(2)),
  408. )
  409. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}}
  410. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  411. Expect(err).ToNot(HaveOccurred())
  412. // don't EXPECT any further calls to the congestion controller
  413. ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 2}}}
  414. err = handler.ReceivedAck(ack, 2, protocol.Encryption1RTT, time.Now())
  415. Expect(err).ToNot(HaveOccurred())
  416. })
  417. It("calls OnPacketAcked and OnPacketLost with the right bytes_in_flight value", func() {
  418. cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(4)
  419. cong.EXPECT().TimeUntilSend(gomock.Any()).Times(4)
  420. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)}))
  421. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2, SendTime: time.Now().Add(-30 * time.Minute)}))
  422. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 3, SendTime: time.Now().Add(-30 * time.Minute)}))
  423. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 4, SendTime: time.Now()}))
  424. // receive the first ACK
  425. gomock.InOrder(
  426. cong.EXPECT().MaybeExitSlowStart(),
  427. cong.EXPECT().OnPacketAcked(protocol.PacketNumber(2), protocol.ByteCount(1), protocol.ByteCount(4), gomock.Any()),
  428. cong.EXPECT().OnPacketLost(protocol.PacketNumber(1), protocol.ByteCount(1), protocol.ByteCount(4)),
  429. )
  430. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}}
  431. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now().Add(-30*time.Minute))
  432. Expect(err).ToNot(HaveOccurred())
  433. // receive the second ACK
  434. gomock.InOrder(
  435. cong.EXPECT().MaybeExitSlowStart(),
  436. cong.EXPECT().OnPacketAcked(protocol.PacketNumber(4), protocol.ByteCount(1), protocol.ByteCount(2), gomock.Any()),
  437. cong.EXPECT().OnPacketLost(protocol.PacketNumber(3), protocol.ByteCount(1), protocol.ByteCount(2)),
  438. )
  439. ack = &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 4, Largest: 4}}}
  440. err = handler.ReceivedAck(ack, 2, protocol.Encryption1RTT, time.Now())
  441. Expect(err).ToNot(HaveOccurred())
  442. })
  443. It("only allows sending of ACKs when congestion limited", func() {
  444. handler.bytesInFlight = 100
  445. cong.EXPECT().GetCongestionWindow().Return(protocol.ByteCount(200))
  446. Expect(handler.SendMode()).To(Equal(SendAny))
  447. cong.EXPECT().GetCongestionWindow().Return(protocol.ByteCount(75))
  448. Expect(handler.SendMode()).To(Equal(SendAck))
  449. })
  450. It("only allows sending of ACKs when we're keeping track of MaxOutstandingSentPackets packets", func() {
  451. cong.EXPECT().GetCongestionWindow().Return(protocol.MaxByteCount).AnyTimes()
  452. cong.EXPECT().TimeUntilSend(gomock.Any()).AnyTimes()
  453. cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
  454. for i := protocol.PacketNumber(1); i < protocol.MaxOutstandingSentPackets; i++ {
  455. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: i}))
  456. Expect(handler.SendMode()).To(Equal(SendAny))
  457. }
  458. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: protocol.MaxOutstandingSentPackets}))
  459. Expect(handler.SendMode()).To(Equal(SendAck))
  460. })
  461. It("doesn't allow retransmission if congestion limited", func() {
  462. handler.bytesInFlight = 100
  463. handler.retransmissionQueue = []*Packet{{PacketNumber: 3}}
  464. cong.EXPECT().GetCongestionWindow().Return(protocol.ByteCount(50))
  465. Expect(handler.SendMode()).To(Equal(SendAck))
  466. })
  467. It("allows sending retransmissions", func() {
  468. cong.EXPECT().GetCongestionWindow().Return(protocol.MaxByteCount)
  469. handler.retransmissionQueue = []*Packet{{PacketNumber: 3}}
  470. Expect(handler.SendMode()).To(Equal(SendRetransmission))
  471. })
  472. It("allow retransmissions, if we're keeping track of between MaxOutstandingSentPackets and MaxTrackedSentPackets packets", func() {
  473. cong.EXPECT().GetCongestionWindow().Return(protocol.MaxByteCount)
  474. Expect(protocol.MaxOutstandingSentPackets).To(BeNumerically("<", protocol.MaxTrackedSentPackets))
  475. handler.retransmissionQueue = make([]*Packet, protocol.MaxOutstandingSentPackets+10)
  476. Expect(handler.SendMode()).To(Equal(SendRetransmission))
  477. handler.retransmissionQueue = make([]*Packet, protocol.MaxTrackedSentPackets)
  478. Expect(handler.SendMode()).To(Equal(SendNone))
  479. })
  480. It("allows RTOs, even when congestion limited", func() {
  481. // note that we don't EXPECT a call to GetCongestionWindow
  482. // that means retransmissions are sent without considering the congestion window
  483. handler.numProbesToSend = 1
  484. handler.retransmissionQueue = []*Packet{{PacketNumber: 3}}
  485. Expect(handler.SendMode()).To(Equal(SendPTO))
  486. })
  487. It("gets the pacing delay", func() {
  488. sendTime := time.Now().Add(-time.Minute)
  489. handler.bytesInFlight = 100
  490. cong.EXPECT().OnPacketSent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any())
  491. cong.EXPECT().TimeUntilSend(protocol.ByteCount(100)).Return(time.Hour)
  492. handler.SentPacket(&Packet{PacketNumber: 1, SendTime: sendTime, EncryptionLevel: protocol.Encryption1RTT})
  493. Expect(handler.TimeUntilSend()).To(Equal(sendTime.Add(time.Hour)))
  494. })
  495. It("allows sending of all RTO probe packets", func() {
  496. handler.numProbesToSend = 5
  497. Expect(handler.ShouldSendNumPackets()).To(Equal(5))
  498. })
  499. It("allows sending of one packet, if it should be sent immediately", func() {
  500. cong.EXPECT().TimeUntilSend(gomock.Any()).Return(time.Duration(0))
  501. Expect(handler.ShouldSendNumPackets()).To(Equal(1))
  502. })
  503. It("allows sending of multiple packets, if the pacing delay is smaller than the minimum", func() {
  504. pacingDelay := protocol.MinPacingDelay / 10
  505. cong.EXPECT().TimeUntilSend(gomock.Any()).Return(pacingDelay)
  506. Expect(handler.ShouldSendNumPackets()).To(Equal(10))
  507. })
  508. It("allows sending of multiple packets, if the pacing delay is smaller than the minimum, and not a fraction", func() {
  509. pacingDelay := protocol.MinPacingDelay * 2 / 5
  510. cong.EXPECT().TimeUntilSend(gomock.Any()).Return(pacingDelay)
  511. Expect(handler.ShouldSendNumPackets()).To(Equal(3))
  512. })
  513. })
  514. It("doesn't set an alarm if there are no outstanding packets", func() {
  515. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 10}))
  516. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 11}))
  517. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 10, Largest: 11}}}
  518. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  519. Expect(err).ToNot(HaveOccurred())
  520. Expect(handler.GetAlarmTimeout()).To(BeZero())
  521. })
  522. It("does nothing on OnAlarm if there are no outstanding packets", func() {
  523. Expect(handler.OnAlarm()).To(Succeed())
  524. Expect(handler.SendMode()).To(Equal(SendAny))
  525. })
  526. Context("probe packets", func() {
  527. It("uses the RTT from RTT stats", func() {
  528. rtt := 2 * time.Second
  529. updateRTT(rtt)
  530. Expect(handler.rttStats.SmoothedOrInitialRTT()).To(Equal(2 * time.Second))
  531. Expect(handler.rttStats.MeanDeviation()).To(Equal(time.Second))
  532. Expect(handler.computePTOTimeout()).To(Equal(time.Duration(2+4) * time.Second))
  533. })
  534. It("uses the granularity for short RTTs", func() {
  535. rtt := time.Microsecond
  536. updateRTT(rtt)
  537. Expect(handler.computePTOTimeout()).To(Equal(granularity))
  538. })
  539. It("implements exponential backoff", func() {
  540. handler.ptoCount = 0
  541. timeout := handler.computePTOTimeout()
  542. Expect(timeout).ToNot(BeZero())
  543. handler.ptoCount = 1
  544. Expect(handler.computePTOTimeout()).To(Equal(2 * timeout))
  545. handler.ptoCount = 2
  546. Expect(handler.computePTOTimeout()).To(Equal(4 * timeout))
  547. })
  548. It("sets the TPO send mode until two packets is sent", func() {
  549. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)}))
  550. handler.OnAlarm()
  551. Expect(handler.SendMode()).To(Equal(SendPTO))
  552. Expect(handler.ShouldSendNumPackets()).To(Equal(2))
  553. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2}))
  554. Expect(handler.SendMode()).To(Equal(SendPTO))
  555. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 3}))
  556. Expect(handler.SendMode()).ToNot(Equal(SendPTO))
  557. })
  558. It("only counts retransmittable packets as probe packets", func() {
  559. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)}))
  560. handler.OnAlarm()
  561. Expect(handler.SendMode()).To(Equal(SendPTO))
  562. Expect(handler.ShouldSendNumPackets()).To(Equal(2))
  563. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2}))
  564. Expect(handler.SendMode()).To(Equal(SendPTO))
  565. for p := protocol.PacketNumber(3); p < 30; p++ {
  566. handler.SentPacket(nonRetransmittablePacket(&Packet{PacketNumber: p}))
  567. Expect(handler.SendMode()).To(Equal(SendPTO))
  568. }
  569. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 30}))
  570. Expect(handler.SendMode()).ToNot(Equal(SendPTO))
  571. })
  572. It("gets two probe packets if RTO expires", func() {
  573. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1}))
  574. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2}))
  575. updateRTT(time.Hour)
  576. Expect(handler.lossTime.IsZero()).To(BeTrue())
  577. handler.OnAlarm() // TLP
  578. handler.OnAlarm() // TLP
  579. handler.OnAlarm() // RTO
  580. p, err := handler.DequeueProbePacket()
  581. Expect(err).ToNot(HaveOccurred())
  582. Expect(p).ToNot(BeNil())
  583. Expect(p.PacketNumber).To(Equal(protocol.PacketNumber(1)))
  584. p, err = handler.DequeueProbePacket()
  585. Expect(err).ToNot(HaveOccurred())
  586. Expect(p).ToNot(BeNil())
  587. Expect(p.PacketNumber).To(Equal(protocol.PacketNumber(2)))
  588. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2)))
  589. Expect(handler.ptoCount).To(BeEquivalentTo(3))
  590. })
  591. It("doesn't delete packets transmitted as PTO from the history", func() {
  592. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)}))
  593. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2, SendTime: time.Now().Add(-time.Hour)}))
  594. handler.rttStats.UpdateRTT(time.Second, 0, time.Now())
  595. handler.OnAlarm() // TLP
  596. handler.OnAlarm() // TLP
  597. handler.OnAlarm() // RTO
  598. _, err := handler.DequeueProbePacket()
  599. Expect(err).ToNot(HaveOccurred())
  600. _, err = handler.DequeueProbePacket()
  601. Expect(err).ToNot(HaveOccurred())
  602. expectInPacketHistory([]protocol.PacketNumber{1, 2}, protocol.Encryption1RTT)
  603. Expect(handler.bytesInFlight).To(Equal(protocol.ByteCount(2)))
  604. // Send a probe packet and receive an ACK for it.
  605. // This verifies the RTO.
  606. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 3}))
  607. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 3, Largest: 3}}}
  608. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())).To(Succeed())
  609. Expect(err).ToNot(HaveOccurred())
  610. Expect(handler.oneRTTPackets.history.Len()).To(BeZero())
  611. Expect(handler.bytesInFlight).To(BeZero())
  612. Expect(handler.retransmissionQueue).To(BeEmpty()) // 1 and 2 were already sent as probe packets
  613. })
  614. It("gets packets sent before the probe packet for retransmission", func() {
  615. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: time.Now().Add(-time.Hour)}))
  616. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2, SendTime: time.Now().Add(-time.Hour)}))
  617. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 3, SendTime: time.Now().Add(-time.Hour)}))
  618. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 4, SendTime: time.Now().Add(-time.Hour)}))
  619. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 5, SendTime: time.Now().Add(-time.Hour)}))
  620. handler.OnAlarm() // TLP
  621. handler.OnAlarm() // TLP
  622. handler.OnAlarm() // RTO
  623. _, err := handler.DequeueProbePacket()
  624. Expect(err).ToNot(HaveOccurred())
  625. _, err = handler.DequeueProbePacket()
  626. Expect(err).ToNot(HaveOccurred())
  627. expectInPacketHistory([]protocol.PacketNumber{1, 2, 3, 4, 5}, protocol.Encryption1RTT)
  628. // Send a probe packet and receive an ACK for it.
  629. // This verifies the RTO.
  630. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 6}))
  631. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 6, Largest: 6}}}
  632. err = handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  633. Expect(err).ToNot(HaveOccurred())
  634. Expect(handler.oneRTTPackets.history.Len()).To(BeZero())
  635. Expect(handler.bytesInFlight).To(BeZero())
  636. Expect(handler.retransmissionQueue).To(HaveLen(3)) // packets 3, 4, 5
  637. })
  638. It("handles ACKs for the original packet", func() {
  639. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 5, SendTime: time.Now().Add(-time.Hour)}))
  640. handler.rttStats.UpdateRTT(time.Second, 0, time.Now())
  641. handler.OnAlarm() // TLP
  642. handler.OnAlarm() // TLP
  643. handler.OnAlarm() // RTO
  644. handler.SentPacketsAsRetransmission([]*Packet{retransmittablePacket(&Packet{PacketNumber: 6})}, 5)
  645. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 5, Largest: 5}}}
  646. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, time.Now())
  647. Expect(err).ToNot(HaveOccurred())
  648. err = handler.OnAlarm()
  649. Expect(err).ToNot(HaveOccurred())
  650. })
  651. It("handles ACKs for the original packet", func() {
  652. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 5, SendTime: time.Now().Add(-time.Hour)}))
  653. handler.rttStats.UpdateRTT(time.Second, 0, time.Now())
  654. err := handler.OnAlarm()
  655. Expect(err).ToNot(HaveOccurred())
  656. err = handler.OnAlarm()
  657. Expect(err).ToNot(HaveOccurred())
  658. })
  659. })
  660. Context("Delay-based loss detection", func() {
  661. It("immediately detects old packets as lost when receiving an ACK", func() {
  662. now := time.Now()
  663. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: now.Add(-time.Hour)}))
  664. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2, SendTime: now.Add(-time.Second)}))
  665. Expect(handler.lossTime.IsZero()).To(BeTrue())
  666. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}}
  667. err := handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, now)
  668. Expect(err).NotTo(HaveOccurred())
  669. Expect(handler.DequeuePacketForRetransmission()).ToNot(BeNil())
  670. Expect(handler.DequeuePacketForRetransmission()).To(BeNil())
  671. // no need to set an alarm, since packet 1 was already declared lost
  672. Expect(handler.lossTime.IsZero()).To(BeTrue())
  673. Expect(handler.bytesInFlight).To(BeZero())
  674. })
  675. It("sets the early retransmit alarm", func() {
  676. now := time.Now()
  677. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 1, SendTime: now.Add(-2 * time.Second), EncryptionLevel: protocol.Encryption1RTT}))
  678. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 2, SendTime: now.Add(-2 * time.Second), EncryptionLevel: protocol.Encryption1RTT}))
  679. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 3, SendTime: now.Add(-time.Second), EncryptionLevel: protocol.Encryption1RTT}))
  680. Expect(handler.lossTime.IsZero()).To(BeTrue())
  681. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 2, Largest: 2}}}
  682. Expect(handler.ReceivedAck(ack, 1, protocol.Encryption1RTT, now.Add(-time.Second))).To(Succeed())
  683. Expect(handler.rttStats.SmoothedRTT()).To(Equal(time.Second))
  684. // Packet 1 should be considered lost (1+1/8) RTTs after it was sent.
  685. Expect(handler.lossTime.IsZero()).To(BeFalse())
  686. Expect(handler.lossTime.Sub(getPacket(1, protocol.Encryption1RTT).SendTime)).To(Equal(time.Second * 9 / 8))
  687. Expect(handler.OnAlarm()).To(Succeed())
  688. Expect(handler.DequeuePacketForRetransmission()).NotTo(BeNil())
  689. // make sure this is not an RTO: only packet 1 is retransmissted
  690. Expect(handler.DequeuePacketForRetransmission()).To(BeNil())
  691. })
  692. })
  693. Context("crypto packets", func() {
  694. BeforeEach(func() {
  695. handler.handshakeComplete = false
  696. })
  697. It("detects the crypto timeout", func() {
  698. now := time.Now()
  699. sendTime := now.Add(-time.Minute)
  700. lastCryptoPacketSendTime := now.Add(-30 * time.Second)
  701. // send Initial packets: 1, 3
  702. // send 1-RTT packet: 100
  703. handler.SentPacket(cryptoPacket(&Packet{PacketNumber: 1, SendTime: sendTime, EncryptionLevel: protocol.EncryptionInitial}))
  704. handler.SentPacket(cryptoPacket(&Packet{PacketNumber: 3, SendTime: sendTime, EncryptionLevel: protocol.EncryptionInitial}))
  705. handler.SentPacket(retransmittablePacket(&Packet{PacketNumber: 100, SendTime: sendTime, EncryptionLevel: protocol.Encryption1RTT}))
  706. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 1, Largest: 1}}}
  707. Expect(handler.ReceivedAck(ack, 1, protocol.EncryptionInitial, now)).To(Succeed())
  708. // RTT is now 1 minute
  709. Expect(handler.rttStats.SmoothedRTT()).To(Equal(time.Minute))
  710. Expect(handler.lossTime.IsZero()).To(BeTrue())
  711. Expect(handler.GetAlarmTimeout().Sub(sendTime)).To(Equal(2 * time.Minute))
  712. Expect(handler.OnAlarm()).To(Succeed())
  713. p := handler.DequeuePacketForRetransmission()
  714. Expect(p).ToNot(BeNil())
  715. Expect(p.PacketNumber).To(Equal(protocol.PacketNumber(3)))
  716. Expect(handler.cryptoCount).To(BeEquivalentTo(1))
  717. handler.SentPacket(cryptoPacket(&Packet{PacketNumber: 4, SendTime: lastCryptoPacketSendTime}))
  718. // make sure the exponential backoff is used
  719. Expect(handler.GetAlarmTimeout().Sub(lastCryptoPacketSendTime)).To(Equal(4 * time.Minute))
  720. })
  721. It("rejects an ACK that acks packets with a higher encryption level", func() {
  722. handler.SentPacket(&Packet{
  723. PacketNumber: 13,
  724. EncryptionLevel: protocol.Encryption1RTT,
  725. Frames: []wire.Frame{&streamFrame},
  726. Length: 1,
  727. })
  728. ack := &wire.AckFrame{AckRanges: []wire.AckRange{{Smallest: 13, Largest: 13}}}
  729. err := handler.ReceivedAck(ack, 1, protocol.EncryptionHandshake, time.Now())
  730. Expect(err).To(MatchError("PROTOCOL_VIOLATION: Received ACK for an unsent packet"))
  731. })
  732. It("deletes crypto packets when the handshake completes", func() {
  733. for i := protocol.PacketNumber(0); i < 6; i++ {
  734. p := retransmittablePacket(&Packet{PacketNumber: i, EncryptionLevel: protocol.EncryptionInitial})
  735. handler.SentPacket(p)
  736. }
  737. for i := protocol.PacketNumber(0); i <= 6; i++ {
  738. p := retransmittablePacket(&Packet{PacketNumber: i, EncryptionLevel: protocol.EncryptionHandshake})
  739. handler.SentPacket(p)
  740. }
  741. handler.queuePacketForRetransmission(getPacket(1, protocol.EncryptionInitial), handler.getPacketNumberSpace(protocol.EncryptionInitial))
  742. handler.queuePacketForRetransmission(getPacket(3, protocol.EncryptionHandshake), handler.getPacketNumberSpace(protocol.EncryptionHandshake))
  743. handler.SetHandshakeComplete()
  744. Expect(handler.initialPackets.history.Len()).To(BeZero())
  745. Expect(handler.handshakePackets.history.Len()).To(BeZero())
  746. packet := handler.DequeuePacketForRetransmission()
  747. Expect(packet).To(BeNil())
  748. })
  749. })
  750. Context("peeking and popping packet number", func() {
  751. It("peeks and pops the initial packet number", func() {
  752. pn, _ := handler.PeekPacketNumber(protocol.EncryptionInitial)
  753. Expect(pn).To(Equal(protocol.PacketNumber(42)))
  754. Expect(handler.PopPacketNumber(protocol.EncryptionInitial)).To(Equal(protocol.PacketNumber(42)))
  755. })
  756. It("peeks and pops beyond the initial packet number", func() {
  757. Expect(handler.PopPacketNumber(protocol.EncryptionInitial)).To(Equal(protocol.PacketNumber(42)))
  758. Expect(handler.PopPacketNumber(protocol.EncryptionInitial)).To(BeNumerically(">", 42))
  759. })
  760. It("starts at 0 for handshake and application-data packet number space", func() {
  761. pn, _ := handler.PeekPacketNumber(protocol.EncryptionHandshake)
  762. Expect(pn).To(BeZero())
  763. Expect(handler.PopPacketNumber(protocol.EncryptionHandshake)).To(BeZero())
  764. pn, _ = handler.PeekPacketNumber(protocol.Encryption1RTT)
  765. Expect(pn).To(BeZero())
  766. Expect(handler.PopPacketNumber(protocol.Encryption1RTT)).To(BeZero())
  767. })
  768. })
  769. Context("resetting for retry", func() {
  770. It("queues outstanding packets for retransmission and cancels alarms", func() {
  771. packet := &Packet{
  772. PacketNumber: 42,
  773. EncryptionLevel: protocol.EncryptionInitial,
  774. Frames: []wire.Frame{&wire.CryptoFrame{Data: []byte("foobar")}},
  775. Length: 100,
  776. }
  777. handler.SentPacket(packet)
  778. Expect(handler.GetAlarmTimeout()).ToNot(BeZero())
  779. Expect(handler.bytesInFlight).ToNot(BeZero())
  780. Expect(handler.DequeuePacketForRetransmission()).To(BeNil())
  781. Expect(handler.SendMode()).To(Equal(SendAny))
  782. // now receive a Retry
  783. Expect(handler.ResetForRetry()).To(Succeed())
  784. Expect(handler.bytesInFlight).To(BeZero())
  785. Expect(handler.GetAlarmTimeout()).To(BeZero())
  786. Expect(handler.SendMode()).To(Equal(SendRetransmission))
  787. p := handler.DequeuePacketForRetransmission()
  788. Expect(p.PacketNumber).To(Equal(packet.PacketNumber))
  789. Expect(p.Frames).To(Equal(packet.Frames))
  790. })
  791. })
  792. })