Skip to content

Commit

Permalink
Added unit test for waitingCache
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyaxod committed Feb 12, 2024
1 parent a75a41e commit 25c30a8
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/storage/modules/waiting_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package modules

import (
"crypto/rand"
"testing"
"time"

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

func TestWaitingCache(t *testing.T) {

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

data := make([]byte, 12000)
rand.Read(data)

// We'll write something in 50ms
go func() {
time.Sleep(50 * time.Millisecond)
_, err := waiting.WriteAt(data, 0)
assert.NoError(t, err)
}()

// The waiting cache will wait for data to be available.
offset := int64(20)
buffer := make([]byte, 8000)
ctime := time.Now()
_, err := waiting.ReadAt(buffer, offset)
assert.NoError(t, err)
wait_time := time.Since(ctime).Milliseconds()

// We'd expect this read to take around 50ms (It's waiting for the data)
assert.InDelta(t, wait_time, 50, 10)

assert.Equal(t, data[offset:int(offset)+len(buffer)], buffer)

// Read again
ctime2 := time.Now()
_, err = waiting.ReadAt(buffer, offset)
assert.NoError(t, err)
wait_time2 := time.Since(ctime2).Milliseconds()

// We'd expect this read to be instant (The data exists now)
assert.InDelta(t, wait_time2, 0, 10)

}

0 comments on commit 25c30a8

Please sign in to comment.