Skip to content

Commit

Permalink
fix: avoid unexpected EOF on gz writer (#1449)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpolaert authored Aug 28, 2020
1 parent 5d13c85 commit ebdf997
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
10 changes: 5 additions & 5 deletions analytics/pubstack/eventchannel/eventchannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ func (c *EventChannel) flush() {
return
}

// reset buffers and writers
defer c.reset()

// finish writing gzip header
err := c.gz.Flush()
err := c.gz.Close()
if err != nil {
glog.Warning("[pubstack] fail to flush gzipped buffer")
glog.Warning("[pubstack] fail to close gzipped buffer")
return
}

Expand All @@ -108,9 +111,6 @@ func (c *EventChannel) flush() {
return
}

// reset buffers and writers
c.reset()

// send events (async)
go c.send(payload)
}
Expand Down
39 changes: 39 additions & 0 deletions analytics/pubstack/eventchannel/eventchannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,42 @@ func TestEventChannel_Push(t *testing.T) {
assert.Equal(t, string(data), "onetwothreefourfivesixseven")

}

func TestEventChannel_OutputFormat(t *testing.T) {

toGzip := func(payload string) []byte {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)

if _, err := zw.Write([]byte(payload)); err != nil {
assert.Fail(t, err.Error())
}

if err := zw.Close(); err != nil {
assert.Fail(t, err.Error())
}
return buf.Bytes()
}

data := make([]byte, 0)
send := func(payload []byte) error {
data = append(data, payload...)
return nil
}

eventChannel := NewEventChannel(send, 15000, 10, 2*time.Minute)

eventChannel.Push([]byte("one"))
eventChannel.flush()
eventChannel.Push([]byte("two"))
eventChannel.Push([]byte("three"))

eventChannel.Close()

time.Sleep(10 * time.Millisecond)

expected := append(toGzip("one"), toGzip("twothree")...)

assert.Equal(t, expected, data)

}

0 comments on commit ebdf997

Please sign in to comment.