From 1e0873c70bb4ea0385ba7532bc46f009b61e3233 Mon Sep 17 00:00:00 2001 From: Seebs Date: Wed, 6 Nov 2019 13:51:53 -0600 Subject: [PATCH] lock BufferLogger for reads/writes With the new addition of the holder background scan, it's possible for an open holder to write log messages at arbitrary times. The TestHolder_Open/ErrIndexName test checks the contents of the output buffer, but those contents could be changing if the background task happens to run at the right time. Use trivial locking around that so that this shouldn't happen. --- test/logger.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/logger.go b/test/logger.go index 60af32acb..ecdfbfc40 100644 --- a/test/logger.go +++ b/test/logger.go @@ -18,12 +18,14 @@ import ( "bytes" "fmt" "io/ioutil" + "sync" ) // bufferLogger represents a test Logger that holds log messages // in a buffer for review. type bufferLogger struct { buf *bytes.Buffer + mu sync.Mutex } // NewBufferLogger returns a new instance of BufferLogger. @@ -34,6 +36,8 @@ func NewBufferLogger() *bufferLogger { } func (b *bufferLogger) Printf(format string, v ...interface{}) { + b.mu.Lock() + defer b.mu.Unlock() s := fmt.Sprintf(format, v...) _, err := b.buf.WriteString(s) if err != nil { @@ -44,5 +48,7 @@ func (b *bufferLogger) Printf(format string, v ...interface{}) { func (b *bufferLogger) Debugf(format string, v ...interface{}) {} func (b *bufferLogger) ReadAll() ([]byte, error) { + b.mu.Lock() + defer b.mu.Unlock() return ioutil.ReadAll(b.buf) }