2016-07-25 19:43:31 +00:00
|
|
|
package protocol_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 18:55:45 +00:00
|
|
|
. "v2ray.com/core/common/protocol"
|
2017-10-24 14:15:35 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-07-25 19:43:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServerList(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2016-07-25 19:43:31 +00:00
|
|
|
|
|
|
|
list := NewServerList()
|
2017-08-29 10:56:57 +00:00
|
|
|
list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(1)), AlwaysValid()))
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(list.Size(), Equals, uint32(1))
|
2017-08-29 10:56:57 +00:00
|
|
|
list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(list.Size(), Equals, uint32(2))
|
2016-07-25 19:43:31 +00:00
|
|
|
|
|
|
|
server := list.GetServer(1)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(2))
|
2016-07-25 19:43:31 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
server = list.GetServer(1)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server, IsNil)
|
2016-07-25 19:43:31 +00:00
|
|
|
|
|
|
|
server = list.GetServer(0)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(1))
|
2016-07-25 19:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerPicker(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2016-07-25 19:43:31 +00:00
|
|
|
|
|
|
|
list := NewServerList()
|
2017-08-29 10:56:57 +00:00
|
|
|
list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(1)), AlwaysValid()))
|
|
|
|
list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
|
|
|
|
list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(3)), BeforeTime(time.Now().Add(time.Second))))
|
2016-07-25 19:43:31 +00:00
|
|
|
|
|
|
|
picker := NewRoundRobinServerPicker(list)
|
|
|
|
server := picker.PickServer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(1))
|
2016-07-25 19:43:31 +00:00
|
|
|
server = picker.PickServer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(2))
|
2016-07-25 19:43:31 +00:00
|
|
|
server = picker.PickServer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(3))
|
2016-07-25 19:43:57 +00:00
|
|
|
server = picker.PickServer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(1))
|
2016-07-25 19:43:31 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
server = picker.PickServer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(1))
|
2016-07-25 19:43:31 +00:00
|
|
|
server = picker.PickServer()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(server.Destination().Port, Equals, net.Port(1))
|
2016-07-25 19:43:31 +00:00
|
|
|
}
|