reader_at_test.go 3.0 KB

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