use the new stream maps for the IETF QUIC streams map

This commit is contained in:
Marten Seemann 2018-02-03 20:32:17 +08:00
parent be6493eff8
commit 02949853f7
7 changed files with 420 additions and 635 deletions

View file

@ -96,19 +96,6 @@ func (mr *MockStreamManagerMockRecorder) GetOrOpenSendStream(arg0 interface{}) *
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrOpenSendStream", reflect.TypeOf((*MockStreamManager)(nil).GetOrOpenSendStream), arg0)
}
// GetOrOpenStream mocks base method
func (m *MockStreamManager) GetOrOpenStream(arg0 protocol.StreamID) (streamI, error) {
ret := m.ctrl.Call(m, "GetOrOpenStream", arg0)
ret0, _ := ret[0].(streamI)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetOrOpenStream indicates an expected call of GetOrOpenStream
func (mr *MockStreamManagerMockRecorder) GetOrOpenStream(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrOpenStream", reflect.TypeOf((*MockStreamManager)(nil).GetOrOpenStream), arg0)
}
// OpenStream mocks base method
func (m *MockStreamManager) OpenStream() (Stream, error) {
ret := m.ctrl.Call(m, "OpenStream")

View file

@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"sync"
"time"
@ -29,7 +30,6 @@ type streamGetter interface {
}
type streamManager interface {
GetOrOpenStream(protocol.StreamID) (streamI, error)
GetOrOpenSendStream(protocol.StreamID) (sendStreamI, error)
GetOrOpenReceiveStream(protocol.StreamID) (receiveStreamI, error)
OpenStream() (Stream, error)
@ -325,7 +325,7 @@ func (s *session) postSetup(initialPacketNumber protocol.PacketNumber) error {
s.receivedPacketHandler = ackhandler.NewReceivedPacketHandler(s.version)
if s.version.UsesTLS() {
s.streamsMap = newStreamsMap(s.newStream, s.perspective)
s.streamsMap = newStreamsMap(s, s.newFlowController, s.perspective, s.version)
} else {
s.streamsMap = newStreamsMapLegacy(s.newStream, s.perspective)
}
@ -892,14 +892,18 @@ func (s *session) logPacket(packet *packedPacket) {
}
// GetOrOpenStream either returns an existing stream, a newly opened stream, or nil if a stream with the provided ID is already closed.
// Newly opened streams should only originate from the client. To open a stream from the server, OpenStream should be used.
// It is *only* needed for gQUIC's H2.
// It will be removed as soon as gQUIC moves towards the IETF H2/QUIC stream mapping.
func (s *session) GetOrOpenStream(id protocol.StreamID) (Stream, error) {
str, err := s.streamsMap.GetOrOpenStream(id)
str, err := s.streamsMap.GetOrOpenSendStream(id)
if str != nil {
return str, err
if bstr, ok := str.(Stream); ok {
return bstr, err
}
return nil, fmt.Errorf("Stream %d is not a bidirectional stream", id)
}
// make sure to return an actual nil value here, not an Stream with value nil
return str, err
return nil, err
}
// AcceptStream returns the next stream openend by the peer
@ -917,11 +921,16 @@ func (s *session) OpenStreamSync() (Stream, error) {
}
func (s *session) newStream(id protocol.StreamID) streamI {
flowController := s.newFlowController(id)
return newStream(id, s, flowController, s.version)
}
func (s *session) newFlowController(id protocol.StreamID) flowcontrol.StreamFlowController {
var initialSendWindow protocol.ByteCount
if s.peerParams != nil {
initialSendWindow = s.peerParams.StreamFlowControlWindow
}
flowController := flowcontrol.NewStreamFlowController(
return flowcontrol.NewStreamFlowController(
id,
s.version.StreamContributesToConnectionFlowControl(id),
s.connFlowController,
@ -930,7 +939,6 @@ func (s *session) newStream(id protocol.StreamID) streamI {
initialSendWindow,
s.rttStats,
)
return newStream(id, s, flowController, s.version)
}
func (s *session) newCryptoStream() cryptoStreamI {

View file

@ -1447,7 +1447,7 @@ var _ = Describe("Session", func() {
Context("getting streams", func() {
It("returns a new stream", func() {
mstr := NewMockStreamI(mockCtrl)
streamManager.EXPECT().GetOrOpenStream(protocol.StreamID(11)).Return(mstr, nil)
streamManager.EXPECT().GetOrOpenSendStream(protocol.StreamID(11)).Return(mstr, nil)
str, err := sess.GetOrOpenStream(11)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(Equal(mstr))
@ -1455,7 +1455,7 @@ var _ = Describe("Session", func() {
It("returns a nil-value (not an interface with value nil) for closed streams", func() {
strI := Stream(nil)
streamManager.EXPECT().GetOrOpenStream(protocol.StreamID(1337)).Return(strI, nil)
streamManager.EXPECT().GetOrOpenSendStream(protocol.StreamID(1337)).Return(strI, nil)
str, err := sess.GetOrOpenStream(1337)
Expect(err).ToNot(HaveOccurred())
// make sure that the returned value is a plain nil, not an Stream with value nil
@ -1463,6 +1463,12 @@ var _ = Describe("Session", func() {
Expect(ok).To(BeFalse())
})
It("errors when trying to get a unidirectional stream", func() {
streamManager.EXPECT().GetOrOpenSendStream(protocol.StreamID(100)).Return(&sendStream{}, nil)
_, err := sess.GetOrOpenStream(100)
Expect(err).To(MatchError("Stream 100 is not a bidirectional stream"))
})
// all relevant tests for this are in the streamsMap
It("opens streams synchronously", func() {
mstr := NewMockStreamI(mockCtrl)

View file

@ -1,234 +1,182 @@
package quic
import (
"errors"
"fmt"
"sync"
"github.com/lucas-clemente/quic-go/internal/flowcontrol"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/wire"
"github.com/lucas-clemente/quic-go/qerr"
)
type streamType int
const (
streamTypeOutgoingBidi streamType = iota
streamTypeIncomingBidi
streamTypeOutgoingUni
streamTypeIncomingUni
)
type streamsMap struct {
mutex sync.RWMutex
perspective protocol.Perspective
streams map[protocol.StreamID]streamI
sender streamSender
newFlowController func(protocol.StreamID) flowcontrol.StreamFlowController
nextStreamToOpen protocol.StreamID // StreamID of the next Stream that will be returned by OpenStream()
highestStreamOpenedByPeer protocol.StreamID
nextStreamOrErrCond sync.Cond
openStreamOrErrCond sync.Cond
closeErr error
nextStreamToAccept protocol.StreamID
newStream newStreamLambda
outgoingBidiStreams *outgoingBidiStreamsMap
outgoingUniStreams *outgoingUniStreamsMap
incomingBidiStreams *incomingBidiStreamsMap
incomingUniStreams *incomingUniStreamsMap
}
var _ streamManager = &streamsMap{}
type newStreamLambda func(protocol.StreamID) streamI
var errMapAccess = errors.New("streamsMap: Error accessing the streams map")
func newStreamsMap(newStream newStreamLambda, pers protocol.Perspective) streamManager {
sm := streamsMap{
perspective: pers,
streams: make(map[protocol.StreamID]streamI),
newStream: newStream,
func newStreamsMap(
sender streamSender,
newFlowController func(protocol.StreamID) flowcontrol.StreamFlowController,
perspective protocol.Perspective,
version protocol.VersionNumber,
) streamManager {
m := &streamsMap{
perspective: perspective,
newFlowController: newFlowController,
sender: sender,
}
sm.nextStreamOrErrCond.L = &sm.mutex
sm.openStreamOrErrCond.L = &sm.mutex
nextClientInitiatedStream := protocol.StreamID(1)
nextServerInitiatedStream := protocol.StreamID(2)
if pers == protocol.PerspectiveServer {
sm.nextStreamToOpen = nextServerInitiatedStream
sm.nextStreamToAccept = nextClientInitiatedStream
var firstOutgoingBidiStream, firstOutgoingUniStream, firstIncomingBidiStream, firstIncomingUniStream protocol.StreamID
if perspective == protocol.PerspectiveServer {
firstOutgoingBidiStream = 1
firstIncomingBidiStream = 4 // the crypto stream is handled separatedly
firstOutgoingUniStream = 3
firstIncomingUniStream = 2
} else {
sm.nextStreamToOpen = nextClientInitiatedStream
sm.nextStreamToAccept = nextServerInitiatedStream
firstOutgoingBidiStream = 4 // the crypto stream is handled separately
firstIncomingBidiStream = 1
firstOutgoingUniStream = 2
firstIncomingUniStream = 3
}
return &sm
newBidiStream := func(id protocol.StreamID) streamI {
return newStream(id, m.sender, m.newFlowController(id), version)
}
newUniSendStream := func(id protocol.StreamID) sendStreamI {
return newSendStream(id, m.sender, m.newFlowController(id), version)
}
newUniReceiveStream := func(id protocol.StreamID) receiveStreamI {
return newReceiveStream(id, m.sender, m.newFlowController(id), version)
}
m.outgoingBidiStreams = newOutgoingBidiStreamsMap(firstOutgoingBidiStream, newBidiStream)
m.incomingBidiStreams = newIncomingBidiStreamsMap(firstIncomingBidiStream, newBidiStream)
m.outgoingUniStreams = newOutgoingUniStreamsMap(firstOutgoingUniStream, newUniSendStream)
m.incomingUniStreams = newIncomingUniStreamsMap(firstIncomingUniStream, newUniReceiveStream)
return m
}
// getStreamPerspective says which side should initiate a stream
func (m *streamsMap) streamInitiatedBy(id protocol.StreamID) protocol.Perspective {
if id%2 == 0 {
return protocol.PerspectiveServer
}
return protocol.PerspectiveClient
}
func (m *streamsMap) nextStreamID(id protocol.StreamID) protocol.StreamID {
if m.perspective == protocol.PerspectiveServer && id == 0 {
return 1
}
return id + 2
}
func (m *streamsMap) GetOrOpenReceiveStream(id protocol.StreamID) (receiveStreamI, error) {
// every bidirectional stream is also a receive stream
return m.GetOrOpenStream(id)
}
func (m *streamsMap) GetOrOpenSendStream(id protocol.StreamID) (sendStreamI, error) {
// every bidirectional stream is also a send stream
return m.GetOrOpenStream(id)
}
// GetOrOpenStream either returns an existing stream, a newly opened stream, or nil if a stream with the provided ID is already closed.
// Newly opened streams should only originate from the client. To open a stream from the server, OpenStream should be used.
func (m *streamsMap) GetOrOpenStream(id protocol.StreamID) (streamI, error) {
m.mutex.RLock()
s, ok := m.streams[id]
m.mutex.RUnlock()
if ok {
return s, nil
}
// ... we don't have an existing stream
m.mutex.Lock()
defer m.mutex.Unlock()
// We need to check whether another invocation has already created a stream (between RUnlock() and Lock()).
s, ok = m.streams[id]
if ok {
return s, nil
}
if m.perspective == m.streamInitiatedBy(id) {
if id <= m.nextStreamToOpen { // this is a stream opened by us. Must have been closed already
return nil, nil
func (m *streamsMap) getStreamType(id protocol.StreamID) streamType {
if m.perspective == protocol.PerspectiveServer {
switch id % 4 {
case 0:
return streamTypeIncomingBidi
case 1:
return streamTypeOutgoingBidi
case 2:
return streamTypeIncomingUni
case 3:
return streamTypeOutgoingUni
}
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))
}
if id <= m.highestStreamOpenedByPeer { // this is a peer-initiated stream that doesn't exist anymore. Must have been closed already
return nil, nil
}
for sid := m.nextStreamID(m.highestStreamOpenedByPeer); sid <= id; sid = m.nextStreamID(sid) {
if _, err := m.openRemoteStream(sid); err != nil {
return nil, err
} else {
switch id % 4 {
case 0:
return streamTypeOutgoingBidi
case 1:
return streamTypeIncomingBidi
case 2:
return streamTypeOutgoingUni
case 3:
return streamTypeIncomingUni
}
}
m.nextStreamOrErrCond.Broadcast()
return m.streams[id], nil
panic("")
}
func (m *streamsMap) openRemoteStream(id protocol.StreamID) (streamI, error) {
if id+protocol.MaxNewStreamIDDelta < m.highestStreamOpenedByPeer {
return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("attempted to open stream %d, which is a lot smaller than the highest opened stream, %d", id, m.highestStreamOpenedByPeer))
}
if id > m.highestStreamOpenedByPeer {
m.highestStreamOpenedByPeer = id
}
s := m.newStream(id)
return s, m.putStream(s)
}
func (m *streamsMap) openStreamImpl() (streamI, error) {
s := m.newStream(m.nextStreamToOpen)
m.nextStreamToOpen = m.nextStreamID(m.nextStreamToOpen)
return s, m.putStream(s)
}
// OpenStream opens the next available stream
func (m *streamsMap) OpenStream() (Stream, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.closeErr != nil {
return nil, m.closeErr
}
return m.openStreamImpl()
return m.outgoingBidiStreams.OpenStream()
}
func (m *streamsMap) OpenStreamSync() (Stream, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
for {
if m.closeErr != nil {
return nil, m.closeErr
}
str, err := m.openStreamImpl()
if err == nil {
return str, err
}
if err != nil && err != qerr.TooManyOpenStreams {
return nil, err
}
m.openStreamOrErrCond.Wait()
}
// TODO(#1150): implement limits
return m.OpenStream()
}
func (m *streamsMap) OpenUniStream() (SendStream, error) {
return m.outgoingUniStreams.OpenStream()
}
func (m *streamsMap) OpenUniStreamSync() (SendStream, error) {
// TODO(#1150): implement limits
return m.outgoingUniStreams.OpenStream()
}
// AcceptStream returns the next stream opened by the peer
// it blocks until a new stream is opened
func (m *streamsMap) AcceptStream() (Stream, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
var str streamI
for {
var ok bool
if m.closeErr != nil {
return nil, m.closeErr
}
str, ok = m.streams[m.nextStreamToAccept]
if ok {
break
}
m.nextStreamOrErrCond.Wait()
}
m.nextStreamToAccept += 2
return str, nil
return m.incomingBidiStreams.AcceptStream()
}
func (m *streamsMap) AcceptUniStream() (ReceiveStream, error) {
return m.incomingUniStreams.AcceptStream()
}
func (m *streamsMap) DeleteStream(id protocol.StreamID) error {
m.mutex.Lock()
defer m.mutex.Unlock()
_, ok := m.streams[id]
if !ok {
return errMapAccess
switch m.getStreamType(id) {
case streamTypeIncomingBidi:
return m.incomingBidiStreams.DeleteStream(id)
case streamTypeOutgoingBidi:
return m.outgoingBidiStreams.DeleteStream(id)
case streamTypeIncomingUni:
return m.incomingUniStreams.DeleteStream(id)
case streamTypeOutgoingUni:
return m.outgoingUniStreams.DeleteStream(id)
default:
panic("invalid stream type")
}
delete(m.streams, id)
m.openStreamOrErrCond.Signal()
return nil
}
func (m *streamsMap) putStream(s streamI) error {
id := s.StreamID()
if _, ok := m.streams[id]; ok {
return fmt.Errorf("a stream with ID %d already exists", id)
func (m *streamsMap) GetOrOpenReceiveStream(id protocol.StreamID) (receiveStreamI, error) {
switch m.getStreamType(id) {
case streamTypeOutgoingBidi:
return m.outgoingBidiStreams.GetStream(id)
case streamTypeIncomingBidi:
return m.incomingBidiStreams.GetOrOpenStream(id)
case streamTypeIncomingUni:
return m.incomingUniStreams.GetOrOpenStream(id)
case streamTypeOutgoingUni:
// an outgoing unidirectional stream is a send stream, not a receive stream
return nil, fmt.Errorf("peer attempted to open receive stream %d", id)
default:
panic("invalid stream type")
}
m.streams[id] = s
return nil
}
func (m *streamsMap) GetOrOpenSendStream(id protocol.StreamID) (sendStreamI, error) {
switch m.getStreamType(id) {
case streamTypeOutgoingBidi:
return m.outgoingBidiStreams.GetStream(id)
case streamTypeIncomingBidi:
return m.incomingBidiStreams.GetOrOpenStream(id)
case streamTypeOutgoingUni:
return m.outgoingUniStreams.GetStream(id)
case streamTypeIncomingUni:
// an incoming unidirectional stream is a receive stream, not a send stream
return nil, fmt.Errorf("peer attempted to open send stream %d", id)
default:
panic("invalid stream type")
}
}
func (m *streamsMap) UpdateLimits(*handshake.TransportParameters) {
// TODO(#1150): implement limits
}
func (m *streamsMap) CloseWithError(err error) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.closeErr = err
m.nextStreamOrErrCond.Broadcast()
m.openStreamOrErrCond.Broadcast()
for _, s := range m.streams {
s.closeForShutdown(err)
}
}
// TODO(#952): this won't be needed when gQUIC supports stateless handshakes
func (m *streamsMap) UpdateLimits(params *handshake.TransportParameters) {
m.mutex.Lock()
for id, str := range m.streams {
str.handleMaxStreamDataFrame(&wire.MaxStreamDataFrame{
StreamID: id,
ByteOffset: params.StreamFlowControlWindow,
})
}
m.mutex.Unlock()
m.openStreamOrErrCond.Broadcast()
m.outgoingBidiStreams.CloseWithError(err)
m.outgoingUniStreams.CloseWithError(err)
m.incomingBidiStreams.CloseWithError(err)
m.incomingUniStreams.CloseWithError(err)
}

View file

@ -1,6 +1,7 @@
package quic
import (
"errors"
"fmt"
"sync"
@ -26,7 +27,7 @@ type streamsMapLegacy struct {
closeErr error
nextStreamToAccept protocol.StreamID
newStream newStreamLambda
newStream func(protocol.StreamID) streamI
numOutgoingStreams uint32
numIncomingStreams uint32
@ -36,7 +37,9 @@ type streamsMapLegacy struct {
var _ streamManager = &streamsMapLegacy{}
func newStreamsMapLegacy(newStream newStreamLambda, pers protocol.Perspective) streamManager {
var errMapAccess = errors.New("streamsMap: Error accessing the streams map")
func newStreamsMapLegacy(newStream func(protocol.StreamID) streamI, pers protocol.Perspective) streamManager {
// add some tolerance to the maximum incoming streams value
maxStreams := uint32(protocol.MaxIncomingStreams)
maxIncomingStreams := utils.MaxUint32(
@ -77,17 +80,17 @@ func (m *streamsMapLegacy) streamInitiatedBy(id protocol.StreamID) protocol.Pers
func (m *streamsMapLegacy) GetOrOpenReceiveStream(id protocol.StreamID) (receiveStreamI, error) {
// every bidirectional stream is also a receive stream
return m.GetOrOpenStream(id)
return m.getOrOpenStream(id)
}
func (m *streamsMapLegacy) GetOrOpenSendStream(id protocol.StreamID) (sendStreamI, error) {
// every bidirectional stream is also a send stream
return m.GetOrOpenStream(id)
return m.getOrOpenStream(id)
}
// GetOrOpenStream either returns an existing stream, a newly opened stream, or nil if a stream with the provided ID is already closed.
// getOrOpenStream either returns an existing stream, a newly opened stream, or nil if a stream with the provided ID is already closed.
// Newly opened streams should only originate from the client. To open a stream from the server, OpenStream should be used.
func (m *streamsMapLegacy) GetOrOpenStream(id protocol.StreamID) (streamI, error) {
func (m *streamsMapLegacy) getOrOpenStream(id protocol.StreamID) (streamI, error) {
m.mutex.RLock()
s, ok := m.streams[id]
m.mutex.RUnlock()

View file

@ -38,7 +38,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Context("client-side streams", func() {
It("gets new streams", func() {
s, err := m.GetOrOpenStream(3)
s, err := m.getOrOpenStream(3)
Expect(err).NotTo(HaveOccurred())
Expect(s).ToNot(BeNil())
Expect(s.StreamID()).To(Equal(protocol.StreamID(3)))
@ -48,38 +48,38 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
})
It("rejects streams with even IDs", func() {
_, err := m.GetOrOpenStream(6)
_, err := m.getOrOpenStream(6)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 6"))
})
It("rejects streams with even IDs, which are lower thatn the highest client-side stream", func() {
_, err := m.GetOrOpenStream(5)
_, err := m.getOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
_, err = m.GetOrOpenStream(4)
_, err = m.getOrOpenStream(4)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 4"))
})
It("gets existing streams", func() {
s, err := m.GetOrOpenStream(5)
s, err := m.getOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
numStreams := m.numIncomingStreams
s, err = m.GetOrOpenStream(5)
s, err = m.getOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
Expect(s.StreamID()).To(Equal(protocol.StreamID(5)))
Expect(m.numIncomingStreams).To(Equal(numStreams))
})
It("returns nil for closed streams", func() {
_, err := m.GetOrOpenStream(5)
_, err := m.getOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
deleteStream(5)
s, err := m.GetOrOpenStream(5)
s, err := m.getOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
Expect(s).To(BeNil())
})
It("opens skipped streams", func() {
_, err := m.GetOrOpenStream(7)
_, err := m.getOrOpenStream(7)
Expect(err).NotTo(HaveOccurred())
Expect(m.streams).To(HaveKey(protocol.StreamID(3)))
Expect(m.streams).To(HaveKey(protocol.StreamID(5)))
@ -87,11 +87,11 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
})
It("doesn't reopen an already closed stream", func() {
_, err := m.GetOrOpenStream(5)
_, err := m.getOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
deleteStream(5)
Expect(err).ToNot(HaveOccurred())
str, err := m.GetOrOpenStream(5)
str, err := m.getOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
@ -99,21 +99,21 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Context("counting streams", func() {
It("errors when too many streams are opened", func() {
for i := uint32(0); i < m.maxIncomingStreams; i++ {
_, err := m.GetOrOpenStream(protocol.StreamID(i*2 + 1))
_, err := m.getOrOpenStream(protocol.StreamID(i*2 + 1))
Expect(err).NotTo(HaveOccurred())
}
_, err := m.GetOrOpenStream(protocol.StreamID(2*m.maxIncomingStreams + 3))
_, err := m.getOrOpenStream(protocol.StreamID(2*m.maxIncomingStreams + 3))
Expect(err).To(MatchError(qerr.TooManyOpenStreams))
})
It("errors when too many streams are opened implicitely", func() {
_, err := m.GetOrOpenStream(protocol.StreamID(m.maxIncomingStreams*2 + 3))
_, err := m.getOrOpenStream(protocol.StreamID(m.maxIncomingStreams*2 + 3))
Expect(err).To(MatchError(qerr.TooManyOpenStreams))
})
It("does not error when many streams are opened and closed", func() {
for i := uint32(2); i < 10*m.maxIncomingStreams; i++ {
str, err := m.GetOrOpenStream(protocol.StreamID(i*2 + 1))
str, err := m.getOrOpenStream(protocol.StreamID(i*2 + 1))
Expect(err).NotTo(HaveOccurred())
deleteStream(str.StreamID())
}
@ -151,7 +151,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(str.StreamID()).To(Equal(protocol.StreamID(2)))
deleteStream(2)
Expect(err).ToNot(HaveOccurred())
str, err = m.GetOrOpenStream(2)
str, err = m.getOrOpenStream(2)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
@ -186,7 +186,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(err).ToNot(HaveOccurred())
}
for i := 0; i < maxOutgoingStreams; i++ {
_, err := m.GetOrOpenStream(protocol.StreamID(2*i + 1))
_, err := m.getOrOpenStream(protocol.StreamID(2*i + 1))
Expect(err).ToNot(HaveOccurred())
}
})
@ -274,7 +274,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(3)
_, err := m.getOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(3)))
@ -290,7 +290,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(5)
_, err := m.getOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(3)))
@ -314,7 +314,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(err).ToNot(HaveOccurred())
close(done2)
}()
_, err := m.GetOrOpenStream(5) // opens stream 3 and 5
_, err := m.getOrOpenStream(5) // opens stream 3 and 5
Expect(err).ToNot(HaveOccurred())
Eventually(done1).Should(BeClosed())
Eventually(done2).Should(BeClosed())
@ -333,7 +333,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
_, err := m.GetOrOpenStream(3)
_, err := m.getOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(3)))
@ -349,7 +349,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(5)
_, err := m.getOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(3)))
@ -359,7 +359,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
})
It("blocks after accepting a stream", func() {
_, err := m.GetOrOpenStream(3)
_, err := m.getOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
str, err := m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
@ -407,19 +407,19 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Context("server-side streams", func() {
It("rejects streams with odd IDs", func() {
_, err := m.GetOrOpenStream(5)
_, err := m.getOrOpenStream(5)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 5"))
})
It("rejects streams with odds IDs, which are lower than the highest server-side stream", func() {
_, err := m.GetOrOpenStream(6)
_, err := m.getOrOpenStream(6)
Expect(err).NotTo(HaveOccurred())
_, err = m.GetOrOpenStream(5)
_, err = m.getOrOpenStream(5)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 5"))
})
It("gets new streams", func() {
s, err := m.GetOrOpenStream(2)
s, err := m.getOrOpenStream(2)
Expect(err).NotTo(HaveOccurred())
Expect(s.StreamID()).To(Equal(protocol.StreamID(2)))
Expect(m.streams).To(HaveLen(1))
@ -428,7 +428,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
})
It("opens skipped streams", func() {
_, err := m.GetOrOpenStream(6)
_, err := m.getOrOpenStream(6)
Expect(err).NotTo(HaveOccurred())
Expect(m.streams).To(HaveKey(protocol.StreamID(2)))
Expect(m.streams).To(HaveKey(protocol.StreamID(4)))
@ -443,7 +443,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(str.StreamID()).To(Equal(protocol.StreamID(3)))
deleteStream(3)
Expect(err).ToNot(HaveOccurred())
str, err = m.GetOrOpenStream(3)
str, err = m.getOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
@ -468,11 +468,11 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
})
It("doesn't reopen an already closed stream", func() {
_, err := m.GetOrOpenStream(4)
_, err := m.getOrOpenStream(4)
Expect(err).ToNot(HaveOccurred())
deleteStream(4)
Expect(err).ToNot(HaveOccurred())
str, err := m.GetOrOpenStream(4)
str, err := m.getOrOpenStream(4)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
@ -489,7 +489,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(2)
_, err := m.getOrOpenStream(2)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(2)))
@ -504,7 +504,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
})
It("deletes an incoming stream", func() {
_, err := m.GetOrOpenStream(5) // open stream 3 and 5
_, err := m.getOrOpenStream(5) // open stream 3 and 5
Expect(err).ToNot(HaveOccurred())
Expect(m.numIncomingStreams).To(BeEquivalentTo(2))
err = m.DeleteStream(3)
@ -534,7 +534,7 @@ var _ = Describe("Streams Map (for gQUIC)", func() {
It("sets the flow control limit", func() {
setNewStreamsMap(protocol.PerspectiveServer)
_, err := m.GetOrOpenStream(5)
_, err := m.getOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
m.streams[3].(*MockStreamI).EXPECT().handleMaxStreamDataFrame(&wire.MaxStreamDataFrame{
StreamID: 3,

View file

@ -2,414 +2,247 @@ package quic
import (
"errors"
"fmt"
"github.com/golang/mock/gomock"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/flowcontrol"
"github.com/lucas-clemente/quic-go/internal/mocks"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/wire"
"github.com/lucas-clemente/quic-go/qerr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type streamMapping struct {
firstIncomingBidiStream protocol.StreamID
firstIncomingUniStream protocol.StreamID
firstOutgoingBidiStream protocol.StreamID
firstOutgoingUniStream protocol.StreamID
}
var _ = Describe("Streams Map (for IETF QUIC)", func() {
var m *streamsMap
newStream := func(id protocol.StreamID) streamI {
str := NewMockStreamI(mockCtrl)
str.EXPECT().StreamID().Return(id).AnyTimes()
return str
newFlowController := func(protocol.StreamID) flowcontrol.StreamFlowController {
return mocks.NewMockStreamFlowController(mockCtrl)
}
setNewStreamsMap := func(p protocol.Perspective) {
m = newStreamsMap(newStream, p).(*streamsMap)
serverStreamMapping := streamMapping{
firstIncomingBidiStream: 4,
firstOutgoingBidiStream: 1,
firstIncomingUniStream: 2,
firstOutgoingUniStream: 3,
}
clientStreamMapping := streamMapping{
firstIncomingBidiStream: 1,
firstOutgoingBidiStream: 4,
firstIncomingUniStream: 3,
firstOutgoingUniStream: 2,
}
deleteStream := func(id protocol.StreamID) {
ExpectWithOffset(1, m.DeleteStream(id)).To(Succeed())
}
for _, p := range []protocol.Perspective{protocol.PerspectiveServer, protocol.PerspectiveClient} {
perspective := p
var ids streamMapping
if perspective == protocol.PerspectiveClient {
ids = clientStreamMapping
} else {
ids = serverStreamMapping
}
Context(perspective.String(), func() {
var m *streamsMap
Context("getting and creating streams", func() {
Context("as a server", func() {
BeforeEach(func() {
setNewStreamsMap(protocol.PerspectiveServer)
m = newStreamsMap(nil, newFlowController, perspective, versionIETFFrames).(*streamsMap)
})
Context("client-side streams", func() {
It("gets new streams", func() {
s, err := m.GetOrOpenStream(1)
Expect(err).NotTo(HaveOccurred())
Expect(s).ToNot(BeNil())
Expect(s.StreamID()).To(Equal(protocol.StreamID(1)))
Expect(m.streams).To(HaveLen(1))
})
It("rejects streams with even IDs", func() {
_, err := m.GetOrOpenStream(6)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 6"))
})
It("rejects streams with even IDs, which are lower thatn the highest client-side stream", func() {
_, err := m.GetOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
_, err = m.GetOrOpenStream(4)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 4"))
})
It("gets existing streams", func() {
s, err := m.GetOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
numStreams := len(m.streams)
s, err = m.GetOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
Expect(s.StreamID()).To(Equal(protocol.StreamID(5)))
Expect(m.streams).To(HaveLen(numStreams))
})
It("returns nil for closed streams", func() {
_, err := m.GetOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
deleteStream(5)
s, err := m.GetOrOpenStream(5)
Expect(err).NotTo(HaveOccurred())
Expect(s).To(BeNil())
})
It("opens skipped streams", func() {
_, err := m.GetOrOpenStream(7)
Expect(err).NotTo(HaveOccurred())
Expect(m.streams).To(HaveKey(protocol.StreamID(3)))
Expect(m.streams).To(HaveKey(protocol.StreamID(5)))
Expect(m.streams).To(HaveKey(protocol.StreamID(7)))
})
It("doesn't reopen an already closed stream", func() {
_, err := m.GetOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
deleteStream(5)
Expect(err).ToNot(HaveOccurred())
str, err := m.GetOrOpenStream(5)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
})
Context("server-side streams", func() {
It("opens a stream 2 first", func() {
s, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(s).ToNot(BeNil())
Expect(s.StreamID()).To(Equal(protocol.StreamID(2)))
})
It("returns the error when the streamsMap was closed", func() {
testErr := errors.New("test error")
m.CloseWithError(testErr)
_, err := m.OpenStream()
Expect(err).To(MatchError(testErr))
})
It("doesn't reopen an already closed stream", func() {
Context("opening", func() {
It("opens bidirectional streams", func() {
str, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(protocol.StreamID(2)))
deleteStream(2)
Expect(str).To(BeAssignableToTypeOf(&stream{}))
Expect(str.StreamID()).To(Equal(ids.firstOutgoingBidiStream))
str, err = m.OpenStream()
Expect(err).ToNot(HaveOccurred())
str, err = m.GetOrOpenStream(2)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
Expect(str).To(BeAssignableToTypeOf(&stream{}))
Expect(str.StreamID()).To(Equal(ids.firstOutgoingBidiStream + 4))
})
Context("opening streams synchronously", func() {
It("immediately returns when OpenStreamSync is called after an error was registered", func() {
testErr := errors.New("test error")
m.CloseWithError(testErr)
_, err := m.OpenStreamSync()
Expect(err).To(MatchError(testErr))
It("opens unidirectional streams", func() {
str, err := m.OpenUniStream()
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeAssignableToTypeOf(&sendStream{}))
Expect(str.StreamID()).To(Equal(ids.firstOutgoingUniStream))
str, err = m.OpenUniStream()
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeAssignableToTypeOf(&sendStream{}))
Expect(str.StreamID()).To(Equal(ids.firstOutgoingUniStream + 4))
})
})
Context("accepting", func() {
It("accepts bidirectional streams", func() {
_, err := m.GetOrOpenReceiveStream(ids.firstIncomingBidiStream)
Expect(err).ToNot(HaveOccurred())
str, err := m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeAssignableToTypeOf(&stream{}))
Expect(str.StreamID()).To(Equal(ids.firstIncomingBidiStream))
})
It("accepts unidirectional streams", func() {
_, err := m.GetOrOpenReceiveStream(ids.firstIncomingUniStream)
Expect(err).ToNot(HaveOccurred())
str, err := m.AcceptUniStream()
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeAssignableToTypeOf(&receiveStream{}))
Expect(str.StreamID()).To(Equal(ids.firstIncomingUniStream))
})
})
Context("deleting", func() {
It("deletes outgoing bidirectional streams", func() {
id := ids.firstOutgoingBidiStream
str, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(id))
Expect(m.DeleteStream(id)).To(Succeed())
dstr, err := m.GetOrOpenSendStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(dstr).To(BeNil())
})
It("deletes incoming bidirectional streams", func() {
id := ids.firstIncomingBidiStream
str, err := m.GetOrOpenReceiveStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(id))
Expect(m.DeleteStream(id)).To(Succeed())
dstr, err := m.GetOrOpenReceiveStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(dstr).To(BeNil())
})
It("deletes outgoing unidirectional streams", func() {
id := ids.firstOutgoingUniStream
str, err := m.OpenUniStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(id))
Expect(m.DeleteStream(id)).To(Succeed())
dstr, err := m.GetOrOpenSendStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(dstr).To(BeNil())
})
It("deletes incoming unidirectional streams", func() {
id := ids.firstIncomingUniStream
str, err := m.GetOrOpenReceiveStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(id))
Expect(m.DeleteStream(id)).To(Succeed())
dstr, err := m.GetOrOpenReceiveStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(dstr).To(BeNil())
})
})
Context("getting streams", func() {
Context("send streams", func() {
It("gets an outgoing bidirectional stream", func() {
// need to open the stream ourselves first
// the peer is not allowed to create a stream initiated by us
_, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
str, err := m.GetOrOpenSendStream(ids.firstOutgoingBidiStream)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(ids.firstOutgoingBidiStream))
})
It("errors when the peer tries to open a higher outgoing bidirectional stream", func() {
id := ids.firstOutgoingBidiStream + 5*4
_, err := m.GetOrOpenSendStream(id)
Expect(err).To(MatchError(qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))))
})
It("gets an outgoing unidirectional stream", func() {
// need to open the stream ourselves first
// the peer is not allowed to create a stream initiated by us
_, err := m.OpenUniStream()
Expect(err).ToNot(HaveOccurred())
str, err := m.GetOrOpenSendStream(ids.firstOutgoingUniStream)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(ids.firstOutgoingUniStream))
})
It("errors when the peer tries to open a higher outgoing bidirectional stream", func() {
id := ids.firstOutgoingUniStream + 5*4
_, err := m.GetOrOpenSendStream(id)
Expect(err).To(MatchError(qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))))
})
It("gets an incoming bidirectional stream", func() {
id := ids.firstIncomingBidiStream + 4*7
str, err := m.GetOrOpenSendStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(id))
})
It("errors when trying to get an incoming unidirectional stream", func() {
id := ids.firstIncomingUniStream
_, err := m.GetOrOpenSendStream(id)
Expect(err).To(MatchError(fmt.Errorf("peer attempted to open send stream %d", id)))
})
})
Context("receive streams", func() {
It("gets an outgoing bidirectional stream", func() {
// need to open the stream ourselves first
// the peer is not allowed to create a stream initiated by us
_, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
str, err := m.GetOrOpenReceiveStream(ids.firstOutgoingBidiStream)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(ids.firstOutgoingBidiStream))
})
It("errors when the peer tries to open a higher outgoing bidirectional stream", func() {
id := ids.firstOutgoingBidiStream + 5*4
_, err := m.GetOrOpenReceiveStream(id)
Expect(err).To(MatchError(qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("peer attempted to open stream %d", id))))
})
It("gets an incoming bidirectional stream", func() {
id := ids.firstIncomingBidiStream + 4*7
str, err := m.GetOrOpenReceiveStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(id))
})
It("gets an incoming unidirectional stream", func() {
id := ids.firstIncomingUniStream + 4*10
str, err := m.GetOrOpenReceiveStream(id)
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(id))
})
It("errors when trying to get an outgoing unidirectional stream", func() {
id := ids.firstOutgoingUniStream
_, err := m.GetOrOpenReceiveStream(id)
Expect(err).To(MatchError(fmt.Errorf("peer attempted to open receive stream %d", id)))
})
})
})
Context("accepting streams", func() {
It("does nothing if no stream is opened", func() {
var accepted bool
go func() {
_, _ = m.AcceptStream()
accepted = true
}()
Consistently(func() bool { return accepted }).Should(BeFalse())
})
It("starts with stream 1", func() {
var str Stream
done := make(chan struct{})
go func() {
defer GinkgoRecover()
var err error
str, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(1)))
})
It("returns an implicitly opened stream, if a stream number is skipped", func() {
var str Stream
done := make(chan struct{})
go func() {
defer GinkgoRecover()
var err error
str, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(1)))
})
It("returns to multiple accepts", func() {
var str1, str2 Stream
done1 := make(chan struct{})
done2 := make(chan struct{})
go func() {
defer GinkgoRecover()
var err error
str1, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
close(done1)
}()
go func() {
defer GinkgoRecover()
var err error
str2, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
close(done2)
}()
_, err := m.GetOrOpenStream(3) // opens stream 1 and 3
Expect(err).ToNot(HaveOccurred())
Eventually(done1).Should(BeClosed())
Eventually(done2).Should(BeClosed())
Expect(str1.StreamID()).ToNot(Equal(str2.StreamID()))
Expect(str1.StreamID() + str2.StreamID()).To(BeEquivalentTo(1 + 3))
})
It("waits until a new stream is available", func() {
var str Stream
done := make(chan struct{})
go func() {
defer GinkgoRecover()
var err error
str, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
_, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(1)))
})
It("returns multiple streams on subsequent Accept calls, if available", func() {
var str Stream
done := make(chan struct{})
go func() {
defer GinkgoRecover()
var err error
str, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(1)))
str, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(protocol.StreamID(3)))
})
It("blocks after accepting a stream", func() {
_, err := m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
str, err := m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(protocol.StreamID(1)))
done := make(chan struct{})
go func() {
defer GinkgoRecover()
_, _ = m.AcceptStream()
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
// make the go routine return
str.(*MockStreamI).EXPECT().closeForShutdown(gomock.Any())
m.CloseWithError(errors.New("shut down"))
Eventually(done).Should(BeClosed())
})
It("stops waiting when an error is registered", func() {
testErr := errors.New("testErr")
done := make(chan struct{})
go func() {
defer GinkgoRecover()
_, err := m.AcceptStream()
Expect(err).To(MatchError(testErr))
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
m.CloseWithError(testErr)
Eventually(done).Should(BeClosed())
})
It("immediately returns when Accept is called after an error was registered", func() {
testErr := errors.New("testErr")
m.CloseWithError(testErr)
_, err := m.AcceptStream()
Expect(err).To(MatchError(testErr))
})
It("closes", func() {
testErr := errors.New("test error")
m.CloseWithError(testErr)
_, err := m.OpenStream()
Expect(err).To(MatchError(testErr))
_, err = m.OpenUniStream()
Expect(err).To(MatchError(testErr))
_, err = m.AcceptStream()
Expect(err).To(MatchError(testErr))
_, err = m.AcceptUniStream()
Expect(err).To(MatchError(testErr))
})
})
Context("as a client", func() {
BeforeEach(func() {
setNewStreamsMap(protocol.PerspectiveClient)
})
Context("server-side streams", func() {
It("rejects streams with odd IDs", func() {
_, err := m.GetOrOpenStream(5)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 5"))
})
It("rejects streams with odds IDs, which are lower than the highest server-side stream", func() {
_, err := m.GetOrOpenStream(6)
Expect(err).NotTo(HaveOccurred())
_, err = m.GetOrOpenStream(5)
Expect(err).To(MatchError("InvalidStreamID: peer attempted to open stream 5"))
})
It("gets new streams", func() {
s, err := m.GetOrOpenStream(2)
Expect(err).NotTo(HaveOccurred())
Expect(s.StreamID()).To(Equal(protocol.StreamID(2)))
Expect(m.streams).To(HaveLen(1))
})
It("opens skipped streams", func() {
_, err := m.GetOrOpenStream(6)
Expect(err).NotTo(HaveOccurred())
Expect(m.streams).To(HaveKey(protocol.StreamID(2)))
Expect(m.streams).To(HaveKey(protocol.StreamID(4)))
Expect(m.streams).To(HaveKey(protocol.StreamID(6)))
})
It("doesn't reopen an already closed stream", func() {
str, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(str.StreamID()).To(Equal(protocol.StreamID(1)))
deleteStream(1)
Expect(err).ToNot(HaveOccurred())
str, err = m.GetOrOpenStream(1)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
})
Context("client-side streams", func() {
It("starts with stream 1", func() {
setNewStreamsMap(protocol.PerspectiveClient)
s, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(s).ToNot(BeNil())
Expect(s.StreamID()).To(BeEquivalentTo(1))
})
It("opens multiple streams", func() {
s1, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
s2, err := m.OpenStream()
Expect(err).ToNot(HaveOccurred())
Expect(s2.StreamID()).To(Equal(s1.StreamID() + 2))
})
It("doesn't reopen an already closed stream", func() {
_, err := m.GetOrOpenStream(4)
Expect(err).ToNot(HaveOccurred())
deleteStream(4)
Expect(err).ToNot(HaveOccurred())
str, err := m.GetOrOpenStream(4)
Expect(err).ToNot(HaveOccurred())
Expect(str).To(BeNil())
})
})
Context("accepting streams", func() {
It("accepts stream 2 first", func() {
var str Stream
done := make(chan struct{})
go func() {
defer GinkgoRecover()
var err error
str, err = m.AcceptStream()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
_, err := m.GetOrOpenStream(2)
Expect(err).ToNot(HaveOccurred())
Eventually(done).Should(BeClosed())
Expect(str.StreamID()).To(Equal(protocol.StreamID(2)))
})
})
})
})
Context("deleting streams", func() {
BeforeEach(func() {
setNewStreamsMap(protocol.PerspectiveServer)
})
It("deletes an incoming stream", func() {
_, err := m.GetOrOpenStream(3) // open stream 1 and 3
Expect(err).ToNot(HaveOccurred())
err = m.DeleteStream(1)
Expect(err).ToNot(HaveOccurred())
Expect(m.streams).To(HaveLen(1))
Expect(m.streams).To(HaveKey(protocol.StreamID(3)))
})
It("deletes an outgoing stream", func() {
_, err := m.OpenStream() // open stream 2
Expect(err).ToNot(HaveOccurred())
_, err = m.OpenStream()
Expect(err).ToNot(HaveOccurred())
err = m.DeleteStream(2)
Expect(err).ToNot(HaveOccurred())
})
It("errors when the stream doesn't exist", func() {
err := m.DeleteStream(1337)
Expect(err).To(MatchError(errMapAccess))
})
})
It("sets the flow control limit", func() {
setNewStreamsMap(protocol.PerspectiveServer)
_, err := m.GetOrOpenStream(3)
Expect(err).ToNot(HaveOccurred())
m.streams[1].(*MockStreamI).EXPECT().handleMaxStreamDataFrame(&wire.MaxStreamDataFrame{
StreamID: 1,
ByteOffset: 321,
})
m.streams[3].(*MockStreamI).EXPECT().handleMaxStreamDataFrame(&wire.MaxStreamDataFrame{
StreamID: 3,
ByteOffset: 321,
})
m.UpdateLimits(&handshake.TransportParameters{StreamFlowControlWindow: 321})
})
}
})