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

87 lines
2.2 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 (
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
2018-04-16 18:40:27 -04:00
pReader, pWriter := pipe.New()
const size = 50000
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)
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)
2017-11-09 16:33:15 -05:00
nBytes, err := io.Copy(DiscardBytes, NewBufferedReader(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))
}