1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 10:26:01 -05:00
v2fly/transport/internet/kcp/receiving_test.go

77 lines
2.0 KiB
Go
Raw Normal View History

2016-06-25 15:35:18 -04:00
package kcp_test
import (
2016-07-02 17:37:51 -04:00
"io"
2016-06-25 15:35:18 -04:00
"testing"
2016-07-02 17:37:51 -04:00
"time"
2016-06-25 15:35:18 -04:00
2016-07-02 17:37:51 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-06-25 15:35:18 -04:00
"github.com/v2ray/v2ray-core/testing/assert"
. "github.com/v2ray/v2ray-core/transport/internet/kcp"
)
func TestRecivingWindow(t *testing.T) {
assert := assert.On(t)
window := NewReceivingWindow(3)
2016-06-29 04:34:34 -04:00
seg0 := &DataSegment{}
seg1 := &DataSegment{}
seg2 := &DataSegment{}
seg3 := &DataSegment{}
2016-06-25 15:35:18 -04:00
assert.Bool(window.Set(0, seg0)).IsTrue()
assert.Pointer(window.RemoveFirst()).Equals(seg0)
2016-06-27 03:03:05 -04:00
e := window.RemoveFirst()
if e != nil {
assert.Fail("Expecting nil.")
}
2016-06-25 15:35:18 -04:00
assert.Bool(window.Set(1, seg1)).IsTrue()
assert.Bool(window.Set(2, seg2)).IsTrue()
window.Advance()
assert.Bool(window.Set(2, seg3)).IsTrue()
assert.Pointer(window.RemoveFirst()).Equals(seg1)
assert.Pointer(window.Remove(1)).Equals(seg2)
assert.Pointer(window.Remove(2)).Equals(seg3)
}
2016-07-02 17:37:51 -04:00
func TestRecivingQueue(t *testing.T) {
assert := assert.On(t)
queue := NewReceivingQueue(2)
assert.Bool(queue.Put(alloc.NewSmallBuffer().Clear().AppendString("abcd"))).IsTrue()
assert.Bool(queue.Put(alloc.NewSmallBuffer().Clear().AppendString("efg"))).IsTrue()
assert.Bool(queue.Put(alloc.NewSmallBuffer().Clear().AppendString("more content"))).IsFalse()
b := make([]byte, 1024)
nBytes, err := queue.Read(b)
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(7)
assert.String(string(b[:nBytes])).Equals("abcdefg")
assert.Bool(queue.Put(alloc.NewSmallBuffer().Clear().AppendString("1"))).IsTrue()
queue.Close()
nBytes, err = queue.Read(b)
assert.Error(err).Equals(io.EOF)
}
func TestRecivingQueueTimeout(t *testing.T) {
assert := assert.On(t)
queue := NewReceivingQueue(2)
assert.Bool(queue.Put(alloc.NewSmallBuffer().Clear().AppendString("abcd"))).IsTrue()
queue.SetReadDeadline(time.Now().Add(time.Second))
b := make([]byte, 1024)
nBytes, err := queue.Read(b)
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(4)
assert.String(string(b[:nBytes])).Equals("abcd")
nBytes, err = queue.Read(b)
assert.Error(err).IsNotNil()
}