-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
139 lines (120 loc) · 3.6 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
package main
import (
"fmt"
"github.com/intrip/golang-load-balancer/common"
"io/ioutil"
"log"
"net"
"net/http"
"testing"
"time"
)
func TestParseBalance(t *testing.T) {
balance := "http://localhost:3000/home,http://localhost:3001/info"
expectedBackends := []common.Backend{common.Backend{"http://localhost:3000/home", 0}, common.Backend{"http://localhost:3001/info", 0}}
backends := parseBalance(balance)
if len(backends) != len(expectedBackends) {
t.Errorf("Result size differ, expected: %d, got: %d", len(expectedBackends), len(backends))
return
}
for index, backend := range backends {
if backend != expectedBackends[index] {
t.Errorf("Backend %d differ, expected: %q got %q", index, expectedBackends[index], backend)
}
}
}
func TestLoadConfig(t *testing.T) {
loadConfig("config_test")
expectedPort := 8080
expectedBind := "localhost"
expectedMaxConnections := 100
expectedReadTimeout := 30
expectedWriteTimeout := 30
expectedBalance := "http://localhost:8081"
if expectedPort != port {
t.Errorf("Port differ, expected %d got %d", expectedPort, port)
}
if expectedBind != bind {
t.Errorf("Bind differ, expected %d got %d", expectedBind, bind)
}
if expectedMaxConnections != maxConnections {
t.Errorf("MaxConnections differ, expected %d got %d", expectedMaxConnections, maxConnections)
}
if expectedReadTimeout != readTimeout {
t.Errorf("readTimeout differ, expected %d got %d", expectedReadTimeout, readTimeout)
}
if expectedWriteTimeout != writeTimeout {
t.Errorf("WriteTimeout differ, expected %d got %d", expectedWriteTimeout, writeTimeout)
}
if expectedBalance != balance {
t.Errorf("Balance differ, expected %d got %d", expectedBalance, balance)
}
}
func TestDoBalance(t *testing.T) {
msg := "Hello world!"
// listen backend
beListen := "localhost:8081"
beRemoteAddr := ""
beServeMux := http.NewServeMux()
beServeMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Header["X-Forwarded-For"][0] != beRemoteAddr {
t.Errorf("Expected X-Forwarded-For: %s, got: %s", r.Header["X-Forwarded-For"][0], beRemoteAddr)
}
// send msg to the caller
fmt.Fprintf(w, msg)
})
beServer := &http.Server{
Addr: beListen,
Handler: beServeMux,
}
// listen balancer
serveMux := http.NewServeMux()
serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
beRemoteAddr, _, _ = net.SplitHostPort(r.RemoteAddr)
doBalance(w, r, &common.Backend{Url: fmt.Sprintf("http://%s", beListen), ActiveConnections: 0})
})
server := &http.Server{
Addr: serverUrl(),
Handler: serveMux,
}
// backend
go func() {
beServer.ListenAndServe()
}()
// balancer
go func() {
server.ListenAndServe()
}()
time.Sleep(time.Duration(100) * time.Millisecond)
res, err := http.Get(fmt.Sprintf("http://%s/", serverUrl()))
if err != nil {
log.Panic("[test] Error connecting to balancer: ", err)
}
bodyBytes, _ := ioutil.ReadAll(res.Body)
if string(bodyBytes) != msg {
t.Errorf("Expected to read %s, got: %s", msg, bodyBytes)
}
defer beServer.Close()
defer server.Close()
}
func TestBackendUnavailable(t *testing.T) {
// backend
serveMux := http.NewServeMux()
serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
doBalance(w, r, &common.Backend{Url: "http://localhost:9999"})
})
server := &http.Server{
Addr: serverUrl(),
Handler: serveMux,
}
// balancer
go func() {
server.ListenAndServe()
}()
time.Sleep(time.Duration(100) * time.Millisecond)
res, _ := http.Get(fmt.Sprintf("http://%s/", serverUrl()))
if res.StatusCode != 502 {
t.Errorf("Expected status code 502, got: %d", res.StatusCode)
}
defer server.Close()
}