v2ray-core/transport/internet/kcp/receiving_test.go

57 lines
1.4 KiB
Go
Raw Normal View History

2016-06-25 19:35:18 +00:00
package kcp_test
import (
"testing"
2016-07-02 21:37:51 +00:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-06-25 19:35:18 +00: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 08:34:34 +00:00
seg0 := &DataSegment{}
seg1 := &DataSegment{}
seg2 := &DataSegment{}
seg3 := &DataSegment{}
2016-06-25 19:35:18 +00:00
assert.Bool(window.Set(0, seg0)).IsTrue()
assert.Pointer(window.RemoveFirst()).Equals(seg0)
2016-06-27 07:03:05 +00:00
e := window.RemoveFirst()
if e != nil {
assert.Fail("Expecting nil.")
}
2016-06-25 19:35:18 +00: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 21:37:51 +00:00
func TestRecivingQueue(t *testing.T) {
assert := assert.On(t)
queue := NewReceivingQueue(2)
2016-07-28 14:24:15 +00:00
queue.Put(alloc.NewLocalBuffer(512).Clear().AppendString("abcd"))
queue.Put(alloc.NewLocalBuffer(512).Clear().AppendString("efg"))
2016-07-06 15:34:38 +00:00
assert.Bool(queue.IsFull()).IsTrue()
2016-07-02 21:37:51 +00:00
b := make([]byte, 1024)
2016-07-06 15:34:38 +00:00
nBytes := queue.Read(b)
2016-07-02 21:37:51 +00:00
assert.Int(nBytes).Equals(7)
assert.String(string(b[:nBytes])).Equals("abcdefg")
2016-07-28 14:24:15 +00:00
queue.Put(alloc.NewLocalBuffer(512).Clear().AppendString("1"))
2016-07-02 21:37:51 +00:00
queue.Close()
2016-07-06 15:34:38 +00:00
nBytes = queue.Read(b)
assert.Int(nBytes).Equals(0)
2016-07-02 21:37:51 +00:00
}