fix early garbage collection of streams

ref #83
This commit is contained in:
Lucas Clemente
2016-07-08 13:56:30 +02:00
parent 5cc88f60dc
commit f8130288a3
3 changed files with 12 additions and 6 deletions

View File

@@ -171,6 +171,8 @@ var _ = Describe("Session", func() {
Expect(session.streams[5]).ToNot(BeNil())
// We still need to close the stream locally
session.streams[5].Close()
// ... and simulate that we actually the FIN
session.streams[5].sentFin()
session.garbageCollectStreams()
Expect(session.streams).To(HaveLen(2))
Expect(session.streams[5]).To(BeNil())
@@ -252,6 +254,7 @@ var _ = Describe("Session", func() {
_, err := session.streams[5].Read([]byte{0})
Expect(err).To(MatchError(io.EOF))
session.streams[5].Close()
session.streams[5].sentFin()
session.garbageCollectStreams()
err = session.handleStreamFrame(&frames.StreamFrame{
StreamID: 5,
@@ -568,7 +571,7 @@ var _ = Describe("Session", func() {
Eventually(conn.written).Should(HaveLen(2))
})
It("sends out two small frames that are written to long after one another into two packet", func() {
It("sends out two small frames that are written to long after one another into two packets", func() {
s, err := session.OpenStream(5)
Expect(err).NotTo(HaveOccurred())
go session.run()
@@ -716,6 +719,7 @@ var _ = Describe("Session", func() {
Expect(err).NotTo(HaveOccurred())
err = s.Close()
Expect(err).NotTo(HaveOccurred())
s.(*stream).sentFin()
s.CloseRemote(0)
_, err = s.Read([]byte("a"))
Expect(err).To(MatchError(io.EOF))

View File

@@ -202,6 +202,7 @@ func (s *stream) getDataForWriting(maxBytes protocol.ByteCount) []byte {
// Close implements io.Closer
func (s *stream) Close() error {
atomic.StoreInt32(&s.closed, 1)
s.session.scheduleSending()
return nil
}
@@ -286,8 +287,9 @@ func (s *stream) finishedReading() bool {
}
func (s *stream) finishedWriting() bool {
// TODO: sentFIN
return atomic.LoadInt32(&s.closed) != 0
s.mutex.Lock()
defer s.mutex.Unlock()
return s.err != nil || (atomic.LoadInt32(&s.closed) != 0 && s.finSent)
}
func (s *stream) finished() bool {

View File

@@ -75,7 +75,7 @@ var _ = Describe("Stream Framer", func() {
})
It("has data with FIN frames", func() {
stream1.Close()
stream1.closed = 1
Expect(framer.HasData()).To(BeTrue())
})
@@ -104,7 +104,7 @@ var _ = Describe("Stream Framer", func() {
})
It("includes estimated FIN frames", func() {
stream1.Close()
stream1.closed = 1
// estimate for an average frame containing only a FIN bit
Expect(framer.EstimatedDataLen()).To(Equal(protocol.ByteCount(5)))
})
@@ -264,7 +264,7 @@ var _ = Describe("Stream Framer", func() {
Context("sending FINs", func() {
It("sends FINs when streams are closed", func() {
stream1.writeOffset = 42
stream1.Close()
stream1.closed = 1
frame, err := framer.PopStreamFrame(1000)
Expect(err).NotTo(HaveOccurred())
Expect(frame.StreamID).To(Equal(stream1.streamID))