Skip to content

Commit

Permalink
Updated with threadsafe bytes buffer for log in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Moore <[email protected]>
  • Loading branch information
jimmyaxod committed Nov 1, 2024
1 parent 733bcf7 commit 57bcd5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 1 addition & 2 deletions pkg/storage/device/device_sync_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package device

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"fmt"
Expand Down Expand Up @@ -52,7 +51,7 @@ func TestDeviceSync(t *testing.T) {
err := s.Decode([]byte(testSyncSchema))
assert.NoError(t, err)

logBuffer := &bytes.Buffer{}
logBuffer := &testutils.SafeWriteBuffer{}
l := logging.New(logging.Zerolog, "device", logBuffer)
l.SetLevel(types.TraceLevel)

Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/expose/nbd_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package expose

import (
"bytes"
"crypto/rand"
"fmt"
"os"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/loopholelabs/logging/types"
"github.com/loopholelabs/silo/pkg/storage/modules"
"github.com/loopholelabs/silo/pkg/storage/sources"
"github.com/loopholelabs/silo/pkg/testutils"
"github.com/stretchr/testify/assert"
)

Expand All @@ -36,7 +36,7 @@ func TestNBDNLDevice(t *testing.T) {
size := 4096 * 1024 * 1024
prov := sources.NewMemoryStorage(size)

logBuffer := &bytes.Buffer{}
logBuffer := &testutils.SafeWriteBuffer{}
// Enable logging here to make sure it doesn't break anything...
l := logging.New(logging.Zerolog, "silo", logBuffer)
l.SetLevel(types.TraceLevel)
Expand Down
29 changes: 29 additions & 0 deletions pkg/testutils/logging_output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package testutils

import (
"bytes"
"sync"
)

type SafeWriteBuffer struct {
bufferLock sync.Mutex
buffer bytes.Buffer
}

func (swb *SafeWriteBuffer) Write(p []byte) (n int, err error) {
swb.bufferLock.Lock()
defer swb.bufferLock.Unlock()
return swb.buffer.Write(p)
}

func (swb *SafeWriteBuffer) Bytes() []byte {
swb.bufferLock.Lock()
defer swb.bufferLock.Unlock()
return swb.buffer.Bytes()
}

func (swb *SafeWriteBuffer) Len() int {
swb.bufferLock.Lock()
defer swb.bufferLock.Unlock()
return swb.buffer.Len()
}

0 comments on commit 57bcd5a

Please sign in to comment.