Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions logbuffer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package goss

import (
"bytes"
"sync"
)

// syncBuffer is a bytes.Buffer that is safe to write and read concurrently.
//
// The serve tests capture log output by pointing the process-wide logger at a
// buffer with log.SetOutput. That destination is global, so a parallel test
// still writes into whichever buffer was installed last while its owner reads
// it — a data race on the buffer even though each test declares its own.
// Guarding the buffer removes the race without giving up the shared logger.
type syncBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}

func (b *syncBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}

func (b *syncBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}

func (b *syncBuffer) Reset() {
b.mu.Lock()
defer b.mu.Unlock()
b.buf.Reset()
}
9 changes: 4 additions & 5 deletions serve_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goss

import (
"bytes"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -44,7 +43,7 @@ func TestServeWithNoContentNegotiation(t *testing.T) {
for testName := range tests {
tc := tests[testName]
t.Run(testName, func(t *testing.T) {
var logOutput bytes.Buffer
var logOutput syncBuffer
log.SetOutput(&logOutput)

config, err := util.NewConfig(
Expand Down Expand Up @@ -158,7 +157,7 @@ func TestServeNegotiatingContent(t *testing.T) {
for testName := range tests {
tc := tests[testName]
t.Run(testName, func(t *testing.T) {
var logOutput bytes.Buffer
var logOutput syncBuffer
log.SetOutput(&logOutput)

config, err := util.NewConfig(
Expand Down Expand Up @@ -189,7 +188,7 @@ func TestServeNegotiatingContent(t *testing.T) {
}

func TestServeCacheWithNoContentNegotiation(t *testing.T) {
var logOutput bytes.Buffer
var logOutput syncBuffer
log.SetOutput(&logOutput)
const cache = time.Duration(time.Millisecond * 100)
config, err := util.NewConfig(
Expand Down Expand Up @@ -236,7 +235,7 @@ func TestServeCacheWithNoContentNegotiation(t *testing.T) {
}

func TestServeCacheNegotiatingContent(t *testing.T) {
var logOutput bytes.Buffer
var logOutput syncBuffer
log.SetOutput(&logOutput)
const cache = time.Duration(time.Millisecond * 100)
config, err := util.NewConfig(
Expand Down