1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 17:38:45 -05:00
v2fly/common/protocol/server_picker_test.go

55 lines
1.8 KiB
Go
Raw Normal View History

2016-07-25 15:43:31 -04:00
package protocol_test
import (
"testing"
"time"
"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()
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))
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()
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
}