From d7285430f23e512ef2bfc2965c4f9fe6563108c7 Mon Sep 17 00:00:00 2001 From: Nathan Rijksen Date: Wed, 13 Sep 2023 10:22:50 -0700 Subject: [PATCH] Fix EOF not triggering a clean of the final output --- outputproducer.go | 3 +++ outputproducer_test.go | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/outputproducer.go b/outputproducer.go index fa98338..3f91138 100644 --- a/outputproducer.go +++ b/outputproducer.go @@ -86,6 +86,9 @@ func (o *outputProducer) processNextRead(r io.Reader, w io.Writer, appendBuffer if _, err := w.Write(snapshot[:n]); err != nil { return fmt.Errorf("could not write: %w", err) } + } + + if n > 0 || isEOF { if err := appendBuffer(snapshot[:n], isEOF); err != nil { return fmt.Errorf("could not append buffer: %w", err) } diff --git a/outputproducer_test.go b/outputproducer_test.go index 46c0bad..5d2775e 100644 --- a/outputproducer_test.go +++ b/outputproducer_test.go @@ -37,7 +37,7 @@ func Test_outputProducer_listen(t *testing.T) { {[]byte("Three"), chunkInterval}, }, }, - []string{"One", "Two", "Three"}, + []string{"One", "Two", "Three", ""}, // Last append is EOF and should ALWAYS happen nil, }, { @@ -48,7 +48,18 @@ func Test_outputProducer_listen(t *testing.T) { {[]byte(valExceedBuff), chunkInterval}, }, }, - []string{valExceedBuff[:bufferSize], valExceedBuff[bufferSize:]}, + []string{valExceedBuff[:bufferSize], valExceedBuff[bufferSize:], ""}, // Last append is EOF and should ALWAYS happen + nil, + }, + { + "EOF should trigger an append, even if new output is empty", + func(t *testing.T) *outputProducer { return newOutputProducer(newTestOpts(nil, t)) }, + &readTester{ + []readTesterChunk{ + {[]byte("foo"), chunkInterval}, + }, + }, + []string{"foo", ""}, nil, }, }