Files
OpenList/pkg/buffer/bytes_test.go
TwoOnefour cbbb5ad231 fix(stream): http chucked upload issue (#1152)
* fix(stream): http chucked upload issue

* fix(stream): use MmapThreshold

* fix(stream): improve caching mechanism and handle size=0 case

* fix bug

* fix(buffer): optimize ReadAt method for improved performance

* fix(upload): handle Content-Length and File-Size headers for better size management

* fix(189pc): 移除重复限速

* fix(upload): handle negative file size during streaming uploads

* fix(upload): update header key from File-Size to X-File-Size for size retrieval

---------

Co-authored-by: j2rong4cn <j2rong@qq.com>
2025-09-15 19:36:16 +08:00

95 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("/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 != int(bs.Size()-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)
}
})
}
}