-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
105 lines (101 loc) · 2.14 KB
/
node_test.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package balancer
import (
"testing"
)
func TestUpstream_IsHealthy(t *testing.T) {
tests := []struct {
name string
upstream MockNode
want bool
}{
{
name: "healty trivial test",
upstream: MockNode{
healthy: true,
},
want: true,
},
{
name: "healty trivial test",
upstream: MockNode{
healthy: false,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := tt.upstream
if got := u.IsHealthy(); got != tt.want {
t.Errorf("MockNode.IsHealthy() = %v, want %v", got, tt.want)
}
})
}
}
func TestUpstream_IncreaseLoad(t *testing.T) {
tests := []struct {
name string
upstream *MockNode
call func(u *MockNode)
wantRequestCount uint64
wantLoad int64
wantHost string
}{
{
name: "IncreaseLoad 1 time",
upstream: &MockNode{
nodeID: "127.0.0.1",
},
call: func(u *MockNode) {
u.IncreaseLoad()
},
wantRequestCount: uint64(1),
wantLoad: int64(1),
wantHost: "127.0.0.1",
},
{
name: "IncreaseLoad 2 time, decrease 1 time",
upstream: &MockNode{
nodeID: "127.0.0.1",
},
call: func(u *MockNode) {
u.IncreaseLoad()
u.IncreaseLoad()
u.DecreaseLoad()
},
wantRequestCount: uint64(2),
wantLoad: int64(1),
wantHost: "127.0.0.1",
},
{
name: "Do requests, no load but increased (done) request",
upstream: &MockNode{
nodeID: "127.0.0.1",
},
call: func(u *MockNode) {
u.DoRequest()
u.DoRequest()
u.DoRequest()
u.DoRequest()
},
wantRequestCount: uint64(4),
wantLoad: int64(0),
wantHost: "127.0.0.1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := tt.upstream
tt.call(u)
if tt.wantLoad != u.Load() {
t.Errorf("MockNode.Load() = %v, want %v", u.Load(), tt.wantLoad)
}
if tt.wantRequestCount != u.TotalRequest() {
t.Errorf("MockNode.TotalRequest() = %v, want %v", u.TotalRequest(), tt.wantRequestCount)
}
if tt.wantHost != u.NodeID() {
t.Errorf("MockNode.NodeID() = %v, want %v", u.NodeID(), tt.wantHost)
}
})
}
}