1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/common/bufio/writer_test.go

54 lines
1.1 KiB
Go
Raw Normal View History

2016-12-09 07:17:34 -05:00
package bufio_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"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-12-09 07:17:34 -05:00
. "v2ray.com/core/common/bufio"
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
2016-12-09 06:08:25 -05:00
content := buf.New()
2016-02-26 17:45:33 -05:00
2016-12-09 07:17:34 -05:00
writer := NewWriter(content)
2016-12-27 15:41:44 -05:00
assert.Bool(writer.Buffered()).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()
2016-12-27 15:41:44 -05:00
writer.SetBuffered(false)
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)
2016-12-09 06:08:25 -05:00
content := buf.NewLocal(128 * 1024)
2016-12-04 15:34:22 -05:00
2016-12-09 07:17:34 -05:00
writer := NewWriter(content)
2016-12-27 15:41:44 -05:00
assert.Bool(writer.Buffered()).IsTrue()
2016-12-04 15:34:22 -05:00
payload := make([]byte, 64*1024)
rand.Read(payload)
nBytes, err := writer.Write(payload[:1024])
assert.Int(nBytes).Equals(1024)
assert.Error(err).IsNil()
assert.Bool(content.IsEmpty()).IsTrue()
nBytes, err = writer.Write(payload[1024:])
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(63 * 1024)
2016-12-05 09:19:14 -05:00
assert.Bytes(content.Bytes()).Equals(payload)
2016-12-04 15:34:22 -05:00
}