From 14a44642661a8929365e2b286cec132fa637b5e5 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 30 Oct 2018 11:27:42 +0700 Subject: [PATCH] implement some stream ID helper functions --- internal/protocol/stream_id.go | 13 ++++++ internal/protocol/stream_id_test.go | 68 ++++++++++++++++++----------- 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/internal/protocol/stream_id.go b/internal/protocol/stream_id.go index edf914259..49c4df7e8 100644 --- a/internal/protocol/stream_id.go +++ b/internal/protocol/stream_id.go @@ -3,6 +3,19 @@ package protocol // A StreamID in QUIC type StreamID uint64 +// InitiatedBy says if the stream was initiated by the client or by the server +func (s StreamID) InitiatedBy() Perspective { + if s%2 == 0 { + return PerspectiveClient + } + return PerspectiveServer +} + +// IsUniDirectional says if this is a unidirectional stream (true) or not (false) +func (s StreamID) IsUniDirectional() bool { + return s%4 >= 2 +} + // MaxBidiStreamID is the highest stream ID that the peer is allowed to open, // when it is allowed to open numStreams bidirectional streams. func MaxBidiStreamID(numStreams int, pers Perspective) StreamID { diff --git a/internal/protocol/stream_id_test.go b/internal/protocol/stream_id_test.go index 306ae31a7..72adac35f 100644 --- a/internal/protocol/stream_id_test.go +++ b/internal/protocol/stream_id_test.go @@ -6,37 +6,53 @@ import ( ) var _ = Describe("Stream ID", func() { - Context("bidirectional streams", func() { - It("doesn't allow any", func() { - Expect(MaxBidiStreamID(0, PerspectiveClient)).To(Equal(StreamID(0))) - Expect(MaxBidiStreamID(0, PerspectiveServer)).To(Equal(StreamID(0))) - }) - - It("allows one", func() { - Expect(MaxBidiStreamID(1, PerspectiveClient)).To(Equal(StreamID(1))) - Expect(MaxBidiStreamID(1, PerspectiveServer)).To(Equal(StreamID(0))) - }) - - It("allows many", func() { - Expect(MaxBidiStreamID(100, PerspectiveClient)).To(Equal(StreamID(397))) - Expect(MaxBidiStreamID(100, PerspectiveServer)).To(Equal(StreamID(396))) - }) + It("says who initiated a stream", func() { + Expect(StreamID(4).InitiatedBy()).To(Equal(PerspectiveClient)) + Expect(StreamID(5).InitiatedBy()).To(Equal(PerspectiveServer)) + Expect(StreamID(6).InitiatedBy()).To(Equal(PerspectiveClient)) + Expect(StreamID(7).InitiatedBy()).To(Equal(PerspectiveServer)) }) - Context("unidirectional streams", func() { - It("doesn't allow any", func() { - Expect(MaxUniStreamID(0, PerspectiveClient)).To(Equal(StreamID(0))) - Expect(MaxUniStreamID(0, PerspectiveServer)).To(Equal(StreamID(0))) + It("tells the directionality", func() { + Expect(StreamID(4).IsUniDirectional()).To(BeFalse()) + Expect(StreamID(5).IsUniDirectional()).To(BeFalse()) + Expect(StreamID(6).IsUniDirectional()).To(BeTrue()) + Expect(StreamID(7).IsUniDirectional()).To(BeTrue()) + }) + + Context("maximum stream IDs", func() { + Context("bidirectional streams", func() { + It("doesn't allow any", func() { + Expect(MaxBidiStreamID(0, PerspectiveClient)).To(Equal(StreamID(0))) + Expect(MaxBidiStreamID(0, PerspectiveServer)).To(Equal(StreamID(0))) + }) + + It("allows one", func() { + Expect(MaxBidiStreamID(1, PerspectiveClient)).To(Equal(StreamID(1))) + Expect(MaxBidiStreamID(1, PerspectiveServer)).To(Equal(StreamID(0))) + }) + + It("allows many", func() { + Expect(MaxBidiStreamID(100, PerspectiveClient)).To(Equal(StreamID(397))) + Expect(MaxBidiStreamID(100, PerspectiveServer)).To(Equal(StreamID(396))) + }) }) - It("allows one", func() { - Expect(MaxUniStreamID(1, PerspectiveClient)).To(Equal(StreamID(3))) - Expect(MaxUniStreamID(1, PerspectiveServer)).To(Equal(StreamID(2))) - }) + Context("unidirectional streams", func() { + It("doesn't allow any", func() { + Expect(MaxUniStreamID(0, PerspectiveClient)).To(Equal(StreamID(0))) + Expect(MaxUniStreamID(0, PerspectiveServer)).To(Equal(StreamID(0))) + }) - It("allows many", func() { - Expect(MaxUniStreamID(100, PerspectiveClient)).To(Equal(StreamID(399))) - Expect(MaxUniStreamID(100, PerspectiveServer)).To(Equal(StreamID(398))) + It("allows one", func() { + Expect(MaxUniStreamID(1, PerspectiveClient)).To(Equal(StreamID(3))) + Expect(MaxUniStreamID(1, PerspectiveServer)).To(Equal(StreamID(2))) + }) + + It("allows many", func() { + Expect(MaxUniStreamID(100, PerspectiveClient)).To(Equal(StreamID(399))) + Expect(MaxUniStreamID(100, PerspectiveServer)).To(Equal(StreamID(398))) + }) }) }) })