Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #847 from mozilla-services/fileoutput-null-payload
Browse files Browse the repository at this point in the history
Fileoutput null payload
  • Loading branch information
Mike Trinkala committed May 15, 2014
2 parents 9219ed0 + 124fe87 commit b92b966
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
Bug Handling
------------

* FileOutput no longer panics when using `format = "text"` and payload is nil
(issue #843).

* Fix SandboxDecoder pass-through case so decoders that only use write_message
and not inject_message will emit messages correctly (issue #844).

Expand Down
2 changes: 1 addition & 1 deletion plugins/file/file_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (o *FileOutput) handleMessage(pack *PipelinePack, outBytes *[]byte) (err er
err = fmt.Errorf("Can't encode to JSON: %s", err)
}
case "text":
*outBytes = append(*outBytes, *pack.Message.Payload...)
*outBytes = append(*outBytes, pack.Message.GetPayload()...)
*outBytes = append(*outBytes, NEWLINE)
case "protobufstream":
if err = ProtobufEncodeMessage(pack, &*outBytes); err != nil {
Expand Down
28 changes: 26 additions & 2 deletions plugins/file/file_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -68,13 +69,15 @@ func FileOutputSpec(c gs.Context) {
outData := make([]byte, 0, 20)

c.Specify("by default", func() {
fileOutput.handleMessage(pack, &outData)
err = fileOutput.handleMessage(pack, &outData)
c.Expect(err, gs.IsNil)
c.Expect(toString(&outData), gs.Equals, *msg.Payload+"\n")
})

c.Specify("w/ a prepended timestamp when specified", func() {
fileOutput.prefix_ts = true
fileOutput.handleMessage(pack, &outData)
err = fileOutput.handleMessage(pack, &outData)
c.Expect(err, gs.IsNil)
// Test will fail if date flips btn handleMessage call and
// todayStr calculation... should be extremely rare.
todayStr := time.Now().Format("[2006/Jan/02:")
Expand All @@ -83,6 +86,27 @@ func FileOutputSpec(c gs.Context) {
c.Expect(strContents, pipeline_ts.StringContains, payload)
c.Expect(strContents, pipeline_ts.StringStartsWith, todayStr)
})

c.Specify("even when payload is nil", func() {
pack.Message.Payload = nil
err = fileOutput.handleMessage(pack, &outData)
c.Expect(err, gs.IsNil)
strContents := toString(&outData)
c.Expect(strContents, gs.Equals, "\n")
})

c.Specify("payload is nil and with a timestamp", func() {
pack.Message.Payload = nil
fileOutput.prefix_ts = true
err = fileOutput.handleMessage(pack, &outData)
c.Expect(err, gs.IsNil)
// Test will fail if date flips btn handleMessage call and
// todayStr calculation... should be extremely rare.
todayStr := time.Now().Format("[2006/Jan/02:")
strContents := toString(&outData)
c.Expect(strings.HasPrefix(strContents, todayStr), gs.IsTrue)
c.Expect(strings.HasSuffix(strContents, " \n"), gs.IsTrue)
})
})

c.Specify("correctly formats JSON output", func() {
Expand Down

0 comments on commit b92b966

Please sign in to comment.