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