-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Package lockbuffer provides a thread-safe buffer that can be leveraged during testing to capture logs or other output. | ||
|
||
package lockbuffer | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"sync" | ||
) | ||
|
||
// LockBuffer is a thread-safe ReadWriter that wraps a bytes.Buffer | ||
type LockBuffer struct { | ||
mu sync.Mutex | ||
buffer *bytes.Buffer | ||
} | ||
|
||
// NewLockBuffer creates a new LockBuffer instance | ||
func NewLockBuffer() *LockBuffer { | ||
return &LockBuffer{ | ||
buffer: bytes.NewBuffer(nil), | ||
} | ||
} | ||
|
||
// Write writes to the buffer safely | ||
func (lb *LockBuffer) Write(p []byte) (int, error) { | ||
lb.mu.Lock() | ||
defer lb.mu.Unlock() | ||
return lb.buffer.Write(p) | ||
} | ||
|
||
// Read reads from the buffer safely | ||
func (lb *LockBuffer) Read(p []byte) (int, error) { | ||
lb.mu.Lock() | ||
defer lb.mu.Unlock() | ||
return lb.buffer.Read(p) | ||
} | ||
|
||
// WriteTo implements the io.WriterTo interface, writing the buffer's content to the given Writer safely | ||
func (lb *LockBuffer) WriteTo(w io.Writer) (int64, error) { | ||
lb.mu.Lock() | ||
defer lb.mu.Unlock() | ||
return lb.buffer.WriteTo(w) | ||
} | ||
|
||
// ReadFrom implements the io.ReaderFrom interface, reading content into the buffer from the given Reader safely | ||
func (lb *LockBuffer) ReadFrom(r io.Reader) (int64, error) { | ||
lb.mu.Lock() | ||
defer lb.mu.Unlock() | ||
return lb.buffer.ReadFrom(r) | ||
} | ||
|
||
// Bytes returns the buffer's content as a byte slice safely | ||
func (lb *LockBuffer) Bytes() []byte { | ||
lb.mu.Lock() | ||
defer lb.mu.Unlock() | ||
return lb.buffer.Bytes() | ||
} | ||
|
||
// String returns the buffer's content as a string safely | ||
func (lb *LockBuffer) String() string { | ||
lb.mu.Lock() | ||
defer lb.mu.Unlock() | ||
return lb.buffer.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters