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