mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-09 14:41:02 +00:00
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.
This commit is contained in:
parent
03f3f424aa
commit
1e0873c70b
1 changed files with 6 additions and 0 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue