1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/common/protocol/server_picker_test.go

72 lines
2.0 KiB
Go
Raw Normal View History

2016-07-25 15:43:31 -04:00
package protocol_test
import (
"testing"
"time"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/net"
. "github.com/v2fly/v2ray-core/v4/common/protocol"
2016-07-25 15:43:31 -04:00
)
func TestServerList(t *testing.T) {
list := NewServerList()
list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(1)), AlwaysValid()))
2019-02-10 09:02:28 -05:00
if list.Size() != 1 {
t.Error("list size: ", list.Size())
}
list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(2)), BeforeTime(time.Now().Add(time.Second))))
2019-02-10 09:02:28 -05:00
if list.Size() != 2 {
t.Error("list.size: ", list.Size())
}
2016-07-25 15:43:31 -04:00
server := list.GetServer(1)
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 2 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:31 -04:00
time.Sleep(2 * time.Second)
server = list.GetServer(1)
2019-02-10 09:02:28 -05:00
if server != nil {
t.Error("server: ", server)
}
2016-07-25 15:43:31 -04:00
server = list.GetServer(0)
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 1 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:31 -04:00
}
func TestServerPicker(t *testing.T) {
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()
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 1 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:31 -04:00
server = picker.PickServer()
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 2 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:31 -04:00
server = picker.PickServer()
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 3 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:57 -04:00
server = picker.PickServer()
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 1 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:31 -04:00
time.Sleep(2 * time.Second)
server = picker.PickServer()
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 1 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:31 -04:00
server = picker.PickServer()
2019-02-10 09:02:28 -05:00
if server.Destination().Port != 1 {
t.Error("server: ", server.Destination())
}
2016-07-25 15:43:31 -04:00
}