You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/filex/rangereader_test.go

45 lines
802 B
Go

4 years ago
package filex
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/fs"
4 years ago
)
func TestRangeReader(t *testing.T) {
const text = `hello
world`
file, err := fs.TempFileWithText(text)
assert.Nil(t, err)
defer func() {
file.Close()
os.Remove(file.Name())
}()
reader := NewRangeReader(file, 5, 8)
buf := make([]byte, 10)
n, err := reader.Read(buf)
assert.Nil(t, err)
assert.Equal(t, 3, n)
assert.Equal(t, `
wo`, string(buf[:n]))
}
func TestRangeReader_OutOfRange(t *testing.T) {
const text = `hello
world`
file, err := fs.TempFileWithText(text)
assert.Nil(t, err)
defer func() {
file.Close()
os.Remove(file.Name())
}()
reader := NewRangeReader(file, 50, 8)
buf := make([]byte, 10)
_, err = reader.Read(buf)
assert.NotNil(t, err)
}