v2ray-core/common/io/buffered_writer_test.go

54 lines
1.1 KiB
Go
Raw Normal View History

2016-02-26 23:45:33 +01:00
package io_test
import (
2016-12-04 21:34:22 +01:00
"crypto/rand"
2016-02-26 23:45:33 +01:00
"testing"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/alloc"
. "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert"
2016-02-26 23:45:33 +01:00
)
func TestBufferedWriter(t *testing.T) {
2016-05-24 21:55:46 +02:00
assert := assert.On(t)
2016-02-26 23:45:33 +01:00
2016-12-06 11:03:42 +01:00
content := alloc.NewBuffer()
2016-02-26 23:45:33 +01:00
writer := NewBufferedWriter(content)
assert.Bool(writer.Cached()).IsTrue()
payload := make([]byte, 16)
nBytes, err := writer.Write(payload)
assert.Int(nBytes).Equals(16)
assert.Error(err).IsNil()
assert.Bool(content.IsEmpty()).IsTrue()
writer.SetCached(false)
assert.Int(content.Len()).Equals(16)
}
2016-12-04 21:34:22 +01:00
func TestBufferedWriterLargePayload(t *testing.T) {
assert := assert.On(t)
2016-12-06 11:03:42 +01:00
content := alloc.NewLocalBuffer(128 * 1024)
2016-12-04 21:34:22 +01:00
writer := NewBufferedWriter(content)
assert.Bool(writer.Cached()).IsTrue()
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 15:19:14 +01:00
assert.Bytes(content.Bytes()).Equals(payload)
2016-12-04 21:34:22 +01:00
}