1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/buf/reader_test.go
2017-11-09 22:33:15 +01:00

73 lines
1.6 KiB
Go

package buf_test
import (
"bytes"
"context"
"io"
"testing"
. "v2ray.com/core/common/buf"
"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, 32*1024)
}
func TestBytesReaderWriteTo(t *testing.T) {
assert := With(t)
stream := ray.NewStream(context.Background())
reader := NewBufferedReader(stream)
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
assert(stream.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
stream.Close()
stream2 := ray.NewStream(context.Background())
writer := NewBufferedWriter(stream2)
writer.SetBuffered(false)
nBytes, err := io.Copy(writer, reader)
assert(err, IsNil)
assert(nBytes, Equals, int64(6))
mb, err := stream2.ReadMultiBuffer()
assert(err, IsNil)
assert(len(mb), Equals, 2)
assert(mb[0].String(), Equals, "abc")
assert(mb[1].String(), Equals, "efg")
}
func TestBytesReaderMultiBuffer(t *testing.T) {
assert := With(t)
stream := ray.NewStream(context.Background())
reader := NewBufferedReader(stream)
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
assert(stream.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
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")
}