1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/common/buf/reader_test.go

97 lines
2.4 KiB
Go
Raw Normal View History

2016-12-09 07:17:34 -05:00
package buf_test
2016-05-11 13:54:20 -04:00
import (
"bytes"
2017-04-18 06:32:24 -04:00
"context"
"io"
2016-05-11 13:54:20 -04:00
"testing"
2016-12-09 07:17:34 -05:00
. "v2ray.com/core/common/buf"
2017-04-18 06:32:24 -04:00
"v2ray.com/core/transport/ray"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2016-05-11 13:54:20 -04:00
)
func TestAdaptiveReader(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-05-11 13:54:20 -04:00
2017-11-08 16:23:21 -05:00
reader := NewReader(bytes.NewReader(make([]byte, 1024*1024)))
2017-11-09 16:33:15 -05:00
b, err := reader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-11-08 16:23:21 -05:00
assert(b.Len(), Equals, 2*1024)
2017-11-09 16:33:15 -05:00
b, err = reader.ReadMultiBuffer()
2017-11-08 16:23:21 -05:00
assert(err, IsNil)
2017-12-08 07:23:15 -05:00
assert(b.Len(), Equals, 8*1024)
b, err = reader.ReadMultiBuffer()
assert(err, IsNil)
2018-03-12 11:21:39 -04:00
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)
assert(b.Len(), Equals, 128*1024)
2016-05-11 13:54:20 -04:00
}
2017-04-18 06:32:24 -04:00
func TestBytesReaderWriteTo(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-18 06:32:24 -04:00
stream := ray.NewStream(context.Background())
2017-11-09 16:33:15 -05:00
reader := NewBufferedReader(stream)
2017-04-18 06:32:24 -04:00
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
2017-11-09 16:33:15 -05:00
assert(stream.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
2017-04-18 06:32:24 -04:00
stream.Close()
stream2 := ray.NewStream(context.Background())
2017-11-09 16:33:15 -05:00
writer := NewBufferedWriter(stream2)
writer.SetBuffered(false)
2017-04-18 06:32:24 -04:00
nBytes, err := io.Copy(writer, reader)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, int64(6))
2017-04-18 06:32:24 -04:00
2017-11-09 16:33:15 -05:00
mb, err := stream2.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(len(mb), Equals, 2)
assert(mb[0].String(), Equals, "abc")
assert(mb[1].String(), Equals, "efg")
2017-04-18 06:32:24 -04:00
}
func TestBytesReaderMultiBuffer(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-04-18 06:32:24 -04:00
stream := ray.NewStream(context.Background())
2017-11-09 16:33:15 -05:00
reader := NewBufferedReader(stream)
2017-04-18 06:32:24 -04:00
b1 := New()
b1.AppendBytes('a', 'b', 'c')
b2 := New()
b2.AppendBytes('e', 'f', 'g')
2017-11-09 16:33:15 -05:00
assert(stream.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
2017-04-18 06:32:24 -04:00
stream.Close()
mbReader := NewReader(reader)
2017-11-09 16:33:15 -05:00
mb, err := mbReader.ReadMultiBuffer()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(len(mb), Equals, 2)
assert(mb[0].String(), Equals, "abc")
assert(mb[1].String(), Equals, "efg")
2017-04-18 06:32:24 -04:00
}
2017-12-03 17:11:29 -05:00
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))
}