stream_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package quic
  2. import (
  3. "io"
  4. "os"
  5. "strconv"
  6. "time"
  7. "github.com/lucas-clemente/quic-go/internal/mocks"
  8. "github.com/lucas-clemente/quic-go/internal/protocol"
  9. "github.com/lucas-clemente/quic-go/internal/wire"
  10. . "github.com/onsi/ginkgo"
  11. . "github.com/onsi/gomega"
  12. "github.com/onsi/gomega/gbytes"
  13. )
  14. // in the tests for the stream deadlines we set a deadline
  15. // and wait to make an assertion when Read / Write was unblocked
  16. // on the CIs, the timing is a lot less precise, so scale every duration by this factor
  17. func scaleDuration(t time.Duration) time.Duration {
  18. scaleFactor := 1
  19. if f, err := strconv.Atoi(os.Getenv("TIMESCALE_FACTOR")); err == nil { // parsing "" errors, so this works fine if the env is not set
  20. scaleFactor = f
  21. }
  22. Expect(scaleFactor).ToNot(BeZero())
  23. return time.Duration(scaleFactor) * t
  24. }
  25. var _ = Describe("Stream", func() {
  26. const streamID protocol.StreamID = 1337
  27. var (
  28. str *stream
  29. strWithTimeout io.ReadWriter // str wrapped with gbytes.Timeout{Reader,Writer}
  30. mockFC *mocks.MockStreamFlowController
  31. mockSender *MockStreamSender
  32. )
  33. BeforeEach(func() {
  34. mockSender = NewMockStreamSender(mockCtrl)
  35. mockFC = mocks.NewMockStreamFlowController(mockCtrl)
  36. str = newStream(streamID, mockSender, mockFC, protocol.VersionWhatever)
  37. timeout := scaleDuration(250 * time.Millisecond)
  38. strWithTimeout = struct {
  39. io.Reader
  40. io.Writer
  41. }{
  42. gbytes.TimeoutReader(str, timeout),
  43. gbytes.TimeoutWriter(str, timeout),
  44. }
  45. })
  46. It("gets stream id", func() {
  47. Expect(str.StreamID()).To(Equal(protocol.StreamID(1337)))
  48. })
  49. // need some stream cancelation tests here, since gQUIC doesn't cleanly separate the two stream halves
  50. Context("stream cancelations", func() {
  51. Context("for gQUIC", func() {
  52. BeforeEach(func() {
  53. str.version = versionGQUICFrames
  54. str.receiveStream.version = versionGQUICFrames
  55. str.sendStream.version = versionGQUICFrames
  56. })
  57. It("unblocks Write when receiving a RST_STREAM frame with non-zero error code", func() {
  58. mockSender.EXPECT().onHasStreamData(streamID)
  59. mockSender.EXPECT().queueControlFrame(&wire.RstStreamFrame{
  60. StreamID: streamID,
  61. ByteOffset: 1000,
  62. ErrorCode: errorCodeStoppingGQUIC,
  63. })
  64. mockSender.EXPECT().onStreamCompleted(streamID)
  65. mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(6), true)
  66. str.writeOffset = 1000
  67. f := &wire.RstStreamFrame{
  68. StreamID: streamID,
  69. ByteOffset: 6,
  70. ErrorCode: 123,
  71. }
  72. writeReturned := make(chan struct{})
  73. go func() {
  74. defer GinkgoRecover()
  75. _, err := strWithTimeout.Write([]byte("foobar"))
  76. Expect(err).To(MatchError("Stream 1337 was reset with error code 123"))
  77. Expect(err).To(BeAssignableToTypeOf(streamCanceledError{}))
  78. Expect(err.(streamCanceledError).Canceled()).To(BeTrue())
  79. Expect(err.(streamCanceledError).ErrorCode()).To(Equal(protocol.ApplicationErrorCode(123)))
  80. close(writeReturned)
  81. }()
  82. Consistently(writeReturned).ShouldNot(BeClosed())
  83. err := str.handleRstStreamFrame(f)
  84. Expect(err).ToNot(HaveOccurred())
  85. Eventually(writeReturned).Should(BeClosed())
  86. })
  87. It("unblocks Write when receiving a RST_STREAM frame with error code 0", func() {
  88. mockSender.EXPECT().onHasStreamData(streamID)
  89. mockSender.EXPECT().queueControlFrame(&wire.RstStreamFrame{
  90. StreamID: streamID,
  91. ByteOffset: 1000,
  92. ErrorCode: errorCodeStoppingGQUIC,
  93. })
  94. mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(6), true)
  95. str.writeOffset = 1000
  96. f := &wire.RstStreamFrame{
  97. StreamID: streamID,
  98. ByteOffset: 6,
  99. ErrorCode: 0,
  100. }
  101. writeReturned := make(chan struct{})
  102. go func() {
  103. defer GinkgoRecover()
  104. _, err := strWithTimeout.Write([]byte("foobar"))
  105. Expect(err).To(MatchError("Stream 1337 was reset with error code 0"))
  106. Expect(err).To(BeAssignableToTypeOf(streamCanceledError{}))
  107. Expect(err.(streamCanceledError).Canceled()).To(BeTrue())
  108. Expect(err.(streamCanceledError).ErrorCode()).To(Equal(protocol.ApplicationErrorCode(0)))
  109. close(writeReturned)
  110. }()
  111. Consistently(writeReturned).ShouldNot(BeClosed())
  112. err := str.handleRstStreamFrame(f)
  113. Expect(err).ToNot(HaveOccurred())
  114. Eventually(writeReturned).Should(BeClosed())
  115. })
  116. It("sends a RST_STREAM with error code 0, after the stream is closed", func() {
  117. str.version = versionGQUICFrames
  118. mockSender.EXPECT().onHasStreamData(streamID).Times(2) // once for the Write, once for the Close
  119. mockFC.EXPECT().SendWindowSize().Return(protocol.MaxByteCount).AnyTimes()
  120. mockFC.EXPECT().AddBytesSent(protocol.ByteCount(6))
  121. err := str.CancelRead(1234)
  122. Expect(err).ToNot(HaveOccurred())
  123. writeReturned := make(chan struct{})
  124. go func() {
  125. defer GinkgoRecover()
  126. _, err := strWithTimeout.Write([]byte("foobar"))
  127. Expect(err).ToNot(HaveOccurred())
  128. close(writeReturned)
  129. }()
  130. Eventually(func() *wire.StreamFrame {
  131. frame, _ := str.popStreamFrame(1000)
  132. return frame
  133. }).ShouldNot(BeNil())
  134. Eventually(writeReturned).Should(BeClosed())
  135. mockSender.EXPECT().queueControlFrame(&wire.RstStreamFrame{
  136. StreamID: streamID,
  137. ByteOffset: 6,
  138. ErrorCode: 0,
  139. })
  140. Expect(str.Close()).To(Succeed())
  141. })
  142. })
  143. Context("for IETF QUIC", func() {
  144. It("doesn't queue a RST_STREAM after closing the stream", func() { // this is what it does for gQUIC
  145. mockSender.EXPECT().queueControlFrame(&wire.StopSendingFrame{
  146. StreamID: streamID,
  147. ErrorCode: 1234,
  148. })
  149. mockSender.EXPECT().onHasStreamData(streamID)
  150. err := str.CancelRead(1234)
  151. Expect(err).ToNot(HaveOccurred())
  152. Expect(str.Close()).To(Succeed())
  153. })
  154. })
  155. })
  156. Context("deadlines", func() {
  157. It("sets a write deadline, when SetDeadline is called", func() {
  158. str.SetDeadline(time.Now().Add(-time.Second))
  159. n, err := strWithTimeout.Write([]byte("foobar"))
  160. Expect(err).To(MatchError(errDeadline))
  161. Expect(n).To(BeZero())
  162. })
  163. It("sets a read deadline, when SetDeadline is called", func() {
  164. mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(6), false).AnyTimes()
  165. f := &wire.StreamFrame{Data: []byte("foobar")}
  166. err := str.handleStreamFrame(f)
  167. Expect(err).ToNot(HaveOccurred())
  168. str.SetDeadline(time.Now().Add(-time.Second))
  169. b := make([]byte, 6)
  170. n, err := strWithTimeout.Read(b)
  171. Expect(err).To(MatchError(errDeadline))
  172. Expect(n).To(BeZero())
  173. })
  174. })
  175. Context("completing", func() {
  176. It("is not completed when only the receive side is completed", func() {
  177. // don't EXPECT a call to mockSender.onStreamCompleted()
  178. str.receiveStream.sender.onStreamCompleted(streamID)
  179. })
  180. It("is not completed when only the send side is completed", func() {
  181. // don't EXPECT a call to mockSender.onStreamCompleted()
  182. str.sendStream.sender.onStreamCompleted(streamID)
  183. })
  184. It("is completed when both sides are completed", func() {
  185. mockSender.EXPECT().onStreamCompleted(streamID)
  186. str.sendStream.sender.onStreamCompleted(streamID)
  187. str.receiveStream.sender.onStreamCompleted(streamID)
  188. })
  189. })
  190. })