mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-20 04:36:09 +08:00

* perf(stream): improve file stream range reading and caching mechanism * 。 * add bytes_test.go * fix(stream): handle EOF and buffer reading more gracefully * 注释 * refactor: update CacheFullAndWriter to accept pointer for UpdateProgress * update tests * Update drivers/google_drive/util.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: j2rong4cn <36783515+j2rong4cn@users.noreply.github.com> * 更优雅的克隆Link * 修复stream已缓存但无法重复读取 * 将Bytes类型重命名为Reader * 修复栈溢出 * update tests --------- Signed-off-by: j2rong4cn <36783515+j2rong4cn@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
96 lines
1.8 KiB
Go
96 lines
1.8 KiB
Go
package buffer
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
func TestReader_ReadAt(t *testing.T) {
|
|
type args struct {
|
|
p []byte
|
|
off int64
|
|
}
|
|
bs := &Reader{}
|
|
bs.Append([]byte("github.com"))
|
|
bs.Append([]byte("/"))
|
|
bs.Append([]byte("OpenList"))
|
|
bs.Append([]byte("Team/"))
|
|
bs.Append([]byte("OpenList"))
|
|
tests := []struct {
|
|
name string
|
|
b *Reader
|
|
args args
|
|
want func(a args, n int, err error) error
|
|
}{
|
|
{
|
|
name: "readAt len 10 offset 0",
|
|
b: bs,
|
|
args: args{
|
|
p: make([]byte, 10),
|
|
off: 0,
|
|
},
|
|
want: func(a args, n int, err error) error {
|
|
if n != len(a.p) {
|
|
return errors.New("read length not match")
|
|
}
|
|
if string(a.p) != "github.com" {
|
|
return errors.New("read content not match")
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
name: "readAt len 12 offset 11",
|
|
b: bs,
|
|
args: args{
|
|
p: make([]byte, 12),
|
|
off: 11,
|
|
},
|
|
want: func(a args, n int, err error) error {
|
|
if n != len(a.p) {
|
|
return errors.New("read length not match")
|
|
}
|
|
if string(a.p) != "OpenListTeam" {
|
|
return errors.New("read content not match")
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
name: "readAt len 50 offset 24",
|
|
b: bs,
|
|
args: args{
|
|
p: make([]byte, 50),
|
|
off: 24,
|
|
},
|
|
want: func(a args, n int, err error) error {
|
|
if n != bs.Len()-int(a.off) {
|
|
return errors.New("read length not match")
|
|
}
|
|
if string(a.p[:n]) != "OpenList" {
|
|
return errors.New("read content not match")
|
|
}
|
|
if err != io.EOF {
|
|
return errors.New("expect eof")
|
|
}
|
|
return nil
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tt.b.ReadAt(tt.args.p, tt.args.off)
|
|
if err := tt.want(tt.args, got, err); err != nil {
|
|
t.Errorf("Bytes.ReadAt() error = %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|