seaweedfs/weed/util/chunk_cache/chunk_cache_on_disk_test.go

60 lines
1.2 KiB
Go
Raw Normal View History

2020-04-12 04:12:41 +00:00
package chunk_cache
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
)
func TestOnDisk(t *testing.T) {
tmpDir, _ := ioutil.TempDir("", "c")
defer os.RemoveAll(tmpDir)
totalDiskSizeMb := int64(32)
2020-04-12 04:12:41 +00:00
cache := NewChunkCache(0, tmpDir, totalDiskSizeMb)
2020-04-12 04:12:41 +00:00
writeCount := 5
type test_data struct {
2020-04-13 04:00:55 +00:00
data []byte
2020-04-12 04:12:41 +00:00
fileId string
2020-04-14 04:58:10 +00:00
size uint64
2020-04-12 04:12:41 +00:00
}
testData := make([]*test_data, writeCount)
2020-04-13 04:00:55 +00:00
for i := 0; i < writeCount; i++ {
2020-04-12 04:12:41 +00:00
buff := make([]byte, 1024*1024)
rand.Read(buff)
testData[i] = &test_data{
data: buff,
fileId: fmt.Sprintf("1,%daabbccdd", i+1),
2020-04-14 04:58:10 +00:00
size: uint64(len(buff)),
2020-04-12 04:12:41 +00:00
}
cache.SetChunk(testData[i].fileId, testData[i].data)
}
2020-04-13 04:00:55 +00:00
for i := 0; i < writeCount; i++ {
2020-04-14 04:58:10 +00:00
data := cache.GetChunk(testData[i].fileId, testData[i].size)
2020-04-12 04:12:41 +00:00
if bytes.Compare(data, testData[i].data) != 0 {
t.Errorf("failed to write to and read from cache: %d", i)
}
}
cache.Shutdown()
cache = NewChunkCache(0, tmpDir, totalDiskSizeMb)
2020-04-12 04:12:41 +00:00
2020-04-13 04:00:55 +00:00
for i := 0; i < writeCount; i++ {
2020-04-14 04:58:10 +00:00
data := cache.GetChunk(testData[i].fileId, testData[i].size)
2020-04-12 04:12:41 +00:00
if bytes.Compare(data, testData[i].data) != 0 {
t.Errorf("failed to write to and read from cache: %d", i)
}
}
cache.Shutdown()
}