Skip to content

Commit

Permalink
fix: add new mock writer that allows us to see what writes are being …
Browse files Browse the repository at this point in the history
…done against it.
  • Loading branch information
perbu committed Dec 20, 2021
1 parent 008b720 commit 3ba2b74
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 62 deletions.
80 changes: 80 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package chainsaw

import (
"bytes"
"strings"
"sync"
)

// Various helper stuff for the tests.

// stringLogger implements io.Writer interface and is useful for testing
// things that write to io.Writers. Each write is stored in a string.
type stringLogger struct {
loglines []string
mu sync.Mutex
}

func (s *stringLogger) Write(p []byte) (n int, err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.loglines == nil {
s.loglines = make([]string, 0)
}
s.loglines = append(s.loglines, string(p))
return len(p), nil
}

func (s *stringLogger) contains(substring string) bool {
s.mu.Lock()
defer s.mu.Unlock()
for _, line := range s.loglines {
if strings.Contains(line, substring) {
return true
}
}
return false
}

type SafeInt struct {
value int
m sync.RWMutex
}

func (i *SafeInt) Inc() {
i.m.Lock()
defer i.m.Unlock()
i.value++
}
func (i *SafeInt) Get() int {
i.m.RLock()
defer i.m.RUnlock()
return i.value
}

type SafeBuffer struct {
b bytes.Buffer
m sync.Mutex
}

func (b *SafeBuffer) Read(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Read(p)
}
func (b *SafeBuffer) Write(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Write(p)
}
func (b *SafeBuffer) String() string {
b.m.Lock()
defer b.m.Unlock()
return b.b.String()
}

func (b *SafeBuffer) Bytes() []byte {
b.m.Lock()
defer b.m.Unlock()
return b.b.Bytes()
}
78 changes: 16 additions & 62 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -66,8 +67,8 @@ func TestLogging(t *testing.T) {
log := MakeLogger("", testDefaultLogBufferSize, testDefaultChanBufferSize)
defer log.Stop()
is := is.New(t)
buffer := &SafeBuffer{}
err := log.AddWriter(buffer)
stringOutput := &stringLogger{}
err := log.AddWriter(stringOutput)
is.NoErr(err)
err = log.RemoveWriter(os.Stdout)
is.NoErr(err)
Expand All @@ -90,23 +91,20 @@ func TestLogging(t *testing.T) {
log.Error("Error", "concatenated")
log.Errorf("Errorf message: %d", 1)
log.Errorw("Error field", P{"errortest", 5})

_ = log.Flush()
time.Sleep(defaultSleepTime)
b := buffer.Bytes()
fmt.Println("=== log buffer =====")
fmt.Println(string(b), "====================")
is.True(!bytes.Contains(b, []byte("Trace")))
is.True(!bytes.Contains(b, []byte("Debug")))
is.True(bytes.Contains(b, []byte("Info concatenated")))
is.True(bytes.Contains(b, []byte("Infof message: 1")))
is.True(bytes.Contains(b, []byte("infotest=3")))
is.True(bytes.Contains(b, []byte("Warn concatenated")))
is.True(bytes.Contains(b, []byte("Warnf message: 1")))
is.True(bytes.Contains(b, []byte("warntest=4")))
is.True(bytes.Contains(b, []byte("Error concatenated")))
is.True(bytes.Contains(b, []byte("Errorf message: 1")))
is.True(bytes.Contains(b, []byte("errortest=5")))
is.Equal(len(stringOutput.loglines), 9)
is.True(!stringOutput.contains("Trace")) // no traces should have been logged.
is.True(!stringOutput.contains("Debug")) // no debug should have been logged.
// Check that the output arrived where we expected it to.
is.True(strings.Contains(stringOutput.loglines[0], "Info concatenated"))
is.True(strings.Contains(stringOutput.loglines[1], "Infof message: 1"))
is.True(strings.Contains(stringOutput.loglines[2], "infotest=3"))
is.True(strings.Contains(stringOutput.loglines[3], "Warn concatenated"))
is.True(strings.Contains(stringOutput.loglines[4], "Warnf message: 1"))
is.True(strings.Contains(stringOutput.loglines[5], "warntest=4"))
is.True(strings.Contains(stringOutput.loglines[6], "Error concatenated"))
is.True(strings.Contains(stringOutput.loglines[7], "Errorf message: 1"))
is.True(strings.Contains(stringOutput.loglines[8], "errortest=5"))

}

Expand All @@ -115,7 +113,6 @@ func TestRemoveWriter(t *testing.T) {
Reset()
SetLevel(InfoLevel)
buffer := &SafeBuffer{}
// buffer := bytes.NewBuffer(nil)
AddWriter(buffer)
RemoveWriter(os.Stdout)
Trace("Trace message")
Expand Down Expand Up @@ -458,46 +455,3 @@ func TestInterface(t *testing.T) {
is := is.New(t)
is.Equal(len(msgs), 10)
}

type SafeInt struct {
value int
m sync.RWMutex
}

func (i *SafeInt) Inc() {
i.m.Lock()
defer i.m.Unlock()
i.value++
}
func (i *SafeInt) Get() int {
i.m.RLock()
defer i.m.RUnlock()
return i.value
}

type SafeBuffer struct {
b bytes.Buffer
m sync.Mutex
}

func (b *SafeBuffer) Read(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Read(p)
}
func (b *SafeBuffer) Write(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Write(p)
}
func (b *SafeBuffer) String() string {
b.m.Lock()
defer b.m.Unlock()
return b.b.String()
}

func (b *SafeBuffer) Bytes() []byte {
b.m.Lock()
defer b.m.Unlock()
return b.b.Bytes()
}

0 comments on commit 3ba2b74

Please sign in to comment.