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"
|
2017-10-24 14:15:35 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-02-26 22:45:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBufferedWriter(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(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)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(writer.IsBuffered(), IsTrue)
|
2016-02-26 22:45:33 +00:00
|
|
|
|
|
|
|
payload := make([]byte, 16)
|
|
|
|
|
|
|
|
nBytes, err := writer.Write(payload)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(nBytes, Equals, 16)
|
|
|
|
assert(err, IsNil)
|
2016-02-26 22:45:33 +00:00
|
|
|
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(content.IsEmpty(), IsTrue)
|
2016-02-26 22:45:33 +00:00
|
|
|
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(writer.SetBuffered(false), IsNil)
|
|
|
|
assert(content.Len(), Equals, 16)
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
2016-12-04 20:34:22 +00:00
|
|
|
|
|
|
|
func TestBufferedWriterLargePayload(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2016-12-04 20:34:22 +00:00
|
|
|
|
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)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(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])
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(nBytes, Equals, 512)
|
|
|
|
assert(err, IsNil)
|
2016-12-04 20:34:22 +00:00
|
|
|
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(content.IsEmpty(), IsTrue)
|
2016-12-04 20:34:22 +00:00
|
|
|
|
2017-01-04 11:52:24 +00:00
|
|
|
nBytes, err = writer.Write(payload[512:])
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(writer.Flush(), IsNil)
|
2017-10-26 19:44:22 +00:00
|
|
|
assert(nBytes, Equals, 64*1024-512)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(content.Bytes(), Equals, payload)
|
2016-12-04 20:34:22 +00:00
|
|
|
}
|