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.

161 lines
3.1 KiB

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