From c686c2db9d3cfdd96270e47ac4e4c17ad3b68cbc Mon Sep 17 00:00:00 2001 From: Jimmy Moore Date: Tue, 13 Feb 2024 12:48:00 +0000 Subject: [PATCH] Added readOnlyGate test and fixed typo in impl --- pkg/storage/modules/read_only_gate.go | 4 +- pkg/storage/modules/read_only_gate_test.go | 43 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 pkg/storage/modules/read_only_gate_test.go diff --git a/pkg/storage/modules/read_only_gate.go b/pkg/storage/modules/read_only_gate.go index 94503b63..8a4c8258 100644 --- a/pkg/storage/modules/read_only_gate.go +++ b/pkg/storage/modules/read_only_gate.go @@ -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, diff --git a/pkg/storage/modules/read_only_gate_test.go b/pkg/storage/modules/read_only_gate_test.go new file mode 100644 index 00000000..61026b90 --- /dev/null +++ b/pkg/storage/modules/read_only_gate_test.go @@ -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) +}