reader_at_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package filer
  2. import (
  3. "fmt"
  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)
  70. testReadAt(t, readerAt, 0, 12, 10, io.EOF)
  71. testReadAt(t, readerAt, 2, 8, 8, io.EOF)
  72. testReadAt(t, readerAt, 3, 6, 6, nil)
  73. }
  74. func testReadAt(t *testing.T, readerAt *ChunkReadAt, offset int64, size int, expected int, expectedErr error) {
  75. data := make([]byte, size)
  76. n, err := readerAt.ReadAt(data, offset)
  77. for _, d := range data {
  78. fmt.Printf("%x", d)
  79. }
  80. fmt.Println()
  81. if expected != n {
  82. t.Errorf("unexpected read size: %d, expect: %d", n, expected)
  83. }
  84. if err != expectedErr {
  85. t.Errorf("unexpected read error: %v, expect: %v", err, expectedErr)
  86. }
  87. }
  88. func TestReaderAt0(t *testing.T) {
  89. visibles := []VisibleInterval{
  90. {
  91. start: 2,
  92. stop: 5,
  93. fileId: "1",
  94. chunkSize: 9,
  95. },
  96. {
  97. start: 7,
  98. stop: 9,
  99. fileId: "2",
  100. chunkSize: 9,
  101. },
  102. }
  103. readerAt := &ChunkReadAt{
  104. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  105. readerLock: sync.Mutex{},
  106. fileSize: 10,
  107. readerCache: newReaderCache(3, &mockChunkCache{}, nil),
  108. readerPattern: NewReaderPattern(),
  109. }
  110. testReadAt(t, readerAt, 0, 10, 10, io.EOF)
  111. testReadAt(t, readerAt, 3, 16, 7, io.EOF)
  112. testReadAt(t, readerAt, 3, 5, 5, nil)
  113. testReadAt(t, readerAt, 11, 5, 0, io.EOF)
  114. testReadAt(t, readerAt, 10, 5, 0, io.EOF)
  115. }
  116. func TestReaderAt1(t *testing.T) {
  117. visibles := []VisibleInterval{
  118. {
  119. start: 2,
  120. stop: 5,
  121. fileId: "1",
  122. chunkSize: 9,
  123. },
  124. }
  125. readerAt := &ChunkReadAt{
  126. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  127. readerLock: sync.Mutex{},
  128. fileSize: 20,
  129. readerCache: newReaderCache(3, &mockChunkCache{}, nil),
  130. readerPattern: NewReaderPattern(),
  131. }
  132. testReadAt(t, readerAt, 0, 20, 20, io.EOF)
  133. testReadAt(t, readerAt, 1, 7, 7, nil)
  134. testReadAt(t, readerAt, 0, 1, 1, nil)
  135. testReadAt(t, readerAt, 18, 4, 2, io.EOF)
  136. testReadAt(t, readerAt, 12, 4, 4, nil)
  137. testReadAt(t, readerAt, 4, 20, 16, io.EOF)
  138. testReadAt(t, readerAt, 4, 10, 10, nil)
  139. testReadAt(t, readerAt, 1, 10, 10, nil)
  140. }