sent_packet_history_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package ackhandler
  2. import (
  3. "errors"
  4. "github.com/lucas-clemente/quic-go/internal/protocol"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/gomega"
  7. )
  8. var _ = Describe("SentPacketHistory", func() {
  9. var hist *sentPacketHistory
  10. expectInHistory := func(packetNumbers []protocol.PacketNumber) {
  11. ExpectWithOffset(1, hist.packetMap).To(HaveLen(len(packetNumbers)))
  12. ExpectWithOffset(1, hist.packetList.Len()).To(Equal(len(packetNumbers)))
  13. i := 0
  14. hist.Iterate(func(p *Packet) (bool, error) {
  15. pn := packetNumbers[i]
  16. ExpectWithOffset(1, p.PacketNumber).To(Equal(pn))
  17. ExpectWithOffset(1, hist.packetMap[pn].Value.PacketNumber).To(Equal(pn))
  18. i++
  19. return true, nil
  20. })
  21. }
  22. BeforeEach(func() {
  23. hist = newSentPacketHistory()
  24. })
  25. It("saves sent packets", func() {
  26. hist.SentPacket(&Packet{PacketNumber: 1})
  27. hist.SentPacket(&Packet{PacketNumber: 3})
  28. hist.SentPacket(&Packet{PacketNumber: 4})
  29. expectInHistory([]protocol.PacketNumber{1, 3, 4})
  30. })
  31. It("gets the length", func() {
  32. hist.SentPacket(&Packet{PacketNumber: 1})
  33. hist.SentPacket(&Packet{PacketNumber: 10})
  34. Expect(hist.Len()).To(Equal(2))
  35. })
  36. Context("getting the first outstanding packet", func() {
  37. It("gets nil, if there are no packets", func() {
  38. Expect(hist.FirstOutstanding()).To(BeNil())
  39. })
  40. It("gets the first outstanding packet", func() {
  41. hist.SentPacket(&Packet{PacketNumber: 2})
  42. hist.SentPacket(&Packet{PacketNumber: 3})
  43. front := hist.FirstOutstanding()
  44. Expect(front).ToNot(BeNil())
  45. Expect(front.PacketNumber).To(Equal(protocol.PacketNumber(2)))
  46. })
  47. It("gets the second packet if the first one is retransmitted", func() {
  48. hist.SentPacket(&Packet{PacketNumber: 1, canBeRetransmitted: true})
  49. hist.SentPacket(&Packet{PacketNumber: 3, canBeRetransmitted: true})
  50. hist.SentPacket(&Packet{PacketNumber: 4, canBeRetransmitted: true})
  51. front := hist.FirstOutstanding()
  52. Expect(front).ToNot(BeNil())
  53. Expect(front.PacketNumber).To(Equal(protocol.PacketNumber(1)))
  54. // Queue the first packet for retransmission.
  55. // The first outstanding packet should now be 3.
  56. err := hist.MarkCannotBeRetransmitted(1)
  57. Expect(err).ToNot(HaveOccurred())
  58. front = hist.FirstOutstanding()
  59. Expect(front).ToNot(BeNil())
  60. Expect(front.PacketNumber).To(Equal(protocol.PacketNumber(3)))
  61. })
  62. It("gets the third packet if the first two are retransmitted", func() {
  63. hist.SentPacket(&Packet{PacketNumber: 1, canBeRetransmitted: true})
  64. hist.SentPacket(&Packet{PacketNumber: 3, canBeRetransmitted: true})
  65. hist.SentPacket(&Packet{PacketNumber: 4, canBeRetransmitted: true})
  66. front := hist.FirstOutstanding()
  67. Expect(front).ToNot(BeNil())
  68. Expect(front.PacketNumber).To(Equal(protocol.PacketNumber(1)))
  69. // Queue the second packet for retransmission.
  70. // The first outstanding packet should still be 3.
  71. err := hist.MarkCannotBeRetransmitted(3)
  72. Expect(err).ToNot(HaveOccurred())
  73. front = hist.FirstOutstanding()
  74. Expect(front).ToNot(BeNil())
  75. Expect(front.PacketNumber).To(Equal(protocol.PacketNumber(1)))
  76. // Queue the first packet for retransmission.
  77. // The first outstanding packet should still be 4.
  78. err = hist.MarkCannotBeRetransmitted(1)
  79. Expect(err).ToNot(HaveOccurred())
  80. front = hist.FirstOutstanding()
  81. Expect(front).ToNot(BeNil())
  82. Expect(front.PacketNumber).To(Equal(protocol.PacketNumber(4)))
  83. })
  84. })
  85. It("gets a packet by packet number", func() {
  86. p := &Packet{PacketNumber: 2}
  87. hist.SentPacket(p)
  88. Expect(hist.GetPacket(2)).To(Equal(p))
  89. })
  90. It("returns nil if the packet doesn't exist", func() {
  91. Expect(hist.GetPacket(1337)).To(BeNil())
  92. })
  93. It("removes packets", func() {
  94. hist.SentPacket(&Packet{PacketNumber: 1})
  95. hist.SentPacket(&Packet{PacketNumber: 4})
  96. hist.SentPacket(&Packet{PacketNumber: 8})
  97. err := hist.Remove(4)
  98. Expect(err).ToNot(HaveOccurred())
  99. expectInHistory([]protocol.PacketNumber{1, 8})
  100. })
  101. It("errors when trying to remove a non existing packet", func() {
  102. hist.SentPacket(&Packet{PacketNumber: 1})
  103. err := hist.Remove(2)
  104. Expect(err).To(MatchError("packet 2 not found in sent packet history"))
  105. })
  106. Context("iterating", func() {
  107. BeforeEach(func() {
  108. hist.SentPacket(&Packet{PacketNumber: 10})
  109. hist.SentPacket(&Packet{PacketNumber: 14})
  110. hist.SentPacket(&Packet{PacketNumber: 18})
  111. })
  112. It("iterates over all packets", func() {
  113. var iterations []protocol.PacketNumber
  114. err := hist.Iterate(func(p *Packet) (bool, error) {
  115. iterations = append(iterations, p.PacketNumber)
  116. return true, nil
  117. })
  118. Expect(err).ToNot(HaveOccurred())
  119. Expect(iterations).To(Equal([]protocol.PacketNumber{10, 14, 18}))
  120. })
  121. It("stops iterating", func() {
  122. var iterations []protocol.PacketNumber
  123. err := hist.Iterate(func(p *Packet) (bool, error) {
  124. iterations = append(iterations, p.PacketNumber)
  125. return p.PacketNumber != 14, nil
  126. })
  127. Expect(err).ToNot(HaveOccurred())
  128. Expect(iterations).To(Equal([]protocol.PacketNumber{10, 14}))
  129. })
  130. It("returns the error", func() {
  131. testErr := errors.New("test error")
  132. var iterations []protocol.PacketNumber
  133. err := hist.Iterate(func(p *Packet) (bool, error) {
  134. iterations = append(iterations, p.PacketNumber)
  135. if p.PacketNumber == 14 {
  136. return false, testErr
  137. }
  138. return true, nil
  139. })
  140. Expect(err).To(MatchError(testErr))
  141. Expect(iterations).To(Equal([]protocol.PacketNumber{10, 14}))
  142. })
  143. })
  144. Context("retransmissions", func() {
  145. BeforeEach(func() {
  146. for i := protocol.PacketNumber(1); i <= 5; i++ {
  147. hist.SentPacket(&Packet{PacketNumber: i})
  148. }
  149. })
  150. It("errors if the packet doesn't exist", func() {
  151. err := hist.MarkCannotBeRetransmitted(100)
  152. Expect(err).To(MatchError("sent packet history: packet 100 not found"))
  153. })
  154. It("adds a sent packets as a retransmission", func() {
  155. hist.SentPacketsAsRetransmission([]*Packet{{PacketNumber: 13}}, 2)
  156. expectInHistory([]protocol.PacketNumber{1, 2, 3, 4, 5, 13})
  157. Expect(hist.GetPacket(13).isRetransmission).To(BeTrue())
  158. Expect(hist.GetPacket(13).retransmissionOf).To(Equal(protocol.PacketNumber(2)))
  159. Expect(hist.GetPacket(2).retransmittedAs).To(Equal([]protocol.PacketNumber{13}))
  160. })
  161. It("adds multiple packets sent as a retransmission", func() {
  162. hist.SentPacketsAsRetransmission([]*Packet{{PacketNumber: 13}, {PacketNumber: 15}}, 2)
  163. expectInHistory([]protocol.PacketNumber{1, 2, 3, 4, 5, 13, 15})
  164. Expect(hist.GetPacket(13).isRetransmission).To(BeTrue())
  165. Expect(hist.GetPacket(13).retransmissionOf).To(Equal(protocol.PacketNumber(2)))
  166. Expect(hist.GetPacket(15).retransmissionOf).To(Equal(protocol.PacketNumber(2)))
  167. Expect(hist.GetPacket(2).retransmittedAs).To(Equal([]protocol.PacketNumber{13, 15}))
  168. })
  169. It("adds a packet as a normal packet if the retransmitted packet doesn't exist", func() {
  170. hist.SentPacketsAsRetransmission([]*Packet{{PacketNumber: 13}}, 7)
  171. expectInHistory([]protocol.PacketNumber{1, 2, 3, 4, 5, 13})
  172. Expect(hist.GetPacket(13).isRetransmission).To(BeFalse())
  173. Expect(hist.GetPacket(13).retransmissionOf).To(BeZero())
  174. })
  175. })
  176. Context("outstanding packets", func() {
  177. It("says if it has outstanding packets", func() {
  178. Expect(hist.HasOutstandingPackets()).To(BeFalse())
  179. hist.SentPacket(&Packet{
  180. EncryptionLevel: protocol.Encryption1RTT,
  181. canBeRetransmitted: true,
  182. })
  183. Expect(hist.HasOutstandingPackets()).To(BeTrue())
  184. })
  185. It("doesn't consider non-retransmittable packets as outstanding", func() {
  186. hist.SentPacket(&Packet{
  187. EncryptionLevel: protocol.EncryptionInitial,
  188. })
  189. Expect(hist.HasOutstandingPackets()).To(BeFalse())
  190. })
  191. It("accounts for deleted packets", func() {
  192. hist.SentPacket(&Packet{
  193. PacketNumber: 10,
  194. EncryptionLevel: protocol.Encryption1RTT,
  195. canBeRetransmitted: true,
  196. })
  197. Expect(hist.HasOutstandingPackets()).To(BeTrue())
  198. err := hist.Remove(10)
  199. Expect(err).ToNot(HaveOccurred())
  200. Expect(hist.HasOutstandingPackets()).To(BeFalse())
  201. })
  202. It("doesn't count packets marked as non-retransmittable", func() {
  203. hist.SentPacket(&Packet{
  204. PacketNumber: 10,
  205. EncryptionLevel: protocol.Encryption1RTT,
  206. canBeRetransmitted: true,
  207. })
  208. Expect(hist.HasOutstandingPackets()).To(BeTrue())
  209. err := hist.MarkCannotBeRetransmitted(10)
  210. Expect(err).ToNot(HaveOccurred())
  211. Expect(hist.HasOutstandingPackets()).To(BeFalse())
  212. })
  213. It("counts the number of packets", func() {
  214. hist.SentPacket(&Packet{
  215. PacketNumber: 10,
  216. EncryptionLevel: protocol.Encryption1RTT,
  217. canBeRetransmitted: true,
  218. })
  219. hist.SentPacket(&Packet{
  220. PacketNumber: 11,
  221. EncryptionLevel: protocol.Encryption1RTT,
  222. canBeRetransmitted: true,
  223. })
  224. err := hist.Remove(11)
  225. Expect(err).ToNot(HaveOccurred())
  226. Expect(hist.HasOutstandingPackets()).To(BeTrue())
  227. err = hist.Remove(10)
  228. Expect(err).ToNot(HaveOccurred())
  229. Expect(hist.HasOutstandingPackets()).To(BeFalse())
  230. })
  231. })
  232. })