From fa381fffa00889d83867c5989a06214720b1dc35 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 13 May 2020 12:10:10 +0700 Subject: [PATCH] add a function to tell if the framer has data --- framer.go | 15 +++++++++++++++ framer_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/framer.go b/framer.go index 382515048..f1f6f4351 100644 --- a/framer.go +++ b/framer.go @@ -10,6 +10,8 @@ import ( ) type framer interface { + HasData() bool + QueueControlFrame(wire.Frame) AppendControlFrames([]ackhandler.Frame, protocol.ByteCount) ([]ackhandler.Frame, protocol.ByteCount) @@ -43,6 +45,19 @@ func newFramer( } } +func (f *framerI) HasData() bool { + f.mutex.Lock() + hasData := len(f.streamQueue) > 0 + f.mutex.Unlock() + if hasData { + return true + } + f.controlFrameMutex.Lock() + hasData = len(f.controlFrames) > 0 + f.controlFrameMutex.Unlock() + return hasData +} + func (f *framerI) QueueControlFrame(frame wire.Frame) { f.controlFrameMutex.Lock() f.controlFrames = append(f.controlFrames, frame) diff --git a/framer_test.go b/framer_test.go index 5c89d63e9..04caf767e 100644 --- a/framer_test.go +++ b/framer_test.go @@ -50,6 +50,16 @@ var _ = Describe("Framer", func() { Expect(length).To(Equal(mdf.Length(version) + msf.Length(version))) }) + It("says if it has data", func() { + Expect(framer.HasData()).To(BeFalse()) + f := &wire.MaxDataFrame{ByteOffset: 0x42} + framer.QueueControlFrame(f) + Expect(framer.HasData()).To(BeTrue()) + frames, _ := framer.AppendControlFrames(nil, 1000) + Expect(frames).To(HaveLen(1)) + Expect(framer.HasData()).To(BeFalse()) + }) + It("appends to the slice given", func() { ping := &wire.PingFrame{} mdf := &wire.MaxDataFrame{ByteOffset: 0x42} @@ -99,6 +109,25 @@ var _ = Describe("Framer", func() { Expect(length).To(Equal(f.Length(version))) }) + It("says if it has data", func() { + streamGetter.EXPECT().GetOrOpenSendStream(id1).Return(stream1, nil).Times(2) + Expect(framer.HasData()).To(BeFalse()) + framer.AddActiveStream(id1) + Expect(framer.HasData()).To(BeTrue()) + f1 := &wire.StreamFrame{StreamID: id1, Data: []byte("foo")} + f2 := &wire.StreamFrame{StreamID: id1, Data: []byte("bar")} + stream1.EXPECT().popStreamFrame(gomock.Any()).Return(&ackhandler.Frame{Frame: f1}, true) + stream1.EXPECT().popStreamFrame(gomock.Any()).Return(&ackhandler.Frame{Frame: f2}, false) + frames, _ := framer.AppendStreamFrames(nil, protocol.MaxByteCount) + Expect(frames).To(HaveLen(1)) + Expect(frames[0].Frame).To(Equal(f1)) + Expect(framer.HasData()).To(BeTrue()) + frames, _ = framer.AppendStreamFrames(nil, protocol.MaxByteCount) + Expect(frames).To(HaveLen(1)) + Expect(frames[0].Frame).To(Equal(f2)) + Expect(framer.HasData()).To(BeFalse()) + }) + It("appends to a frame slice", func() { streamGetter.EXPECT().GetOrOpenSendStream(id1).Return(stream1, nil) f := &wire.StreamFrame{