seaweedfs/weed/filer/reader_at_test.go

165 lines
3.2 KiB
Go
Raw Normal View History

2020-09-01 15:21:19 +08:00
package filer
2020-08-18 12:17:32 +08:00
import (
"fmt"
"io"
"math"
"strconv"
"sync"
"testing"
)
type mockChunkCache struct {
}
func (m *mockChunkCache) GetChunk(fileId string, minSize uint64) (data []byte) {
x, _ := strconv.Atoi(fileId)
data = make([]byte, minSize)
for i := 0; i < int(minSize); i++ {
data[i] = byte(x)
}
return data
}
2021-05-06 18:37:51 +08:00
func (m *mockChunkCache) GetChunkSlice(fileId string, offset, length uint64) []byte {
2021-12-23 08:05:08 +08:00
return m.GetChunk(fileId, length)
}
2020-08-18 12:17:32 +08:00
func (m *mockChunkCache) SetChunk(fileId string, data []byte) {
}
func TestReaderAt(t *testing.T) {
visibles := []VisibleInterval{
{
2020-08-30 13:28:33 +08:00
start: 1,
stop: 2,
fileId: "1",
2020-08-18 12:17:32 +08:00
chunkSize: 9,
},
{
2020-08-30 13:28:33 +08:00
start: 3,
stop: 4,
fileId: "3",
2020-08-18 12:17:32 +08:00
chunkSize: 1,
},
{
2020-08-30 13:28:33 +08:00
start: 5,
stop: 6,
fileId: "5",
2020-08-18 12:17:32 +08:00
chunkSize: 2,
},
{
2020-08-30 13:28:33 +08:00
start: 7,
stop: 9,
fileId: "7",
2020-08-18 12:17:32 +08:00
chunkSize: 2,
},
{
2020-08-30 13:28:33 +08:00
start: 9,
stop: 10,
fileId: "9",
2020-08-18 12:17:32 +08:00
chunkSize: 2,
},
}
readerAt := &ChunkReadAt{
2021-12-23 08:05:08 +08:00
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
lookupFileId: nil,
readerLock: sync.Mutex{},
fileSize: 10,
chunkCache: &mockChunkCache{},
readerPattern: NewReaderPattern(),
2020-08-18 12:17:32 +08:00
}
2020-10-15 03:27:52 +08:00
testReadAt(t, readerAt, 0, 10, 10, io.EOF)
2020-08-18 12:17:32 +08:00
testReadAt(t, readerAt, 0, 12, 10, io.EOF)
2020-10-15 03:27:52 +08:00
testReadAt(t, readerAt, 2, 8, 8, io.EOF)
2020-08-18 12:17:32 +08:00
testReadAt(t, readerAt, 3, 6, 6, nil)
}
func testReadAt(t *testing.T, readerAt *ChunkReadAt, offset int64, size int, expected int, expectedErr error) {
data := make([]byte, size)
n, err := readerAt.ReadAt(data, offset)
2020-08-18 13:46:32 +08:00
for _, d := range data {
fmt.Printf("%x", d)
}
fmt.Println()
2020-08-18 12:17:32 +08:00
if expected != n {
t.Errorf("unexpected read size: %d, expect: %d", n, expected)
}
if err != expectedErr {
t.Errorf("unexpected read error: %v, expect: %v", err, expectedErr)
}
}
func TestReaderAt0(t *testing.T) {
visibles := []VisibleInterval{
{
2020-08-30 13:28:33 +08:00
start: 2,
stop: 5,
fileId: "1",
2020-08-18 12:17:32 +08:00
chunkSize: 9,
},
{
2020-08-30 13:28:33 +08:00
start: 7,
stop: 9,
fileId: "2",
2020-08-18 12:17:32 +08:00
chunkSize: 9,
},
}
readerAt := &ChunkReadAt{
2021-12-23 08:05:08 +08:00
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
lookupFileId: nil,
readerLock: sync.Mutex{},
fileSize: 10,
chunkCache: &mockChunkCache{},
readerPattern: NewReaderPattern(),
2020-08-18 12:17:32 +08:00
}
2020-10-15 03:27:52 +08:00
testReadAt(t, readerAt, 0, 10, 10, io.EOF)
2020-08-18 12:17:32 +08:00
testReadAt(t, readerAt, 3, 16, 7, io.EOF)
testReadAt(t, readerAt, 3, 5, 5, nil)
2020-08-18 13:46:32 +08:00
testReadAt(t, readerAt, 11, 5, 0, io.EOF)
2020-08-18 13:47:27 +08:00
testReadAt(t, readerAt, 10, 5, 0, io.EOF)
2020-08-18 13:46:32 +08:00
2020-08-18 12:17:32 +08:00
}
2020-08-18 15:32:01 +08:00
func TestReaderAt1(t *testing.T) {
visibles := []VisibleInterval{
{
2020-08-30 13:28:33 +08:00
start: 2,
stop: 5,
fileId: "1",
2020-08-18 15:32:01 +08:00
chunkSize: 9,
},
}
readerAt := &ChunkReadAt{
2021-12-23 08:05:08 +08:00
chunkViews: ViewFromVisibleIntervals(visibles, 0, math.MaxInt64),
lookupFileId: nil,
readerLock: sync.Mutex{},
fileSize: 20,
chunkCache: &mockChunkCache{},
readerPattern: NewReaderPattern(),
2020-08-18 15:32:01 +08:00
}
2020-10-15 03:27:52 +08:00
testReadAt(t, readerAt, 0, 20, 20, io.EOF)
2020-08-18 15:32:01 +08:00
testReadAt(t, readerAt, 1, 7, 7, nil)
testReadAt(t, readerAt, 0, 1, 1, nil)
testReadAt(t, readerAt, 18, 4, 2, io.EOF)
testReadAt(t, readerAt, 12, 4, 4, nil)
testReadAt(t, readerAt, 4, 20, 16, io.EOF)
testReadAt(t, readerAt, 4, 10, 10, nil)
testReadAt(t, readerAt, 1, 10, 10, nil)
}