chunk_cache_on_disk_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package chunk_cache
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "math/rand"
  7. "os"
  8. "testing"
  9. )
  10. func TestOnDisk(t *testing.T) {
  11. tmpDir, _ := ioutil.TempDir("", "c")
  12. defer os.RemoveAll(tmpDir)
  13. totalDiskSizeMb := int64(32)
  14. cache := NewChunkCache(0, tmpDir, totalDiskSizeMb)
  15. writeCount := 5
  16. type test_data struct {
  17. data []byte
  18. fileId string
  19. size uint64
  20. }
  21. testData := make([]*test_data, writeCount)
  22. for i := 0; i < writeCount; i++ {
  23. buff := make([]byte, 1024*1024)
  24. rand.Read(buff)
  25. testData[i] = &test_data{
  26. data: buff,
  27. fileId: fmt.Sprintf("1,%daabbccdd", i+1),
  28. size: uint64(len(buff)),
  29. }
  30. cache.SetChunk(testData[i].fileId, testData[i].data)
  31. }
  32. for i := 0; i < writeCount; i++ {
  33. data := cache.GetChunk(testData[i].fileId, testData[i].size)
  34. if bytes.Compare(data, testData[i].data) != 0 {
  35. t.Errorf("failed to write to and read from cache: %d", i)
  36. }
  37. }
  38. cache.Shutdown()
  39. cache = NewChunkCache(0, tmpDir, totalDiskSizeMb)
  40. for i := 0; i < writeCount; i++ {
  41. data := cache.GetChunk(testData[i].fileId, testData[i].size)
  42. if bytes.Compare(data, testData[i].data) != 0 {
  43. t.Errorf("failed to write to and read from cache: %d", i)
  44. }
  45. }
  46. cache.Shutdown()
  47. }