From 34ad9f9e638163039405f2a6ac1e33a986084725 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 14 Dec 2017 17:51:59 +0700 Subject: [PATCH] when the stream write deadline expires, return the data length written --- stream.go | 7 ++----- stream_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/stream.go b/stream.go index 6dacfd3f..273e4efc 100644 --- a/stream.go +++ b/stream.go @@ -243,13 +243,10 @@ func (s *stream) Write(p []byte) (int, error) { s.mutex.Lock() } - if err != nil { - return 0, err - } if s.err != nil { - return len(p) - len(s.dataForWriting), s.err + err = s.err } - return len(p), nil + return len(p) - len(s.dataForWriting), err } func (s *stream) GetWriteOffset() protocol.ByteCount { diff --git a/stream_test.go b/stream_test.go index 5bc197cb..617de864 100644 --- a/stream_test.go +++ b/stream_test.go @@ -927,6 +927,31 @@ var _ = Describe("Stream", func() { Expect(time.Now()).To(BeTemporally("~", deadline, scaleDuration(20*time.Millisecond))) }) + It("returns the number of bytes written, when the deadline expires", func() { + mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(10000)).AnyTimes() + mockFC.EXPECT().AddBytesSent(gomock.Any()) + deadline := time.Now().Add(scaleDuration(50 * time.Millisecond)) + str.SetWriteDeadline(deadline) + var n int + writeReturned := make(chan struct{}) + go func() { + defer GinkgoRecover() + var err error + n, err = strWithTimeout.Write(bytes.Repeat([]byte{0}, 100)) + Expect(err).To(MatchError(errDeadline)) + Expect(time.Now()).To(BeTemporally("~", deadline, scaleDuration(20*time.Millisecond))) + close(writeReturned) + }() + var frame *wire.StreamFrame + Eventually(func() *wire.StreamFrame { + defer GinkgoRecover() + frame = str.PopStreamFrame(50) + return frame + }).ShouldNot(BeNil()) + Eventually(writeReturned, scaleDuration(80*time.Millisecond)).Should(BeClosed()) + Expect(n).To(BeEquivalentTo(frame.DataLen())) + }) + It("doesn't unblock if the deadline is changed before the first one expires", func() { deadline1 := time.Now().Add(scaleDuration(50 * time.Millisecond)) deadline2 := time.Now().Add(scaleDuration(100 * time.Millisecond))