Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package protocol_test
 
import (
    "testing"
    "time"
 
    "github.com/v2fly/v2ray-core/v5/common/net"
    . "github.com/v2fly/v2ray-core/v5/common/protocol"
)
 
func TestServerList(t *testing.T) {
    list := NewServerList()
    list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(1)), AlwaysValid()))
    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))))
    if list.Size() != 2 {
        t.Error("list.size: ", list.Size())
    }
 
    server := list.GetServer(1)
    if server.Destination().Port != 2 {
        t.Error("server: ", server.Destination())
    }
    time.Sleep(2 * time.Second)
    server = list.GetServer(1)
    if server != nil {
        t.Error("server: ", server)
    }
 
    server = list.GetServer(0)
    if server.Destination().Port != 1 {
        t.Error("server: ", server.Destination())
    }
}
 
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))))
 
    picker := NewRoundRobinServerPicker(list)
    server := picker.PickServer()
    if server.Destination().Port != 1 {
        t.Error("server: ", server.Destination())
    }
    server = picker.PickServer()
    if server.Destination().Port != 2 {
        t.Error("server: ", server.Destination())
    }
    server = picker.PickServer()
    if server.Destination().Port != 3 {
        t.Error("server: ", server.Destination())
    }
    server = picker.PickServer()
    if server.Destination().Port != 1 {
        t.Error("server: ", server.Destination())
    }
 
    time.Sleep(2 * time.Second)
    server = picker.PickServer()
    if server.Destination().Port != 1 {
        t.Error("server: ", server.Destination())
    }
    server = picker.PickServer()
    if server.Destination().Port != 1 {
        t.Error("server: ", server.Destination())
    }
}