reader_at_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package filer
  2. import (
  3. "bytes"
  4. "io"
  5. "math"
  6. "strconv"
  7. "testing"
  8. )
  9. type mockChunkCache struct {
  10. }
  11. func (m *mockChunkCache) GetChunk(fileId string, minSize uint64) (data []byte) {
  12. x, _ := strconv.Atoi(fileId)
  13. data = make([]byte, minSize)
  14. for i := 0; i < int(minSize); i++ {
  15. data[i] = byte(x)
  16. }
  17. return data
  18. }
  19. func (m *mockChunkCache) ReadChunkAt(data []byte, fileId string, offset uint64) (n int, err error) {
  20. x, _ := strconv.Atoi(fileId)
  21. for i := 0; i < len(data); i++ {
  22. data[i] = byte(x)
  23. }
  24. return len(data), nil
  25. }
  26. func (m *mockChunkCache) SetChunk(fileId string, data []byte) {
  27. }
  28. func TestReaderAt(t *testing.T) {
  29. visibles := NewIntervalList[*VisibleInterval]()
  30. addVisibleInterval(visibles, &VisibleInterval{
  31. start: 1,
  32. stop: 2,
  33. fileId: "1",
  34. chunkSize: 9,
  35. })
  36. addVisibleInterval(visibles, &VisibleInterval{
  37. start: 3,
  38. stop: 4,
  39. fileId: "3",
  40. chunkSize: 1,
  41. })
  42. addVisibleInterval(visibles, &VisibleInterval{
  43. start: 5,
  44. stop: 6,
  45. fileId: "5",
  46. chunkSize: 2,
  47. })
  48. addVisibleInterval(visibles, &VisibleInterval{
  49. start: 7,
  50. stop: 9,
  51. fileId: "7",
  52. chunkSize: 2,
  53. })
  54. addVisibleInterval(visibles, &VisibleInterval{
  55. start: 9,
  56. stop: 10,
  57. fileId: "9",
  58. chunkSize: 2,
  59. })
  60. readerAt := &ChunkReadAt{
  61. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  62. fileSize: 10,
  63. readerCache: NewReaderCache(3, &mockChunkCache{}, nil),
  64. readerPattern: NewReaderPattern(),
  65. }
  66. testReadAt(t, readerAt, 0, 10, 10, io.EOF, nil, nil)
  67. testReadAt(t, readerAt, 0, 12, 12, io.EOF, nil, nil)
  68. testReadAt(t, readerAt, 2, 8, 8, io.EOF, nil, nil)
  69. testReadAt(t, readerAt, 3, 6, 6, nil, nil, nil)
  70. }
  71. func testReadAt(t *testing.T, readerAt *ChunkReadAt, offset int64, size int, expectedN int, expectedErr error, data, expectedData []byte) {
  72. if data == nil {
  73. data = make([]byte, size)
  74. }
  75. n, _, err := readerAt.doReadAt(data, offset)
  76. if expectedN != n {
  77. t.Errorf("unexpected read size: %d, expect: %d", n, expectedN)
  78. }
  79. if err != expectedErr {
  80. t.Errorf("unexpected read error: %v, expect: %v", err, expectedErr)
  81. }
  82. if expectedData != nil && !bytes.Equal(data, expectedData) {
  83. t.Errorf("unexpected read data: %v, expect: %v", data, expectedData)
  84. }
  85. }
  86. func TestReaderAt0(t *testing.T) {
  87. visibles := NewIntervalList[*VisibleInterval]()
  88. addVisibleInterval(visibles, &VisibleInterval{
  89. start: 2,
  90. stop: 5,
  91. fileId: "1",
  92. chunkSize: 9,
  93. })
  94. addVisibleInterval(visibles, &VisibleInterval{
  95. start: 7,
  96. stop: 9,
  97. fileId: "2",
  98. chunkSize: 9,
  99. })
  100. readerAt := &ChunkReadAt{
  101. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  102. fileSize: 10,
  103. readerCache: NewReaderCache(3, &mockChunkCache{}, nil),
  104. readerPattern: NewReaderPattern(),
  105. }
  106. testReadAt(t, readerAt, 0, 10, 10, io.EOF, nil, nil)
  107. testReadAt(t, readerAt, 3, 16, 7, io.EOF, nil, nil)
  108. testReadAt(t, readerAt, 3, 5, 5, nil, nil, nil)
  109. testReadAt(t, readerAt, 11, 5, 5, io.EOF, nil, nil)
  110. testReadAt(t, readerAt, 10, 5, 5, io.EOF, nil, nil)
  111. }
  112. func TestReaderAt1(t *testing.T) {
  113. visibles := NewIntervalList[*VisibleInterval]()
  114. addVisibleInterval(visibles, &VisibleInterval{
  115. start: 2,
  116. stop: 5,
  117. fileId: "1",
  118. chunkSize: 9,
  119. })
  120. readerAt := &ChunkReadAt{
  121. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  122. fileSize: 20,
  123. readerCache: NewReaderCache(3, &mockChunkCache{}, nil),
  124. readerPattern: NewReaderPattern(),
  125. }
  126. testReadAt(t, readerAt, 0, 20, 20, io.EOF, nil, nil)
  127. testReadAt(t, readerAt, 1, 7, 7, nil, nil, nil)
  128. testReadAt(t, readerAt, 0, 1, 1, nil, nil, nil)
  129. testReadAt(t, readerAt, 18, 4, 2, io.EOF, nil, nil)
  130. testReadAt(t, readerAt, 12, 4, 4, nil, nil, nil)
  131. testReadAt(t, readerAt, 4, 20, 16, io.EOF, nil, nil)
  132. testReadAt(t, readerAt, 4, 10, 10, nil, nil, nil)
  133. testReadAt(t, readerAt, 1, 10, 10, nil, nil, nil)
  134. }
  135. func TestReaderAtGappedChunksDoNotLeak(t *testing.T) {
  136. visibles := NewIntervalList[*VisibleInterval]()
  137. addVisibleInterval(visibles, &VisibleInterval{
  138. start: 2,
  139. stop: 3,
  140. fileId: "1",
  141. chunkSize: 5,
  142. })
  143. addVisibleInterval(visibles, &VisibleInterval{
  144. start: 7,
  145. stop: 9,
  146. fileId: "1",
  147. chunkSize: 4,
  148. })
  149. readerAt := &ChunkReadAt{
  150. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  151. fileSize: 9,
  152. readerCache: NewReaderCache(3, &mockChunkCache{}, nil),
  153. readerPattern: NewReaderPattern(),
  154. }
  155. testReadAt(t, readerAt, 0, 9, 9, io.EOF, []byte{2, 2, 2, 2, 2, 2, 2, 2, 2}, []byte{0, 0, 1, 0, 0, 0, 0, 1, 1})
  156. testReadAt(t, readerAt, 1, 8, 8, io.EOF, []byte{2, 2, 2, 2, 2, 2, 2, 2}, []byte{0, 1, 0, 0, 0, 0, 1, 1})
  157. }
  158. func TestReaderAtSparseFileDoesNotLeak(t *testing.T) {
  159. readerAt := &ChunkReadAt{
  160. chunkViews: ViewFromVisibleIntervals(NewIntervalList[*VisibleInterval](), 0, math.MaxInt64),
  161. fileSize: 3,
  162. readerCache: NewReaderCache(3, &mockChunkCache{}, nil),
  163. readerPattern: NewReaderPattern(),
  164. }
  165. testReadAt(t, readerAt, 0, 3, 3, io.EOF, []byte{2, 2, 2}, []byte{0, 0, 0})
  166. testReadAt(t, readerAt, 1, 2, 2, io.EOF, []byte{2, 2}, []byte{0, 0})
  167. }