-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
186 lines (156 loc) · 4.26 KB
/
main_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/jonboulle/clockwork"
)
var (
client = &http.Client{}
lbServer *httptest.Server
lb = &LoadBalancer{}
servers []*http.Server
nodeParams = []NodeParams{
{ID: 1, URL: "http://localhost:8081", ReqLimit: 2, BodyLimit: 76},
{ID: 2, URL: "http://localhost:8082", ReqLimit: 3, BodyLimit: 2 * 1024 * 1024},
{ID: 3, URL: "http://localhost:8083", ReqLimit: 5, BodyLimit: 1 * 1024 * 1024},
}
)
func startTestServer(port int) *http.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: handler,
}
go server.ListenAndServe()
return server
}
func setup() {
for _, nodeParams := range nodeParams {
server := startTestServer(8080 + nodeParams.ID)
servers = append(servers, server)
lb.AddNode(NewNode(nodeParams))
}
lbServer = httptest.NewServer(lb)
time.Sleep(1 * time.Second)
}
func teardown() {
lbServer.Close()
for _, server := range servers {
server.Close()
}
}
func resetRateLimits(lb *LoadBalancer) {
for _, node := range lb.Nodes {
node.Mutex.Lock()
node.ReqCount = 0
node.BodyCount = 0
node.Mutex.Unlock()
}
}
func resetHealthChecks(lb *LoadBalancer) {
for _, node := range lb.Nodes {
node.Mutex.Lock()
node.Healthy = true
node.Mutex.Unlock()
}
}
func TestLoadBalancer(t *testing.T) {
setup()
defer teardown()
t.Run("RoundRobin", testRoundRobin)
t.Run("RateLimitExceeded", testRateLimitExceededByRPM)
t.Run("NodeHealthHandling", testAllNodesUnhealthy)
t.Run("AllNodesBusy", testAllNodesBusy)
t.Run("RateLimitReset", testRateLimitReset)
}
// Should work by round robin
func testRoundRobin(t *testing.T) {
next := lb.Next
for i := 0; i <= len(lb.Nodes) + 1; i++ {
node := lb.Nodes[next]
if node.ID != next+1 {
t.Errorf("Round robin not working correctly, expected %d, got %d", next+1, node.ID)
}
next = (next + 1) % len(lb.Nodes)
}
}
// Should hit rate limit by requests per minute for each node
func testRateLimitExceededByRPM(t *testing.T) {
resetRateLimits(lb)
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := client.Get(lbServer.URL)
if err != nil {
t.Errorf("Failed to send request: %v", err)
return
}
resp.Body.Close()
}()
}
wg.Wait()
if lb.Nodes[0].ReqCount < lb.Nodes[0].ReqLimit {
t.Errorf("Rate Limit should be hit; RPM %d/%d", lb.Nodes[0].ReqCount, lb.Nodes[0].ReqLimit)
}
t.Logf("Node2 ReqCount:%d, ReqLimit:%d", lb.Nodes[1].ReqCount, lb.Nodes[1].ReqLimit)
t.Logf("Node3 ReqCount:%d, ReqLimit:%d", lb.Nodes[2].ReqCount, lb.Nodes[2].ReqLimit)
}
// TODO: Should hit rate limit by request body size per minute for each node
func testRateLimitExceededByBPM(t *testing.T) {
}
// Should return 503 when all nodes are unhealthy
func testAllNodesUnhealthy(t *testing.T) {
resetRateLimits(lb)
for _, node := range lb.Nodes {
node.Healthy = false
}
resp, err := client.Get(lbServer.URL)
if err != nil && resp.StatusCode != http.StatusServiceUnavailable {
t.Errorf("Expected status code %d, got %d", http.StatusServiceUnavailable, resp.StatusCode)
t.Logf("Err: %v", err)
}
resp.Body.Close()
}
// Should return 503 when all nodes have hit rate limits
func testAllNodesBusy(t *testing.T) {
resetHealthChecks(lb)
for _, node := range lb.Nodes {
node.ReqCount = node.ReqLimit
}
resp, err := client.Get(lbServer.URL)
if err != nil && resp.StatusCode != http.StatusServiceUnavailable {
t.Errorf("Expected status code %d, got %d", http.StatusServiceUnavailable, resp.StatusCode)
t.Logf("Err: %v", err)
}
resp.Body.Close()
}
// Should reset RPM and BPM for each node every minute
func testRateLimitReset(t *testing.T) {
c := clockwork.NewFakeClock()
lb2 := NewLoadBalancer(c)
node := NewNode(nodeParams[0])
lb2.AddNode(node)
node.ReqCount = 10
node.BodyCount = 100
var wg sync.WaitGroup
wg.Add(1)
go func() {
lb2.StartPeriodicTasks()
c.Advance(61 * time.Second)
wg.Done()
}()
c.BlockUntil(1)
wg.Wait()
if node.ReqCount != 0 || node.BodyCount != 0 {
t.Errorf("Expected counters to be reset to 0, got ReqCount=%d, BodyCount=%d", node.ReqCount, node.BodyCount)
}
}