-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added readOnlyGate test and fixed typo in impl
- Loading branch information
Showing
2 changed files
with
45 additions
and
2 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
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,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) | ||
} |