Skip to content

Commit

Permalink
Merge pull request #3145 from getlantern/issue3143
Browse files Browse the repository at this point in the history
add test for NonStopWriter
  • Loading branch information
myleshorton committed Sep 15, 2015
2 parents 3c64880 + f081e96 commit 30e03f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/github.com/getlantern/flashlight/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,8 @@ func NonStopWriter(writers ...io.Writer) io.Writer {
// It never fails and always return the length of bytes passed in
func (t *nonStopWriter) Write(p []byte) (int, error) {
for _, w := range t.writers {
if n, err := w.Write(p); err != nil {
return n, err
}
// intentionally not checking for errors
_, _ = w.Write(p)
}
return len(p), nil
}
Expand Down
20 changes: 20 additions & 0 deletions src/github.com/getlantern/flashlight/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logging
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strings"
"testing"
Expand All @@ -12,6 +13,25 @@ import (
"github.com/stretchr/testify/assert"
)

type BadWriter struct{}
type GoodWriter struct{ counter int }

func (w *BadWriter) Write(p []byte) (int, error) {
return 0, fmt.Errorf("Fail intentionally")
}

func (w *GoodWriter) Write(p []byte) (int, error) {
w.counter = len(p)
return w.counter, nil
}

func TestNonStopWriter(t *testing.T) {
b, g := BadWriter{}, GoodWriter{}
ns := NonStopWriter(&b, &g)
ns.Write([]byte("1234"))
assert.Equal(t, 4, g.counter, "Should write to all writers even when error encountered")
}

func TestLoggly(t *testing.T) {
var buf bytes.Buffer
var result map[string]interface{}
Expand Down

0 comments on commit 30e03f0

Please sign in to comment.