reader_at_test.go 4.7 KB

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