reader_at_test.go 5.2 KB

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