Skip to content

Commit

Permalink
Added readOnlyGate test and fixed typo in impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyaxod committed Feb 13, 2024
1 parent afbae01 commit c686c2d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/storage/modules/read_only_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type ReadOnlyGate struct {
locked bool
}

func NewReadOnlyGate(prov storage.StorageProvider) *Lockable {
return &Lockable{
func NewReadOnlyGate(prov storage.StorageProvider) *ReadOnlyGate {
return &ReadOnlyGate{
prov: prov,
lock: sync.NewCond(&sync.Mutex{}),
locked: false,
Expand Down
43 changes: 43 additions & 0 deletions pkg/storage/modules/read_only_gate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package modules

import (
"testing"
"time"

"github.com/loopholelabs/silo/pkg/storage/sources"
"github.com/stretchr/testify/assert"
)

func TestReadOnlyGate(t *testing.T) {

// Create a new block storage, backed by memory storage
size := 1024 * 1024 * 32
mem := sources.NewMemoryStorage(size)
metrics := NewMetrics(mem)
gate := NewReadOnlyGate(metrics)

// First try a write
buffer := make([]byte, 1234567)
_, err := gate.WriteAt(buffer, 10)
assert.NoError(t, err)

// Now lock and wait...
gate.Lock()

// Reads should still get through
_, err = gate.ReadAt(buffer, 10)
assert.NoError(t, err)

go func() {
time.Sleep(50 * time.Millisecond)
gate.Unlock()
}()

ctime := time.Now()
_, err = gate.WriteAt(buffer, 10)
assert.NoError(t, err)
d := time.Since(ctime).Milliseconds()

// Should have taken around 50ms
assert.InDelta(t, 50, d, 10)
}

0 comments on commit c686c2d

Please sign in to comment.