aboutsummaryrefslogtreecommitdiff
path: root/weed/util/chunk_cache/chunk_cache_on_disk_test.go
blob: 256b101399917a94eb93f1cbab257033faea3ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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(6)
	segmentCount := 2

	cache := NewChunkCache(0, tmpDir, totalDiskSizeMb, segmentCount)

	writeCount := 5
	type test_data struct {
		data []byte
		fileId string
	}
	testData := make([]*test_data, writeCount)
	for i:=0;i<writeCount;i++{
		buff := make([]byte, 1024*1024)
		rand.Read(buff)
		testData[i] = &test_data{
			data:   buff,
			fileId: fmt.Sprintf("1,%daabbccdd", i+1),
		}
		cache.SetChunk(testData[i].fileId, testData[i].data)
	}

	for i:=0;i<writeCount;i++{
		data := cache.GetChunk(testData[i].fileId)
		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, segmentCount)

	for i:=0;i<writeCount;i++{
		data := cache.GetChunk(testData[i].fileId)
		if bytes.Compare(data, testData[i].data) != 0 {
			t.Errorf("failed to write to and read from cache: %d", i)
		}
	}

	cache.Shutdown()

}