2016-07-25 15:43:31 -04:00
|
|
|
package protocol_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
. "v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/testing/assert"
|
2016-07-25 15:43:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServerList(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
list := NewServerList()
|
2016-11-02 11:41:02 -04:00
|
|
|
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid()))
|
2016-07-25 15:43:31 -04:00
|
|
|
assert.Uint32(list.Size()).Equals(1)
|
2016-11-02 11:41:02 -04:00
|
|
|
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
|
2016-07-25 15:43:31 -04:00
|
|
|
assert.Uint32(list.Size()).Equals(2)
|
|
|
|
|
|
|
|
server := list.GetServer(1)
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(2)
|
2016-07-25 15:43:31 -04:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
server = list.GetServer(1)
|
|
|
|
assert.Pointer(server).IsNil()
|
|
|
|
|
|
|
|
server = list.GetServer(0)
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(1)
|
2016-07-25 15:43:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerPicker(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
list := NewServerList()
|
2016-11-02 11:41:02 -04:00
|
|
|
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid()))
|
|
|
|
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
|
|
|
|
list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(3)), BeforeTime(time.Now().Add(time.Second))))
|
2016-07-25 15:43:31 -04:00
|
|
|
|
|
|
|
picker := NewRoundRobinServerPicker(list)
|
|
|
|
server := picker.PickServer()
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(1)
|
2016-07-25 15:43:31 -04:00
|
|
|
server = picker.PickServer()
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(2)
|
2016-07-25 15:43:31 -04:00
|
|
|
server = picker.PickServer()
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(3)
|
2016-07-25 15:43:57 -04:00
|
|
|
server = picker.PickServer()
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(1)
|
2016-07-25 15:43:31 -04:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
server = picker.PickServer()
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(1)
|
2016-07-25 15:43:31 -04:00
|
|
|
server = picker.PickServer()
|
2016-09-20 05:53:05 -04:00
|
|
|
assert.Port(server.Destination().Port).Equals(1)
|
2016-07-25 15:43:31 -04:00
|
|
|
}
|