chunked_file_writer_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package page_writer
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "os"
  5. "testing"
  6. )
  7. func TestChunkedFileWriter_toActualOffset(t *testing.T) {
  8. cw := NewChunkedFileWriter("", 16)
  9. writeToFile(cw, 50, 60)
  10. writeToFile(cw, 60, 64)
  11. writeToFile(cw, 32, 40)
  12. writeToFile(cw, 42, 48)
  13. writeToFile(cw, 48, 50)
  14. assert.Equal(t, 1, cw.chunkUsages[0].size(), "fully covered")
  15. assert.Equal(t, 2, cw.chunkUsages[1].size(), "2 intervals")
  16. }
  17. func writeToFile(cw *ChunkedFileWriter, startOffset int64, stopOffset int64) {
  18. _, chunkUsage := cw.toActualWriteOffset(startOffset)
  19. // skip doing actual writing
  20. innerOffset := startOffset % cw.ChunkSize
  21. chunkUsage.MarkWritten(innerOffset, innerOffset+stopOffset-startOffset)
  22. }
  23. func TestWriteChunkedFile(t *testing.T) {
  24. x := NewChunkedFileWriter(os.TempDir(), 20)
  25. defer x.Destroy()
  26. y := NewChunkedFileWriter(os.TempDir(), 12)
  27. defer y.Destroy()
  28. batchSize := 4
  29. buf := make([]byte, batchSize)
  30. for i := 0; i < 256; i++ {
  31. for x := 0; x < batchSize; x++ {
  32. buf[x] = byte(i)
  33. }
  34. x.WriteAt(buf, int64(i*batchSize))
  35. y.WriteAt(buf, int64((255-i)*batchSize))
  36. }
  37. a := make([]byte, 1)
  38. b := make([]byte, 1)
  39. for i := 0; i < 256*batchSize; i++ {
  40. x.ReadDataAt(a, int64(i))
  41. y.ReadDataAt(b, int64(256*batchSize-1-i))
  42. assert.Equal(t, a[0], b[0], "same read")
  43. }
  44. }