reader_at_test.go 5.2 KB

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