2016-12-09 12:17:34 +00:00
|
|
|
package buf_test
|
2016-05-11 17:54:20 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-17 20:35:20 +00:00
|
|
|
"bufio"
|
2016-05-11 17:54:20 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-17 20:35:20 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
. "v2ray.com/core/common/buf"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/testing/assert"
|
2017-04-17 20:35:20 +00:00
|
|
|
"v2ray.com/core/transport/ray"
|
2016-05-11 17:54:20 +00:00
|
|
|
)
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
func TestWriter(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2016-05-11 17:54:20 +00:00
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
lb := New()
|
2016-12-21 14:37:16 +00:00
|
|
|
assert.Error(lb.AppendSupplier(ReadFrom(rand.Reader))).IsNil()
|
2016-05-11 17:54:20 +00:00
|
|
|
|
2016-12-06 10:03:42 +00:00
|
|
|
expectedBytes := append([]byte(nil), lb.Bytes()...)
|
2016-05-11 17:54:20 +00:00
|
|
|
|
2016-12-06 10:03:42 +00:00
|
|
|
writeBuffer := bytes.NewBuffer(make([]byte, 0, 1024*1024))
|
|
|
|
|
2017-02-15 21:51:01 +00:00
|
|
|
writer := NewWriter(NewBufferedWriter(writeBuffer))
|
2017-04-15 19:07:23 +00:00
|
|
|
err := writer.Write(NewMultiBufferValue(lb))
|
2016-05-11 17:54:20 +00:00
|
|
|
assert.Error(err).IsNil()
|
2016-12-06 10:03:42 +00:00
|
|
|
assert.Bytes(expectedBytes).Equals(writeBuffer.Bytes())
|
2016-05-11 17:54:20 +00:00
|
|
|
}
|
2017-04-17 20:35:20 +00:00
|
|
|
|
|
|
|
func TestBytesWriterReadFrom(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
cache := ray.NewStream(context.Background())
|
|
|
|
reader := bufio.NewReader(io.LimitReader(rand.Reader, 8192))
|
|
|
|
_, err := reader.WriteTo(ToBytesWriter(cache))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
mb, err := cache.Read()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(mb.Len()).Equals(8192)
|
|
|
|
assert.Int(len(mb)).Equals(4)
|
|
|
|
}
|