2016-12-09 07:17:34 -05:00
|
|
|
package buf_test
|
2016-05-11 13:54:20 -04:00
|
|
|
|
|
|
|
import (
|
2017-04-17 16:35:20 -04:00
|
|
|
"bufio"
|
2016-05-11 13:54:20 -04:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2017-04-17 16:35:20 -04:00
|
|
|
"io"
|
2018-04-16 18:40:27 -04:00
|
|
|
"testing"
|
2017-04-17 16:35:20 -04:00
|
|
|
|
2017-11-03 20:33:35 -04:00
|
|
|
"v2ray.com/core/common"
|
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-26 15:44:22 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-05-11 13:54:20 -04:00
|
|
|
)
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
func TestWriter(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-05-11 13:54:20 -04:00
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
lb := New()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(lb.AppendSupplier(ReadFrom(rand.Reader)), IsNil)
|
2016-05-11 13:54:20 -04:00
|
|
|
|
2016-12-06 05:03:42 -05:00
|
|
|
expectedBytes := append([]byte(nil), lb.Bytes()...)
|
2016-05-11 13:54:20 -04:00
|
|
|
|
2016-12-06 05:03:42 -05:00
|
|
|
writeBuffer := bytes.NewBuffer(make([]byte, 0, 1024*1024))
|
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
writer := NewBufferedWriter(NewWriter(writeBuffer))
|
|
|
|
writer.SetBuffered(false)
|
|
|
|
err := writer.WriteMultiBuffer(NewMultiBufferValue(lb))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-11-09 16:33:15 -05:00
|
|
|
assert(writer.Flush(), IsNil)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(expectedBytes, Equals, writeBuffer.Bytes())
|
2016-05-11 13:54:20 -04:00
|
|
|
}
|
2017-04-17 16:35:20 -04:00
|
|
|
|
|
|
|
func TestBytesWriterReadFrom(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2017-04-17 16:35:20 -04:00
|
|
|
|
2017-11-24 17:16:01 -05:00
|
|
|
const size = 50000
|
2018-05-25 06:50:33 -04:00
|
|
|
pReader, pWriter := pipe.New(pipe.WithSizeLimit(size))
|
2017-11-24 17:16:01 -05:00
|
|
|
reader := bufio.NewReader(io.LimitReader(rand.Reader, size))
|
2018-04-16 18:40:27 -04:00
|
|
|
writer := NewBufferedWriter(pWriter)
|
2017-11-09 16:33:15 -05:00
|
|
|
writer.SetBuffered(false)
|
2017-11-24 17:16:01 -05:00
|
|
|
nBytes, err := reader.WriteTo(writer)
|
|
|
|
assert(nBytes, Equals, int64(size))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2017-04-17 16:35:20 -04:00
|
|
|
|
2018-04-16 18:40:27 -04:00
|
|
|
mb, err := pReader.ReadMultiBuffer()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2018-04-02 14:00:50 -04:00
|
|
|
assert(mb.Len(), Equals, int32(size))
|
2017-04-17 16:35:20 -04:00
|
|
|
}
|
2017-11-03 20:33:35 -04:00
|
|
|
|
|
|
|
func TestDiscardBytes(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
b := New()
|
2017-11-09 16:33:15 -05:00
|
|
|
common.Must(b.Reset(ReadFullFrom(rand.Reader, Size)))
|
2017-11-03 20:33:35 -04:00
|
|
|
|
|
|
|
nBytes, err := io.Copy(DiscardBytes, b)
|
|
|
|
assert(nBytes, Equals, int64(Size))
|
|
|
|
assert(err, IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiscardBytesMultiBuffer(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
const size = 10240*1024 + 1
|
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, size))
|
|
|
|
common.Must2(buffer.ReadFrom(io.LimitReader(rand.Reader, size)))
|
|
|
|
|
|
|
|
r := NewReader(buffer)
|
2018-04-20 18:54:53 -04:00
|
|
|
nBytes, err := io.Copy(DiscardBytes, &BufferedReader{Reader: r})
|
2017-11-03 20:33:35 -04:00
|
|
|
assert(nBytes, Equals, int64(size))
|
|
|
|
assert(err, IsNil)
|
|
|
|
}
|
2017-12-03 17:11:29 -05:00
|
|
|
|
|
|
|
func TestWriterInterface(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
assert((*BufferToBytesWriter)(nil), Implements, (*Writer)(nil))
|
|
|
|
assert((*BufferToBytesWriter)(nil), Implements, (*io.Writer)(nil))
|
|
|
|
assert((*BufferToBytesWriter)(nil), Implements, (*io.ReaderFrom)(nil))
|
|
|
|
|
|
|
|
assert((*BufferedWriter)(nil), Implements, (*Writer)(nil))
|
|
|
|
assert((*BufferedWriter)(nil), Implements, (*io.Writer)(nil))
|
|
|
|
assert((*BufferedWriter)(nil), Implements, (*io.ReaderFrom)(nil))
|
|
|
|
assert((*BufferedWriter)(nil), Implements, (*io.ByteWriter)(nil))
|
|
|
|
}
|