You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/buf/reader_test.go

97 lines
2.4 KiB

package buf_test
import (
"bytes"
8 years ago
"context"
"io"
"testing"
. "v2ray.com/core/common/buf"
8 years ago
"v2ray.com/core/transport/ray"
. "v2ray.com/ext/assert"
)
func TestAdaptiveReader(t *testing.T) {
assert := With(t)
reader := NewReader(bytes.NewReader(make([]byte, 1024*1024)))
b, err := reader.ReadMultiBuffer()
assert(err, IsNil)
assert(b.Len(), Equals, 2*1024)
b, err = reader.ReadMultiBuffer()
assert(err, IsNil)
assert(b.Len(), Equals, 8*1024)
b, err = reader.ReadMultiBuffer()
assert(err, IsNil)
assert(b.Len(), Equals, 32*1024)
b, err = reader.ReadMultiBuffer()
assert(err, IsNil)
assert(b.Len(), Equals, 128*1024)
b, err = reader.ReadMultiBuffer()
assert(err, IsNil)
7 years ago
assert(b.Len(), Equals, 512*1024)
}
8 years ago
func TestBytesReaderWriteTo(t *testing.T) {
assert := With(t)
8 years ago
stream := ray.NewStream(context.Background())
reader := NewBufferedReader(stream)
8 years ago
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
assert(stream.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
8 years ago
stream.Close()
stream2 := ray.NewStream(context.Background())
writer := NewBufferedWriter(stream2)
writer.SetBuffered(false)
8 years ago
nBytes, err := io.Copy(writer, reader)
assert(err, IsNil)
assert(nBytes, Equals, int64(6))
8 years ago
mb, err := stream2.ReadMultiBuffer()
assert(err, IsNil)
assert(len(mb), Equals, 2)
assert(mb[0].String(), Equals, "abc")
assert(mb[1].String(), Equals, "efg")
8 years ago
}
func TestBytesReaderMultiBuffer(t *testing.T) {
assert := With(t)
8 years ago
stream := ray.NewStream(context.Background())
reader := NewBufferedReader(stream)
8 years ago
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
assert(stream.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
8 years ago
stream.Close()
mbReader := NewReader(reader)
mb, err := mbReader.ReadMultiBuffer()
assert(err, IsNil)
assert(len(mb), Equals, 2)
assert(mb[0].String(), Equals, "abc")
assert(mb[1].String(), Equals, "efg")
8 years ago
}
func TestReaderInterface(t *testing.T) {
assert := With(t)
assert((*BytesToBufferReader)(nil), Implements, (*io.Reader)(nil))
assert((*BytesToBufferReader)(nil), Implements, (*Reader)(nil))
assert((*BufferedReader)(nil), Implements, (*Reader)(nil))
assert((*BufferedReader)(nil), Implements, (*io.Reader)(nil))
assert((*BufferedReader)(nil), Implements, (*io.ByteReader)(nil))
assert((*BufferedReader)(nil), Implements, (*io.WriterTo)(nil))
}