You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.2 KiB

4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
  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) GetChunkSlice(fileId string, offset, length uint64) []byte {
  21. return m.GetChunk(fileId, length)
  22. }
  23. func (m *mockChunkCache) SetChunk(fileId string, data []byte) {
  24. }
  25. func TestReaderAt(t *testing.T) {
  26. visibles := []VisibleInterval{
  27. {
  28. start: 1,
  29. stop: 2,
  30. fileId: "1",
  31. chunkSize: 9,
  32. },
  33. {
  34. start: 3,
  35. stop: 4,
  36. fileId: "3",
  37. chunkSize: 1,
  38. },
  39. {
  40. start: 5,
  41. stop: 6,
  42. fileId: "5",
  43. chunkSize: 2,
  44. },
  45. {
  46. start: 7,
  47. stop: 9,
  48. fileId: "7",
  49. chunkSize: 2,
  50. },
  51. {
  52. start: 9,
  53. stop: 10,
  54. fileId: "9",
  55. chunkSize: 2,
  56. },
  57. }
  58. readerAt := &ChunkReadAt{
  59. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  60. lookupFileId: nil,
  61. readerLock: sync.Mutex{},
  62. fileSize: 10,
  63. chunkCache: &mockChunkCache{},
  64. readerPattern: NewReaderPattern(),
  65. }
  66. testReadAt(t, readerAt, 0, 10, 10, io.EOF)
  67. testReadAt(t, readerAt, 0, 12, 10, io.EOF)
  68. testReadAt(t, readerAt, 2, 8, 8, io.EOF)
  69. testReadAt(t, readerAt, 3, 6, 6, nil)
  70. }
  71. func testReadAt(t *testing.T, readerAt *ChunkReadAt, offset int64, size int, expected int, expectedErr error) {
  72. data := make([]byte, size)
  73. n, err := readerAt.ReadAt(data, offset)
  74. for _, d := range data {
  75. fmt.Printf("%x", d)
  76. }
  77. fmt.Println()
  78. if expected != n {
  79. t.Errorf("unexpected read size: %d, expect: %d", n, expected)
  80. }
  81. if err != expectedErr {
  82. t.Errorf("unexpected read error: %v, expect: %v", err, expectedErr)
  83. }
  84. }
  85. func TestReaderAt0(t *testing.T) {
  86. visibles := []VisibleInterval{
  87. {
  88. start: 2,
  89. stop: 5,
  90. fileId: "1",
  91. chunkSize: 9,
  92. },
  93. {
  94. start: 7,
  95. stop: 9,
  96. fileId: "2",
  97. chunkSize: 9,
  98. },
  99. }
  100. readerAt := &ChunkReadAt{
  101. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  102. lookupFileId: nil,
  103. readerLock: sync.Mutex{},
  104. fileSize: 10,
  105. chunkCache: &mockChunkCache{},
  106. readerPattern: NewReaderPattern(),
  107. }
  108. testReadAt(t, readerAt, 0, 10, 10, io.EOF)
  109. testReadAt(t, readerAt, 3, 16, 7, io.EOF)
  110. testReadAt(t, readerAt, 3, 5, 5, nil)
  111. testReadAt(t, readerAt, 11, 5, 0, io.EOF)
  112. testReadAt(t, readerAt, 10, 5, 0, io.EOF)
  113. }
  114. func TestReaderAt1(t *testing.T) {
  115. visibles := []VisibleInterval{
  116. {
  117. start: 2,
  118. stop: 5,
  119. fileId: "1",
  120. chunkSize: 9,
  121. },
  122. }
  123. readerAt := &ChunkReadAt{
  124. chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
  125. lookupFileId: nil,
  126. readerLock: sync.Mutex{},
  127. fileSize: 20,
  128. chunkCache: &mockChunkCache{},
  129. readerPattern: NewReaderPattern(),
  130. }
  131. testReadAt(t, readerAt, 0, 20, 20, io.EOF)
  132. testReadAt(t, readerAt, 1, 7, 7, nil)
  133. testReadAt(t, readerAt, 0, 1, 1, nil)
  134. testReadAt(t, readerAt, 18, 4, 2, io.EOF)
  135. testReadAt(t, readerAt, 12, 4, 4, nil)
  136. testReadAt(t, readerAt, 4, 20, 16, io.EOF)
  137. testReadAt(t, readerAt, 4, 10, 10, nil)
  138. testReadAt(t, readerAt, 1, 10, 10, nil)
  139. }