-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
211 lines (169 loc) · 4.76 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"os/exec"
"sync"
"testing"
"time"
"github.com/teejays/clog"
)
var targetPorts = []int{9000, 9001, 9002, 9003, 9004, 9005}
var serverAddrs ServerAddresses
func init() {
// Make the interval smaller for testing
HealthCheckInterval = time.Second * 2
// Initialize the ServerAddress instance, just like if someone has passed all these args
for _, p := range targetPorts {
serverAddrs = append(serverAddrs, fmt.Sprintf("http://localhost:%d", p))
}
// Supress logging level
clog.LogLevel = 4
// Start the servers
err := startTargetServers()
if err != nil {
log.Fatalf("failed to start target servers: %s", err)
}
// Initialize ServerAddresses & ServerPool
pool, err = NewServerPool(serverAddrs)
if err != nil {
log.Fatal(err)
}
}
// // TestNewServerPool tests that we can successfully create a new ServerPool instance.
// func TestNewServerPool(t *testing.T) {
// err := startTargetServers()
// if err != nil {
// t.Fatalf("failed to start target servers: %s", err)
// }
// defer stopTargetServers()
// // Initialize ServerAddresses & ServerPool
// pool, err = NewServerPool(serverAddrs)
// if err != nil {
// t.Error(err)
// }
// pool.CancelHealthCheck()
// pool = nil
// }
// TestNewServerPool tests that successfully get a 503 when there are no healthy servers
func TestNoHealthyServer(t *testing.T) {
// Initialize the pool
// pool, err = NewServerPool(serverAddrs)
// if err != nil {
// t.Error(err)
// }
// defer func() {
// pool.CancelHealthCheck()
// pool = nil
// }()
// Degrade all the servers and keep in that state
pool.PauseHealthChecks()
pool.DegradeAll()
// Create a request to pass to our handler.
r := httptest.NewRequest("GET", fmt.Sprintf("localhost:%d", listenerPortDeault), nil)
w := httptest.NewRecorder()
listenerHandler(w, r)
// We should expect a 503
if w.Code != http.StatusServiceUnavailable {
t.Errorf("Expected a 503 status code but got %d", w.Code)
}
pool.Normalize()
}
// TestNewServerPool makes concurrent requests to the load balancer and fails if it receives anything
// other than a 503 or 200
func TestConcurrent(t *testing.T) {
// Create some load to pass to our handler
var concurrency = make(chan int, 1)
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
concurrency <- 4
go func(i int) {
defer func() {
if r := recover(); r != nil {
log.Fatalf("Panic (recovered) during TestConcurrent(): %s", r)
}
<-concurrency
wg.Done()
}()
r := httptest.NewRequest("GET", fmt.Sprintf("http://localhost:%d", listenerPortDeault), nil)
w := httptest.NewRecorder()
listenerHandler(w, r)
// We should expect a 200 or 503
if w.Code != http.StatusServiceUnavailable && w.Code != http.StatusOK {
t.Errorf("[%d] Expected a 200 or 503 status code but got %d", i, w.Code)
}
}(i)
time.Sleep(time.Millisecond * 200)
}
wg.Wait()
}
// Todo: Write test for the case when the state of a healthy server changes after it has been picked up
// in the round robin. It is difficult to deterministically create this scenario
// TestRoundRobin tests that Round Robin behaves as expected, returning the next healthy server.
func TestRoundRobin(t *testing.T) {
// Test 1: When all servers are healthy
pool.PauseHealthChecks()
pool.HealthyAll()
pool.CurrentIndex = 0
for i := 0; i < len(pool.Servers); i++ {
rrIdx, err := RoundRobin(pool)
if err != nil {
t.Error(err)
}
if rrIdx != i {
t.Errorf("Expected RoundRobin to choose index %d but it chose %d", i, rrIdx)
}
}
// Test 2: When server at index K is unhealthy
k := 2
if k >= len(pool.Servers) {
log.Fatalf("Invalid test: index is server to be marked unhealthy is out of bounds")
}
pool.Servers[k].Degrade()
for i := 0; i < len(pool.Servers); i++ {
rrIdx, err := RoundRobin(pool)
if err != nil {
t.Error(err)
}
if rrIdx == k {
t.Errorf("Expected RoundRobin to to never chose unhealthy server at index %d but it did", k)
}
}
pool.Normalize()
}
func BenchmarkServer(b *testing.B) {
for n := 0; n < b.N; n++ {
r := httptest.NewRequest("GET", fmt.Sprintf("http://localhost:%d", listenerPortDeault), nil)
w := httptest.NewRecorder()
listenerHandler(w, r)
}
}
// Functions to start/stop the target servers `go test`
func startTargetServers() (err error) {
for _, p := range targetPorts {
err = startTargetServer(p)
if err != nil {
return err
}
time.Sleep(2 * time.Second)
}
return nil
}
func stopTargetServers() {
cmd := exec.Command("pkill", "-f", targetBinaryName)
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func startTargetServer(port int) error {
cmd := exec.Command(targetBinaryName, "server", "-p", fmt.Sprintf("%d", port))
err := cmd.Start()
if err != nil {
return err
}
return nil
}