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

71 lines
1.6 KiB

package buf_test
import (
"bytes"
8 years ago
"context"
"io"
"testing"
. "v2ray.com/core/common/buf"
"v2ray.com/core/testing/assert"
8 years ago
"v2ray.com/core/transport/ray"
)
func TestAdaptiveReader(t *testing.T) {
9 years ago
assert := assert.On(t)
rawContent := make([]byte, 1024*1024)
buffer := bytes.NewBuffer(rawContent)
reader := NewReader(buffer)
8 years ago
b, err := reader.Read()
assert.Error(err).IsNil()
8 years ago
assert.Int(b.Len()).Equals(32 * 1024)
}
8 years ago
func TestBytesReaderWriteTo(t *testing.T) {
assert := assert.On(t)
stream := ray.NewStream(context.Background())
reader := ToBytesReader(stream)
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
assert.Error(stream.Write(NewMultiBufferValue(b1, b2))).IsNil()
stream.Close()
stream2 := ray.NewStream(context.Background())
writer := ToBytesWriter(stream2)
nBytes, err := io.Copy(writer, reader)
assert.Error(err).IsNil()
assert.Int64(nBytes).Equals(6)
mb, err := stream2.Read()
assert.Error(err).IsNil()
assert.Int(len(mb)).Equals(2)
assert.String(mb[0].String()).Equals("abc")
assert.String(mb[1].String()).Equals("efg")
}
func TestBytesReaderMultiBuffer(t *testing.T) {
assert := assert.On(t)
stream := ray.NewStream(context.Background())
reader := ToBytesReader(stream)
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
assert.Error(stream.Write(NewMultiBufferValue(b1, b2))).IsNil()
stream.Close()
mbReader := NewReader(reader)
mb, err := mbReader.Read()
assert.Error(err).IsNil()
assert.Int(len(mb)).Equals(2)
assert.String(mb[0].String()).Equals("abc")
assert.String(mb[1].String()).Equals("efg")
}