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