pass a callback containing the callbacks to the stream

This commit is contained in:
Marten Seemann 2017-12-18 18:37:37 +07:00
parent 44cff87e53
commit fc8fafd15e
12 changed files with 218 additions and 165 deletions

View file

@ -21,21 +21,16 @@ var _ = Describe("Send Stream", func() {
const streamID protocol.StreamID = 1337
var (
str *sendStream
strWithTimeout io.Writer // str wrapped with gbytes.TimeoutWriter
onDataCalled bool
queuedControlFrames []wire.Frame
mockFC *mocks.MockStreamFlowController
str *sendStream
strWithTimeout io.Writer // str wrapped with gbytes.TimeoutWriter
mockFC *mocks.MockStreamFlowController
mockSender *MockStreamSender
)
onData := func() { onDataCalled = true }
queueControlFrame := func(f wire.Frame) { queuedControlFrames = append(queuedControlFrames, f) }
BeforeEach(func() {
queuedControlFrames = queuedControlFrames[:0]
onDataCalled = false
mockSender = NewMockStreamSender(mockCtrl)
mockFC = mocks.NewMockStreamFlowController(mockCtrl)
str = newSendStream(streamID, onData, queueControlFrame, mockFC, protocol.VersionWhatever)
str = newSendStream(streamID, mockSender, mockFC, protocol.VersionWhatever)
timeout := scaleDuration(250 * time.Millisecond)
strWithTimeout = gbytes.TimeoutWriter(str, timeout)
@ -47,6 +42,7 @@ var _ = Describe("Send Stream", func() {
Context("writing", func() {
It("writes and gets all data at once", func() {
mockSender.EXPECT().scheduleSending()
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999))
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(6))
mockFC.EXPECT().IsNewlyBlocked()
@ -64,7 +60,6 @@ var _ = Describe("Send Stream", func() {
f = str.popStreamFrame(1000)
return f
}).ShouldNot(BeNil())
Expect(onDataCalled).To(BeTrue())
Expect(f.Data).To(Equal([]byte("foobar")))
Expect(f.FinBit).To(BeFalse())
Expect(f.Offset).To(BeZero())
@ -75,6 +70,7 @@ var _ = Describe("Send Stream", func() {
})
It("writes and gets data in two turns", func() {
mockSender.EXPECT().scheduleSending()
frameHeaderLen := protocol.ByteCount(4)
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999)).Times(2)
mockFC.EXPECT().AddBytesSent(gomock.Any() /* protocol.ByteCount(3)*/).Times(2)
@ -111,6 +107,7 @@ var _ = Describe("Send Stream", func() {
})
It("copies the slice while writing", func() {
mockSender.EXPECT().scheduleSending()
frameHeaderSize := protocol.ByteCount(4)
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999)).Times(2)
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(1))
@ -152,6 +149,11 @@ var _ = Describe("Send Stream", func() {
Context("adding BLOCKED", func() {
It("queues a BLOCKED frame if the stream is flow control blocked", func() {
mockSender.EXPECT().scheduleSending()
mockSender.EXPECT().queueControlFrame(&wire.StreamBlockedFrame{
StreamID: streamID,
Offset: 10,
})
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999))
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(6))
// don't use offset 6 here, to make sure the BLOCKED frame contains the number returned by the flow controller
@ -168,20 +170,15 @@ var _ = Describe("Send Stream", func() {
f = str.popStreamFrame(1000)
return f
}).ShouldNot(BeNil())
Expect(queuedControlFrames).To(Equal([]wire.Frame{
&wire.StreamBlockedFrame{
StreamID: streamID,
Offset: 10,
},
}))
Expect(onDataCalled).To(BeTrue())
Eventually(done).Should(BeClosed())
})
It("doesn't queue a BLOCKED frame if the stream is flow control blocked, but the frame popped has the FIN bit set", func() {
mockSender.EXPECT().scheduleSending()
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999))
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(6))
// don't EXPECT a call to IsNewlyBlocked
// don't EXPECT a call to mockFC.IsNewlyBlocked
// don't EXPECT a call to mockSender.queueControlFrame
done := make(chan struct{})
go func() {
defer GinkgoRecover()
@ -197,7 +194,6 @@ var _ = Describe("Send Stream", func() {
return f
}).ShouldNot(BeNil())
Expect(f.FinBit).To(BeTrue())
Expect(queuedControlFrames).To(BeEmpty())
Eventually(done).Should(BeClosed())
})
})
@ -208,10 +204,10 @@ var _ = Describe("Send Stream", func() {
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
Expect(onDataCalled).To(BeFalse())
})
It("unblocks after the deadline", func() {
mockSender.EXPECT().scheduleSending()
deadline := time.Now().Add(scaleDuration(50 * time.Millisecond))
str.SetWriteDeadline(deadline)
n, err := strWithTimeout.Write([]byte("foobar"))
@ -221,6 +217,7 @@ var _ = Describe("Send Stream", func() {
})
It("returns the number of bytes written, when the deadline expires", func() {
mockSender.EXPECT().scheduleSending()
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(10000)).AnyTimes()
mockFC.EXPECT().AddBytesSent(gomock.Any())
mockFC.EXPECT().IsNewlyBlocked()
@ -246,6 +243,7 @@ var _ = Describe("Send Stream", func() {
})
It("doesn't pop any data after the deadline expired", func() {
mockSender.EXPECT().scheduleSending()
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(10000)).AnyTimes()
mockFC.EXPECT().AddBytesSent(gomock.Any())
mockFC.EXPECT().IsNewlyBlocked()
@ -268,6 +266,7 @@ var _ = Describe("Send Stream", func() {
})
It("doesn't unblock if the deadline is changed before the first one expires", func() {
mockSender.EXPECT().scheduleSending()
deadline1 := time.Now().Add(scaleDuration(50 * time.Millisecond))
deadline2 := time.Now().Add(scaleDuration(100 * time.Millisecond))
str.SetWriteDeadline(deadline1)
@ -286,6 +285,7 @@ var _ = Describe("Send Stream", func() {
})
It("unblocks earlier, when a new deadline is set", func() {
mockSender.EXPECT().scheduleSending()
deadline1 := time.Now().Add(scaleDuration(200 * time.Millisecond))
deadline2 := time.Now().Add(scaleDuration(50 * time.Millisecond))
go func() {
@ -361,6 +361,7 @@ var _ = Describe("Send Stream", func() {
})
It("doesn't get data for writing if an error occurred", func() {
mockSender.EXPECT().scheduleSending()
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999))
mockFC.EXPECT().AddBytesSent(gomock.Any())
mockFC.EXPECT().IsNewlyBlocked()
@ -388,19 +389,19 @@ var _ = Describe("Send Stream", func() {
Context("stream cancelations", func() {
Context("canceling writing", func() {
It("queues a RST_STREAM frame", func() {
mockSender.EXPECT().queueControlFrame(&wire.RstStreamFrame{
StreamID: streamID,
ByteOffset: 1234,
ErrorCode: 9876,
})
str.writeOffset = 1234
err := str.CancelWrite(9876)
Expect(err).ToNot(HaveOccurred())
Expect(queuedControlFrames).To(Equal([]wire.Frame{
&wire.RstStreamFrame{
StreamID: streamID,
ByteOffset: 1234,
ErrorCode: 9876,
},
}))
})
It("unblocks Write", func() {
mockSender.EXPECT().scheduleSending()
mockSender.EXPECT().queueControlFrame(gomock.Any())
mockFC.EXPECT().SendWindowSize().Return(protocol.MaxByteCount)
mockFC.EXPECT().AddBytesSent(gomock.Any())
mockFC.EXPECT().IsNewlyBlocked()
@ -425,12 +426,14 @@ var _ = Describe("Send Stream", func() {
})
It("cancels the context", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
Expect(str.Context().Done()).ToNot(BeClosed())
str.CancelWrite(1234)
Expect(str.Context().Done()).To(BeClosed())
})
It("doesn't allow further calls to Write", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
err := str.CancelWrite(1234)
Expect(err).ToNot(HaveOccurred())
_, err = strWithTimeout.Write([]byte("foobar"))
@ -438,12 +441,11 @@ var _ = Describe("Send Stream", func() {
})
It("only cancels once", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
err := str.CancelWrite(1234)
Expect(err).ToNot(HaveOccurred())
Expect(queuedControlFrames).To(HaveLen(1))
err = str.CancelWrite(4321)
Expect(err).ToNot(HaveOccurred())
Expect(queuedControlFrames).To(HaveLen(1))
})
It("doesn't cancel when the stream was already closed", func() {
@ -456,19 +458,19 @@ var _ = Describe("Send Stream", func() {
Context("receiving STOP_SENDING frames", func() {
It("queues a RST_STREAM frames with error code Stopping", func() {
mockSender.EXPECT().queueControlFrame(&wire.RstStreamFrame{
StreamID: streamID,
ErrorCode: errorCodeStopping,
})
str.handleStopSendingFrame(&wire.StopSendingFrame{
StreamID: streamID,
ErrorCode: 101,
})
Expect(queuedControlFrames).To(Equal([]wire.Frame{
&wire.RstStreamFrame{
StreamID: streamID,
ErrorCode: errorCodeStopping,
},
}))
})
It("unblocks Write", func() {
mockSender.EXPECT().scheduleSending()
mockSender.EXPECT().queueControlFrame(gomock.Any())
done := make(chan struct{})
go func() {
defer GinkgoRecover()
@ -488,6 +490,7 @@ var _ = Describe("Send Stream", func() {
})
It("doesn't allow further calls to Write", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
str.handleStopSendingFrame(&wire.StopSendingFrame{
StreamID: streamID,
ErrorCode: 123,
@ -515,12 +518,14 @@ var _ = Describe("Send Stream", func() {
})
It("is finished after CancelWrite", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
err := str.CancelWrite(123)
Expect(err).ToNot(HaveOccurred())
Expect(str.finished()).To(BeTrue())
})
It("is finished after receiving a STOP_SENDING (and sending a RST_STREAM)", func() {
mockSender.EXPECT().queueControlFrame(gomock.Any())
str.handleStopSendingFrame(&wire.StopSendingFrame{StreamID: streamID})
Expect(str.finished()).To(BeTrue())
})