-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_servers_test.go
133 lines (122 loc) · 2.82 KB
/
backend_servers_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
package loadbalancer
import (
"io"
"net/http"
"testing"
"time"
)
func TestStartBackendServers(t *testing.T) {
testCases := []struct {
name string
Configuration AppConfig
expected error
}{
{
name: "Wrong server address configured to load balance returns an error",
expected: ErrInvalidServerAddress,
Configuration: AppConfig{
BackendServers: map[int]BackendServer{
0: {
Address: "http://localhost",
},
1: {
Address: "http://localhost:8081",
},
},
LoadBalancerPort: ":8080",
InProduction: false,
StartGivenServers: true,
},
},
{
name: "Start backend servers correctly",
expected: nil,
Configuration: AppConfig{
BackendServers: map[int]BackendServer{
0: {
Address: "http://localhost:8080",
},
},
LoadBalancerPort: ":8080",
InProduction: false,
StartGivenServers: true,
},
},
{
name: "No backend servers configured to load balance returns an error",
expected: ErrNoBackendServersConfigured,
Configuration: AppConfig{
BackendServers: map[int]BackendServer{},
LoadBalancerPort: ":8080",
InProduction: false,
StartGivenServers: true,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
lb := LoadBalancer{
Configuration: &tc.Configuration,
}
err := lb.startBackendServers()
if err != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, err)
}
})
}
}
func TestGoroutineStartsWhenStartingServer(t *testing.T) {
startSignal := make(chan bool, 1)
go startServer(":8083", startSignal)
select {
case <-startSignal:
t.Log("Goroutine started")
case <-time.After(1 * time.Second):
t.Error("Timeout waiting for goroutine to start")
}
}
func TestBackendServerHandlerResponse(t *testing.T) {
testCases := []struct {
name string
port string
expected string
}{
{
name: "Backend server on port 9000 returns expected response",
port: ":9000",
expected: "Hello from backend server :9000",
},
{
name: "Backend server on port 9000 returns expected response",
port: ":9001",
expected: "Hello from backend server :9001",
},
}
client := &http.Client{}
for _, tc := range testCases {
channel := make(chan bool, 1)
go startServer(tc.port, channel)
<-channel
close(channel)
url := "http://localhost" + tc.port
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
t.Error(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status OK, got %v", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
result := string(body)
if result != tc.expected {
t.Errorf("Expected %v', got %v", tc.expected, result)
}
}
}