route/vendor/github.com/lucas-clemente/quic-go/stream_test.go

1103 lines
36 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package quic
import (
"errors"
"io"
2018-01-03 19:19:49 +00:00
"runtime"
"strconv"
2017-12-12 02:51:45 +00:00
"time"
2018-01-03 19:19:49 +00:00
"os"
"github.com/golang/mock/gomock"
"github.com/lucas-clemente/quic-go/internal/mocks"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/wire"
2017-12-12 02:51:45 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2018-01-03 19:19:49 +00:00
"github.com/onsi/gomega/gbytes"
2017-12-12 02:51:45 +00:00
)
var _ = Describe("Stream", func() {
2018-01-03 19:19:49 +00:00
const streamID protocol.StreamID = 1337
2017-12-12 02:51:45 +00:00
var (
2018-01-03 19:19:49 +00:00
str *stream
strWithTimeout io.ReadWriter // str wrapped with gbytes.Timeout{Reader,Writer}
onDataCalled bool
2017-12-12 02:51:45 +00:00
resetCalled bool
resetCalledForStream protocol.StreamID
resetCalledAtOffset protocol.ByteCount
2018-01-03 19:19:49 +00:00
mockFC *mocks.MockStreamFlowController
2017-12-12 02:51:45 +00:00
)
2018-01-03 19:19:49 +00:00
// in the tests for the stream deadlines we set a deadline
// and wait to make an assertion when Read / Write was unblocked
// on the CIs, the timing is a lot less precise, so scale every duration by this factor
scaleDuration := func(t time.Duration) time.Duration {
scaleFactor := 1
if f, err := strconv.Atoi(os.Getenv("TIMESCALE_FACTOR")); err == nil { // parsing "" errors, so this works fine if the env is not set
scaleFactor = f
}
Expect(scaleFactor).ToNot(BeZero())
return time.Duration(scaleFactor) * t
}
2017-12-12 02:51:45 +00:00
onData := func() {
onDataCalled = true
}
onReset := func(id protocol.StreamID, offset protocol.ByteCount) {
resetCalled = true
resetCalledForStream = id
resetCalledAtOffset = offset
}
BeforeEach(func() {
onDataCalled = false
resetCalled = false
2018-01-03 19:19:49 +00:00
mockFC = mocks.NewMockStreamFlowController(mockCtrl)
str = newStream(streamID, onData, onReset, mockFC, protocol.VersionWhatever)
timeout := scaleDuration(250 * time.Millisecond)
strWithTimeout = struct {
io.Reader
io.Writer
}{
gbytes.TimeoutReader(str, timeout),
gbytes.TimeoutWriter(str, timeout),
}
2017-12-12 02:51:45 +00:00
})
It("gets stream id", func() {
Expect(str.StreamID()).To(Equal(protocol.StreamID(1337)))
})
Context("reading", func() {
It("reads a single StreamFrame", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(4))
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
}
err := str.AddStreamFrame(&frame)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
Expect(b).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF}))
})
It("reads a single StreamFrame in multiple goes", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2))
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2))
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
}
err := str.AddStreamFrame(&frame)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 2)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(2))
Expect(b).To(Equal([]byte{0xDE, 0xAD}))
2018-01-03 19:19:49 +00:00
n, err = strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(2))
Expect(b).To(Equal([]byte{0xBE, 0xEF}))
})
It("reads all data available", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2)).Times(2)
frame1 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD},
}
2018-01-03 19:19:49 +00:00
frame2 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 2,
Data: []byte{0xBE, 0xEF},
}
err := str.AddStreamFrame(&frame1)
Expect(err).ToNot(HaveOccurred())
err = str.AddStreamFrame(&frame2)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 6)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
Expect(b).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00}))
})
It("assembles multiple StreamFrames", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2)).Times(2)
frame1 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD},
}
2018-01-03 19:19:49 +00:00
frame2 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 2,
Data: []byte{0xBE, 0xEF},
}
err := str.AddStreamFrame(&frame1)
Expect(err).ToNot(HaveOccurred())
err = str.AddStreamFrame(&frame2)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
Expect(b).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF}))
})
It("waits until data is available", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2))
2017-12-12 02:51:45 +00:00
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
frame := wire.StreamFrame{Data: []byte{0xDE, 0xAD}}
time.Sleep(10 * time.Millisecond)
2017-12-12 02:51:45 +00:00
err := str.AddStreamFrame(&frame)
Expect(err).ToNot(HaveOccurred())
}()
b := make([]byte, 2)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(2))
})
It("handles StreamFrames in wrong order", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2)).Times(2)
frame1 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 2,
Data: []byte{0xBE, 0xEF},
}
2018-01-03 19:19:49 +00:00
frame2 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD},
}
err := str.AddStreamFrame(&frame1)
Expect(err).ToNot(HaveOccurred())
err = str.AddStreamFrame(&frame2)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
Expect(b).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF}))
})
It("ignores duplicate StreamFrames", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2)).Times(2)
frame1 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD},
}
2018-01-03 19:19:49 +00:00
frame2 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0x13, 0x37},
}
2018-01-03 19:19:49 +00:00
frame3 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 2,
Data: []byte{0xBE, 0xEF},
}
err := str.AddStreamFrame(&frame1)
Expect(err).ToNot(HaveOccurred())
err = str.AddStreamFrame(&frame2)
Expect(err).ToNot(HaveOccurred())
err = str.AddStreamFrame(&frame3)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
Expect(b).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF}))
})
It("doesn't rejects a StreamFrames with an overlapping data range", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(6), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2))
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(4))
frame1 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte("foob"),
}
2018-01-03 19:19:49 +00:00
frame2 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 2,
Data: []byte("obar"),
}
err := str.AddStreamFrame(&frame1)
Expect(err).ToNot(HaveOccurred())
err = str.AddStreamFrame(&frame2)
2018-01-03 19:19:49 +00:00
Expect(err).ToNot(HaveOccurred())
2017-12-12 02:51:45 +00:00
b := make([]byte, 6)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
Expect(b).To(Equal([]byte("foobar")))
})
It("calls onData", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(4))
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
}
str.AddStreamFrame(&frame)
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
_, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(onDataCalled).To(BeTrue())
})
2018-01-03 19:19:49 +00:00
It("sets the read offset", func() {
str.SetReadOffset(0x42)
Expect(str.readOffset).To(Equal(protocol.ByteCount(0x42)))
Expect(str.frameQueue.readPosition).To(Equal(protocol.ByteCount(0x42)))
})
Context("deadlines", func() {
It("the deadline error has the right net.Error properties", func() {
Expect(errDeadline.Temporary()).To(BeTrue())
Expect(errDeadline.Timeout()).To(BeTrue())
})
It("returns an error when Read is called after the deadline", func() {
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(6), false).AnyTimes()
f := &wire.StreamFrame{Data: []byte("foobar")}
err := str.AddStreamFrame(f)
Expect(err).ToNot(HaveOccurred())
str.SetReadDeadline(time.Now().Add(-time.Second))
b := make([]byte, 6)
n, err := strWithTimeout.Read(b)
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
})
It("unblocks after the deadline", func() {
deadline := time.Now().Add(scaleDuration(50 * time.Millisecond))
str.SetReadDeadline(deadline)
b := make([]byte, 6)
n, err := strWithTimeout.Read(b)
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
Expect(time.Now()).To(BeTemporally("~", deadline, scaleDuration(10*time.Millisecond)))
})
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))
str.SetReadDeadline(deadline1)
go func() {
defer GinkgoRecover()
time.Sleep(scaleDuration(20 * time.Millisecond))
str.SetReadDeadline(deadline2)
// make sure that this was actually execute before the deadline expires
Expect(time.Now()).To(BeTemporally("<", deadline1))
}()
runtime.Gosched()
b := make([]byte, 10)
n, err := strWithTimeout.Read(b)
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
Expect(time.Now()).To(BeTemporally("~", deadline2, scaleDuration(20*time.Millisecond)))
})
It("unblocks earlier, when a new deadline is set", func() {
deadline1 := time.Now().Add(scaleDuration(200 * time.Millisecond))
deadline2 := time.Now().Add(scaleDuration(50 * time.Millisecond))
go func() {
defer GinkgoRecover()
time.Sleep(scaleDuration(10 * time.Millisecond))
str.SetReadDeadline(deadline2)
// make sure that this was actually execute before the deadline expires
Expect(time.Now()).To(BeTemporally("<", deadline2))
}()
str.SetReadDeadline(deadline1)
runtime.Gosched()
b := make([]byte, 10)
_, err := strWithTimeout.Read(b)
Expect(err).To(MatchError(errDeadline))
Expect(time.Now()).To(BeTemporally("~", deadline2, scaleDuration(25*time.Millisecond)))
})
It("sets a read deadline, when SetDeadline is called", func() {
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(6), false).AnyTimes()
f := &wire.StreamFrame{Data: []byte("foobar")}
err := str.AddStreamFrame(f)
Expect(err).ToNot(HaveOccurred())
str.SetDeadline(time.Now().Add(-time.Second))
b := make([]byte, 6)
n, err := strWithTimeout.Read(b)
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
})
})
2017-12-12 02:51:45 +00:00
Context("closing", func() {
Context("with FIN bit", func() {
It("returns EOFs", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), true)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(4))
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
FinBit: true,
}
str.AddStreamFrame(&frame)
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).To(MatchError(io.EOF))
Expect(n).To(Equal(4))
Expect(b).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF}))
2018-01-03 19:19:49 +00:00
n, err = strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(io.EOF))
})
It("handles out-of-order frames", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), true)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2)).Times(2)
frame1 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 2,
Data: []byte{0xBE, 0xEF},
FinBit: true,
}
2018-01-03 19:19:49 +00:00
frame2 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD},
}
err := str.AddStreamFrame(&frame1)
Expect(err).ToNot(HaveOccurred())
err = str.AddStreamFrame(&frame2)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).To(MatchError(io.EOF))
Expect(n).To(Equal(4))
Expect(b).To(Equal([]byte{0xDE, 0xAD, 0xBE, 0xEF}))
2018-01-03 19:19:49 +00:00
n, err = strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(io.EOF))
})
It("returns EOFs with partial read", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(2), true)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(2))
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD},
FinBit: true,
}
err := str.AddStreamFrame(&frame)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).To(MatchError(io.EOF))
Expect(n).To(Equal(2))
Expect(b[:n]).To(Equal([]byte{0xDE, 0xAD}))
})
It("handles immediate FINs", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(0))
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{},
FinBit: true,
}
err := str.AddStreamFrame(&frame)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(io.EOF))
})
})
Context("when CloseRemote is called", func() {
It("closes", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(0))
2017-12-12 02:51:45 +00:00
str.CloseRemote(0)
b := make([]byte, 8)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(io.EOF))
})
2018-01-03 19:19:49 +00:00
It("doesn't cancel the context", func() {
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
str.CloseRemote(0)
Expect(str.Context().Done()).ToNot(BeClosed())
})
2017-12-12 02:51:45 +00:00
})
})
Context("cancelling the stream", func() {
testErr := errors.New("test error")
It("immediately returns all reads", func() {
2018-01-03 19:19:49 +00:00
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
b := make([]byte, 4)
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
n, err := strWithTimeout.Read(b)
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
close(done)
2017-12-12 02:51:45 +00:00
}()
2018-01-03 19:19:49 +00:00
Consistently(done).ShouldNot(BeClosed())
2017-12-12 02:51:45 +00:00
str.Cancel(testErr)
2018-01-03 19:19:49 +00:00
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("errors for all following reads", func() {
str.Cancel(testErr)
b := make([]byte, 1)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
})
2018-01-03 19:19:49 +00:00
It("cancels the context", func() {
Expect(str.Context().Done()).ToNot(BeClosed())
str.Cancel(testErr)
Expect(str.Context().Done()).To(BeClosed())
})
2017-12-12 02:51:45 +00:00
})
})
Context("resetting", func() {
testErr := errors.New("testErr")
Context("reset by the peer", func() {
It("continues reading after receiving a remote error", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(10), true)
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
}
str.AddStreamFrame(&frame)
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 10)
2017-12-12 02:51:45 +00:00
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
})
It("reads a delayed StreamFrame that arrives after receiving a remote error", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), true)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
str.RegisterRemoteError(testErr, 4)
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
}
err := str.AddStreamFrame(&frame)
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(4))
})
It("returns the error if reading past the offset of the frame received", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(8), true)
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
}
str.AddStreamFrame(&frame)
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 8)
2017-12-12 02:51:45 +00:00
b := make([]byte, 10)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(b[0:4]).To(Equal(frame.Data))
Expect(err).To(MatchError(testErr))
Expect(n).To(Equal(4))
})
It("returns an EOF when reading past the offset, if the stream received a finbit", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), true)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(8), true)
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
FinBit: true,
}
str.AddStreamFrame(&frame)
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 8)
2017-12-12 02:51:45 +00:00
b := make([]byte, 10)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(b[:4]).To(Equal(frame.Data))
Expect(err).To(MatchError(io.EOF))
Expect(n).To(Equal(4))
})
It("continues reading in small chunks after receiving a remote error", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), true)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), true)
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
FinBit: true,
}
str.AddStreamFrame(&frame)
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 4)
2017-12-12 02:51:45 +00:00
b := make([]byte, 3)
2018-01-03 19:19:49 +00:00
_, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(b).To(Equal([]byte{0xde, 0xad, 0xbe}))
b = make([]byte, 3)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).To(MatchError(io.EOF))
Expect(b[:1]).To(Equal([]byte{0xef}))
Expect(n).To(Equal(1))
})
It("doesn't inform the flow controller about bytes read after receiving the remote error", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(4), false)
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(10), true)
// No AddBytesRead()
frame := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Offset: 0,
StreamID: 5,
Data: []byte{0xDE, 0xAD, 0xBE, 0xEF},
}
str.AddStreamFrame(&frame)
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 10)
2017-12-12 02:51:45 +00:00
b := make([]byte, 3)
2018-01-03 19:19:49 +00:00
_, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
})
It("stops writing after receiving a remote error", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(10), true)
done := make(chan struct{})
go func() {
defer GinkgoRecover()
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
close(done)
}()
str.RegisterRemoteError(testErr, 10)
Eventually(done).Should(BeClosed())
})
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
It("returns how much was written when recieving a remote error", func() {
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(10), true)
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999))
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(4))
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError(testErr))
Expect(n).To(Equal(4))
close(done)
2017-12-12 02:51:45 +00:00
}()
2018-01-03 19:19:49 +00:00
Eventually(func() []byte { data, _ := str.GetDataForWriting(4); return data }).ShouldNot(BeEmpty())
str.RegisterRemoteError(testErr, 10)
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("calls onReset when receiving a remote error", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
str.writeOffset = 0x1000
go func() {
2018-01-03 19:19:49 +00:00
_, _ = strWithTimeout.Write([]byte("foobar"))
close(done)
2017-12-12 02:51:45 +00:00
}()
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 0)
2017-12-12 02:51:45 +00:00
Expect(resetCalled).To(BeTrue())
Expect(resetCalledForStream).To(Equal(protocol.StreamID(1337)))
Expect(resetCalledAtOffset).To(Equal(protocol.ByteCount(0x1000)))
2018-01-03 19:19:49 +00:00
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("doesn't call onReset if it already sent a FIN", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
2017-12-12 02:51:45 +00:00
str.Close()
2018-01-03 19:19:49 +00:00
_, sentFin := str.GetDataForWriting(1000)
Expect(sentFin).To(BeTrue())
str.RegisterRemoteError(testErr, 0)
2017-12-12 02:51:45 +00:00
Expect(resetCalled).To(BeFalse())
})
It("doesn't call onReset if the stream was reset locally before", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
2017-12-12 02:51:45 +00:00
str.Reset(testErr)
Expect(resetCalled).To(BeTrue())
resetCalled = false
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 0)
2017-12-12 02:51:45 +00:00
Expect(resetCalled).To(BeFalse())
})
It("doesn't call onReset twice, when it gets two remote errors", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
str.RegisterRemoteError(testErr, 0)
2017-12-12 02:51:45 +00:00
Expect(resetCalled).To(BeTrue())
resetCalled = false
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 0)
2017-12-12 02:51:45 +00:00
Expect(resetCalled).To(BeFalse())
})
})
Context("reset locally", func() {
It("stops writing", func() {
2018-01-03 19:19:49 +00:00
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
close(done)
2017-12-12 02:51:45 +00:00
}()
2018-01-03 19:19:49 +00:00
Consistently(done).ShouldNot(BeClosed())
2017-12-12 02:51:45 +00:00
str.Reset(testErr)
2018-01-03 19:19:49 +00:00
Expect(str.GetDataForWriting(6)).To(BeNil())
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("doesn't allow further writes", func() {
str.Reset(testErr)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Write([]byte("foobar"))
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
2018-01-03 19:19:49 +00:00
Expect(str.GetDataForWriting(6)).To(BeNil())
2017-12-12 02:51:45 +00:00
})
It("stops reading", func() {
2018-01-03 19:19:49 +00:00
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
2017-12-12 02:51:45 +00:00
b := make([]byte, 4)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
close(done)
2017-12-12 02:51:45 +00:00
}()
2018-01-03 19:19:49 +00:00
Consistently(done).ShouldNot(BeClosed())
2017-12-12 02:51:45 +00:00
str.Reset(testErr)
2018-01-03 19:19:49 +00:00
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("doesn't allow further reads", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(6), false)
str.AddStreamFrame(&wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Data: []byte("foobar"),
})
str.Reset(testErr)
b := make([]byte, 6)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
})
It("calls onReset", func() {
str.writeOffset = 0x1000
str.Reset(testErr)
Expect(resetCalled).To(BeTrue())
Expect(resetCalledForStream).To(Equal(protocol.StreamID(1337)))
Expect(resetCalledAtOffset).To(Equal(protocol.ByteCount(0x1000)))
})
It("doesn't call onReset if it already sent a FIN", func() {
str.Close()
2018-01-03 19:19:49 +00:00
_, sentFin := str.GetDataForWriting(1000)
Expect(sentFin).To(BeTrue())
2017-12-12 02:51:45 +00:00
str.Reset(testErr)
Expect(resetCalled).To(BeFalse())
})
It("doesn't call onReset if the stream was reset remotely before", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
str.RegisterRemoteError(testErr, 0)
2017-12-12 02:51:45 +00:00
Expect(resetCalled).To(BeTrue())
resetCalled = false
str.Reset(testErr)
Expect(resetCalled).To(BeFalse())
})
It("doesn't call onReset twice", func() {
str.Reset(testErr)
Expect(resetCalled).To(BeTrue())
resetCalled = false
str.Reset(testErr)
Expect(resetCalled).To(BeFalse())
})
2018-01-03 19:19:49 +00:00
It("cancels the context", func() {
Expect(str.Context().Done()).ToNot(BeClosed())
str.Reset(testErr)
Expect(str.Context().Done()).To(BeClosed())
})
2017-12-12 02:51:45 +00:00
})
})
Context("writing", func() {
2018-01-03 19:19:49 +00:00
It("writes and gets all data at once", func() {
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999))
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(6))
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
n, err := strWithTimeout.Write([]byte("foobar"))
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
close(done)
}()
Eventually(func() []byte {
str.mutex.Lock()
defer str.mutex.Unlock()
return str.dataForWriting
}).Should(Equal([]byte("foobar")))
2018-01-03 19:19:49 +00:00
Consistently(done).ShouldNot(BeClosed())
2017-12-12 02:51:45 +00:00
Expect(onDataCalled).To(BeTrue())
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeTrue())
data, sendFin := str.GetDataForWriting(1000)
2017-12-12 02:51:45 +00:00
Expect(data).To(Equal([]byte("foobar")))
2018-01-03 19:19:49 +00:00
Expect(sendFin).To(BeFalse())
2017-12-12 02:51:45 +00:00
Expect(str.writeOffset).To(Equal(protocol.ByteCount(6)))
Expect(str.dataForWriting).To(BeNil())
2018-01-03 19:19:49 +00:00
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("writes and gets data in two turns", func() {
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999)).Times(2)
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(3)).Times(2)
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
n, err := strWithTimeout.Write([]byte("foobar"))
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(6))
close(done)
}()
Eventually(func() []byte {
str.mutex.Lock()
defer str.mutex.Unlock()
return str.dataForWriting
}).Should(Equal([]byte("foobar")))
2018-01-03 19:19:49 +00:00
Consistently(done).ShouldNot(BeClosed())
Expect(str.HasDataForWriting()).To(BeTrue())
data, sendFin := str.GetDataForWriting(3)
2017-12-12 02:51:45 +00:00
Expect(data).To(Equal([]byte("foo")))
2018-01-03 19:19:49 +00:00
Expect(sendFin).To(BeFalse())
2017-12-12 02:51:45 +00:00
Expect(str.writeOffset).To(Equal(protocol.ByteCount(3)))
Expect(str.dataForWriting).ToNot(BeNil())
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeTrue())
data, sendFin = str.GetDataForWriting(3)
2017-12-12 02:51:45 +00:00
Expect(data).To(Equal([]byte("bar")))
2018-01-03 19:19:49 +00:00
Expect(sendFin).To(BeFalse())
2017-12-12 02:51:45 +00:00
Expect(str.writeOffset).To(Equal(protocol.ByteCount(6)))
Expect(str.dataForWriting).To(BeNil())
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeFalse())
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("getDataForWriting returns nil if no data is available", func() {
2018-01-03 19:19:49 +00:00
Expect(str.GetDataForWriting(1000)).To(BeNil())
2017-12-12 02:51:45 +00:00
})
It("copies the slice while writing", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999))
mockFC.EXPECT().AddBytesSent(protocol.ByteCount(3))
2017-12-12 02:51:45 +00:00
s := []byte("foo")
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
n, err := strWithTimeout.Write(s)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(3))
}()
2018-01-03 19:19:49 +00:00
Eventually(func() bool { return str.HasDataForWriting() }).Should(BeTrue())
2017-12-12 02:51:45 +00:00
s[0] = 'v'
2018-01-03 19:19:49 +00:00
Expect(str.GetDataForWriting(3)).To(Equal([]byte("foo")))
2017-12-12 02:51:45 +00:00
})
It("returns when given a nil input", func() {
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Write(nil)
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).ToNot(HaveOccurred())
})
It("returns when given an empty slice", func() {
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Write([]byte(""))
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).ToNot(HaveOccurred())
})
2018-01-03 19:19:49 +00:00
Context("deadlines", func() {
It("returns an error when Write is called after the deadline", func() {
str.SetWriteDeadline(time.Now().Add(-time.Second))
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
})
It("unblocks after the deadline", func() {
deadline := time.Now().Add(scaleDuration(50 * time.Millisecond))
str.SetWriteDeadline(deadline)
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
Expect(time.Now()).To(BeTemporally("~", deadline, scaleDuration(20*time.Millisecond)))
})
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))
str.SetWriteDeadline(deadline1)
go func() {
defer GinkgoRecover()
time.Sleep(scaleDuration(20 * time.Millisecond))
str.SetWriteDeadline(deadline2)
// make sure that this was actually execute before the deadline expires
Expect(time.Now()).To(BeTemporally("<", deadline1))
}()
runtime.Gosched()
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
Expect(time.Now()).To(BeTemporally("~", deadline2, scaleDuration(20*time.Millisecond)))
})
It("unblocks earlier, when a new deadline is set", func() {
deadline1 := time.Now().Add(scaleDuration(200 * time.Millisecond))
deadline2 := time.Now().Add(scaleDuration(50 * time.Millisecond))
go func() {
defer GinkgoRecover()
time.Sleep(scaleDuration(10 * time.Millisecond))
str.SetWriteDeadline(deadline2)
// make sure that this was actually execute before the deadline expires
Expect(time.Now()).To(BeTemporally("<", deadline2))
}()
str.SetWriteDeadline(deadline1)
runtime.Gosched()
_, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError(errDeadline))
Expect(time.Now()).To(BeTemporally("~", deadline2, scaleDuration(20*time.Millisecond)))
})
It("sets a read deadline, when SetDeadline is called", func() {
str.SetDeadline(time.Now().Add(-time.Second))
n, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError(errDeadline))
Expect(n).To(BeZero())
})
})
2017-12-12 02:51:45 +00:00
Context("closing", func() {
2018-01-03 19:19:49 +00:00
It("doesn't allow writes after it has been closed", func() {
2017-12-12 02:51:45 +00:00
str.Close()
2018-01-03 19:19:49 +00:00
_, err := strWithTimeout.Write([]byte("foobar"))
Expect(err).To(MatchError("write on closed stream 1337"))
2017-12-12 02:51:45 +00:00
})
It("allows FIN", func() {
str.Close()
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeTrue())
data, sendFin := str.GetDataForWriting(1000)
Expect(data).To(BeEmpty())
Expect(sendFin).To(BeTrue())
2017-12-12 02:51:45 +00:00
})
It("does not allow FIN when there's still data", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().SendWindowSize().Return(protocol.ByteCount(9999)).Times(2)
mockFC.EXPECT().AddBytesSent(gomock.Any()).Times(2)
2017-12-12 02:51:45 +00:00
str.dataForWriting = []byte("foobar")
str.Close()
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeTrue())
data, sendFin := str.GetDataForWriting(3)
Expect(data).To(Equal([]byte("foo")))
Expect(sendFin).To(BeFalse())
data, sendFin = str.GetDataForWriting(3)
Expect(data).To(Equal([]byte("bar")))
Expect(sendFin).To(BeTrue())
2017-12-12 02:51:45 +00:00
})
It("does not allow FIN when the stream is not closed", func() {
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeFalse())
_, sendFin := str.GetDataForWriting(3)
Expect(sendFin).To(BeFalse())
2017-12-12 02:51:45 +00:00
})
It("does not allow FIN after an error", func() {
str.Cancel(errors.New("test"))
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeFalse())
data, sendFin := str.GetDataForWriting(1000)
Expect(data).To(BeEmpty())
Expect(sendFin).To(BeFalse())
2017-12-12 02:51:45 +00:00
})
It("does not allow FIN twice", func() {
str.Close()
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeTrue())
data, sendFin := str.GetDataForWriting(1000)
Expect(data).To(BeEmpty())
Expect(sendFin).To(BeTrue())
Expect(str.HasDataForWriting()).To(BeFalse())
data, sendFin = str.GetDataForWriting(1000)
Expect(data).To(BeEmpty())
Expect(sendFin).To(BeFalse())
2017-12-12 02:51:45 +00:00
})
})
Context("cancelling", func() {
testErr := errors.New("test")
It("returns errors when the stream is cancelled", func() {
str.Cancel(testErr)
2018-01-03 19:19:49 +00:00
n, err := strWithTimeout.Write([]byte("foo"))
2017-12-12 02:51:45 +00:00
Expect(n).To(BeZero())
Expect(err).To(MatchError(testErr))
})
It("doesn't get data for writing if an error occurred", func() {
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
_, err := strWithTimeout.Write([]byte("foobar"))
2017-12-12 02:51:45 +00:00
Expect(err).To(MatchError(testErr))
}()
Eventually(func() []byte { return str.dataForWriting }).ShouldNot(BeNil())
2018-01-03 19:19:49 +00:00
Expect(str.HasDataForWriting()).To(BeTrue())
2017-12-12 02:51:45 +00:00
str.Cancel(testErr)
2018-01-03 19:19:49 +00:00
data, sendFin := str.GetDataForWriting(6)
2017-12-12 02:51:45 +00:00
Expect(data).To(BeNil())
2018-01-03 19:19:49 +00:00
Expect(sendFin).To(BeFalse())
Expect(str.HasDataForWriting()).To(BeFalse())
2017-12-12 02:51:45 +00:00
})
})
})
2018-01-03 19:19:49 +00:00
It("errors when a StreamFrames causes a flow control violation", func() {
testErr := errors.New("flow control violation")
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(8), false).Return(testErr)
frame := wire.StreamFrame{
Offset: 2,
Data: []byte("foobar"),
}
err := str.AddStreamFrame(&frame)
Expect(err).To(MatchError(testErr))
2017-12-12 02:51:45 +00:00
})
Context("closing", func() {
testErr := errors.New("testErr")
finishReading := func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
err := str.AddStreamFrame(&wire.StreamFrame{FinBit: true})
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
b := make([]byte, 100)
2018-01-03 19:19:49 +00:00
_, err = strWithTimeout.Read(b)
2017-12-12 02:51:45 +00:00
Expect(err).To(MatchError(io.EOF))
}
It("is finished after it is canceled", func() {
str.Cancel(testErr)
2018-01-03 19:19:49 +00:00
Expect(str.Finished()).To(BeTrue())
2017-12-12 02:51:45 +00:00
})
It("is not finished if it is only closed for writing", func() {
str.Close()
2018-01-03 19:19:49 +00:00
_, sentFin := str.GetDataForWriting(1000)
Expect(sentFin).To(BeTrue())
Expect(str.Finished()).To(BeFalse())
})
It("cancels the context after it is closed", func() {
Expect(str.Context().Done()).ToNot(BeClosed())
str.Close()
Expect(str.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("is not finished if it is only closed for reading", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(0))
2017-12-12 02:51:45 +00:00
finishReading()
2018-01-03 19:19:49 +00:00
Expect(str.Finished()).To(BeFalse())
2017-12-12 02:51:45 +00:00
})
It("is finished after receiving a RST and sending one", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
2017-12-12 02:51:45 +00:00
// this directly sends a rst
2018-01-03 19:19:49 +00:00
str.RegisterRemoteError(testErr, 0)
2017-12-12 02:51:45 +00:00
Expect(str.rstSent.Get()).To(BeTrue())
2018-01-03 19:19:49 +00:00
Expect(str.Finished()).To(BeTrue())
})
It("cancels the context after receiving a RST", func() {
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(0), true)
Expect(str.Context().Done()).ToNot(BeClosed())
str.RegisterRemoteError(testErr, 0)
Expect(str.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("is finished after being locally reset and receiving a RST in response", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(13), true)
2017-12-12 02:51:45 +00:00
str.Reset(testErr)
2018-01-03 19:19:49 +00:00
Expect(str.Finished()).To(BeFalse())
str.RegisterRemoteError(testErr, 13)
Expect(str.Finished()).To(BeTrue())
2017-12-12 02:51:45 +00:00
})
It("is finished after finishing writing and receiving a RST", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().UpdateHighestReceived(protocol.ByteCount(13), true)
2017-12-12 02:51:45 +00:00
str.Close()
2018-01-03 19:19:49 +00:00
_, sentFin := str.GetDataForWriting(1000)
Expect(sentFin).To(BeTrue())
str.RegisterRemoteError(testErr, 13)
Expect(str.Finished()).To(BeTrue())
2017-12-12 02:51:45 +00:00
})
It("is finished after finishing reading and being locally reset", func() {
2018-01-03 19:19:49 +00:00
mockFC.EXPECT().AddBytesRead(protocol.ByteCount(0))
2017-12-12 02:51:45 +00:00
finishReading()
2018-01-03 19:19:49 +00:00
Expect(str.Finished()).To(BeFalse())
2017-12-12 02:51:45 +00:00
str.Reset(testErr)
2018-01-03 19:19:49 +00:00
Expect(str.Finished()).To(BeTrue())
2017-12-12 02:51:45 +00:00
})
})
2018-01-03 19:19:49 +00:00
Context("flow control", func() {
It("says when it's flow control blocked", func() {
mockFC.EXPECT().IsBlocked().Return(false)
Expect(str.IsFlowControlBlocked()).To(BeFalse())
mockFC.EXPECT().IsBlocked().Return(true)
Expect(str.IsFlowControlBlocked()).To(BeTrue())
})
It("updates the flow control window", func() {
mockFC.EXPECT().UpdateSendWindow(protocol.ByteCount(0x42))
str.UpdateSendWindow(0x42)
})
It("gets a window update", func() {
mockFC.EXPECT().GetWindowUpdate().Return(protocol.ByteCount(0x100))
Expect(str.GetWindowUpdate()).To(Equal(protocol.ByteCount(0x100)))
})
})
2017-12-12 02:51:45 +00:00
})