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

96 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()
2018-11-02 10:01:33 -04:00
common.Must2(lb.ReadFrom(rand.Reader))
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)
2018-11-16 05:08:12 -05:00
err := writer.WriteMultiBuffer(MultiBuffer{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
const size = 50000
2018-05-25 06:50:33 -04:00
pReader, pWriter := pipe.New(pipe.WithSizeLimit(size))
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))
2018-10-24 06:02:02 -04:00
if err != nil {
t.Fatal("expect success, but actually error: ", err.Error())
}
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()
2018-11-02 10:01:33 -04:00
common.Must2(b.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) {
2018-07-31 07:43:27 -04:00
{
var writer interface{} = (*BufferToBytesWriter)(nil)
switch writer.(type) {
case Writer, io.Writer, io.ReaderFrom:
default:
t.Error("BufferToBytesWriter is not Writer, io.Writer or io.ReaderFrom")
}
}
{
var writer interface{} = (*BufferedWriter)(nil)
switch writer.(type) {
case Writer, io.Writer, io.ReaderFrom, io.ByteWriter:
default:
t.Error("BufferedWriter is not Writer, io.Writer, io.ReaderFrom or io.ByteWriter")
}
}
2017-12-03 17:11:29 -05:00
}