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()
|
2017-08-29 06:56:57 -04:00
|
|
|
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())
|
|
|
|
}
|
2017-08-29 06:56:57 -04:00
|
|
|
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()
|
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()
|
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
|
|
|
}
|