rangereader_test.go 801 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package filex
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/fs"
  7. )
  8. func TestRangeReader(t *testing.T) {
  9. const text = `hello
  10. world`
  11. file, err := fs.TempFileWithText(text)
  12. assert.Nil(t, err)
  13. defer func() {
  14. file.Close()
  15. os.Remove(file.Name())
  16. }()
  17. reader := NewRangeReader(file, 5, 8)
  18. buf := make([]byte, 10)
  19. n, err := reader.Read(buf)
  20. assert.Nil(t, err)
  21. assert.Equal(t, 3, n)
  22. assert.Equal(t, `
  23. wo`, string(buf[:n]))
  24. }
  25. func TestRangeReader_OutOfRange(t *testing.T) {
  26. const text = `hello
  27. world`
  28. file, err := fs.TempFileWithText(text)
  29. assert.Nil(t, err)
  30. defer func() {
  31. file.Close()
  32. os.Remove(file.Name())
  33. }()
  34. reader := NewRangeReader(file, 50, 8)
  35. buf := make([]byte, 10)
  36. _, err = reader.Read(buf)
  37. assert.NotNil(t, err)
  38. }