2016-12-09 07:17:34 -05:00
|
|
|
package buf_test
|
2016-05-11 13:54:20 -04:00
|
|
|
|
|
|
|
import (
|
2017-04-18 06:32:24 -04:00
|
|
|
"io"
|
2016-05-11 13:54:20 -04:00
|
|
|
"testing"
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
. "v2ray.com/core/common/buf"
|
2018-04-16 18:40:27 -04:00
|
|
|
"v2ray.com/core/transport/pipe"
|
2017-10-24 10:15:35 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
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
|
|
|
|
2018-05-25 06:50:33 -04:00
|
|
|
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
|
2018-04-20 18:54:53 -04:00
|
|
|
reader := &BufferedReader{Reader: pReader}
|
2017-04-18 06:32:24 -04:00
|
|
|
b1 := New()
|
2018-07-30 16:45:06 -04:00
|
|
|
b1.WriteBytes('a', 'b', 'c')
|
2017-04-18 06:32:24 -04:00
|
|
|
b2 := New()
|
2018-07-30 16:45:06 -04:00
|
|
|
b2.WriteBytes('e', 'f', 'g')
|
2018-04-16 18:40:27 -04:00
|
|
|
assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
|
|
|
|
pWriter.Close()
|
2017-04-18 06:32:24 -04:00
|
|
|
|
2018-05-25 06:50:33 -04:00
|
|
|
pReader2, pWriter2 := pipe.New(pipe.WithSizeLimit(1024))
|
2018-04-16 18:40:27 -04:00
|
|
|
writer := NewBufferedWriter(pWriter2)
|
2017-11-09 16:33:15 -05:00
|
|
|
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
|
|
|
|
2018-04-16 18:40:27 -04:00
|
|
|
mb, err := pReader2.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
|
|
|
|
2018-05-25 06:50:33 -04:00
|
|
|
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
|
2018-04-20 18:54:53 -04:00
|
|
|
reader := &BufferedReader{Reader: pReader}
|
2017-04-18 06:32:24 -04:00
|
|
|
b1 := New()
|
2018-07-30 16:45:06 -04:00
|
|
|
b1.WriteBytes('a', 'b', 'c')
|
2017-04-18 06:32:24 -04:00
|
|
|
b2 := New()
|
2018-07-30 16:45:06 -04:00
|
|
|
b2.WriteBytes('e', 'f', 'g')
|
2018-04-16 18:40:27 -04:00
|
|
|
assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
|
|
|
|
pWriter.Close()
|
2017-04-18 06:32:24 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-08-25 04:41:39 -04:00
|
|
|
assert((*ReadVReader)(nil), Implements, (*io.Reader)(nil))
|
|
|
|
assert((*ReadVReader)(nil), Implements, (*Reader)(nil))
|
2017-12-03 17:11:29 -05:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|