1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-18 01:46:06 -04:00
v2fly/common/buf/buffered_writer_test.go

53 lines
1.1 KiB
Go
Raw Normal View History

2017-02-15 16:51:01 -05:00
package buf_test
2016-02-26 17:45:33 -05:00
import (
2016-12-04 15:34:22 -05:00
"crypto/rand"
2016-02-26 17:45:33 -05:00
"testing"
2017-02-15 16:51:01 -05:00
. "v2ray.com/core/common/buf"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/testing/assert"
2016-02-26 17:45:33 -05:00
)
func TestBufferedWriter(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-02-26 17:45:33 -05:00
2017-02-15 16:51:01 -05:00
content := New()
2016-02-26 17:45:33 -05:00
2017-02-15 16:51:01 -05:00
writer := NewBufferedWriter(content)
2016-12-27 17:09:08 -05:00
assert.Bool(writer.IsBuffered()).IsTrue()
2016-02-26 17:45:33 -05:00
payload := make([]byte, 16)
nBytes, err := writer.Write(payload)
assert.Int(nBytes).Equals(16)
assert.Error(err).IsNil()
assert.Bool(content.IsEmpty()).IsTrue()
2017-02-20 04:33:35 -05:00
assert.Error(writer.SetBuffered(false)).IsNil()
2016-02-26 17:45:33 -05:00
assert.Int(content.Len()).Equals(16)
}
2016-12-04 15:34:22 -05:00
func TestBufferedWriterLargePayload(t *testing.T) {
assert := assert.On(t)
2017-02-15 16:51:01 -05:00
content := NewLocal(128 * 1024)
2016-12-04 15:34:22 -05:00
2017-02-15 16:51:01 -05:00
writer := NewBufferedWriter(content)
2016-12-27 17:09:08 -05:00
assert.Bool(writer.IsBuffered()).IsTrue()
2016-12-04 15:34:22 -05:00
payload := make([]byte, 64*1024)
rand.Read(payload)
2017-01-04 06:52:24 -05:00
nBytes, err := writer.Write(payload[:512])
assert.Int(nBytes).Equals(512)
2016-12-04 15:34:22 -05:00
assert.Error(err).IsNil()
assert.Bool(content.IsEmpty()).IsTrue()
2017-01-04 06:52:24 -05:00
nBytes, err = writer.Write(payload[512:])
2016-12-04 15:34:22 -05:00
assert.Error(err).IsNil()
2017-01-04 06:52:24 -05:00
assert.Int(nBytes).Equals(64*1024 - 512)
2016-12-05 09:19:14 -05:00
assert.Bytes(content.Bytes()).Equals(payload)
2016-12-04 15:34:22 -05:00
}