-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
78 lines (60 loc) · 1.83 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
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
// Dummy server for testing
type dummyServer struct {
addr string
isAlive bool
}
func (s *dummyServer) Address() string { return s.addr }
func (s *dummyServer) IsAlive() bool { return s.isAlive }
func (s *dummyServer) Serve(rw http.ResponseWriter, req *http.Request) {}
func TestGetNextAvailableServer(t *testing.T) {
servers := []Server{
&dummyServer{addr: "server1", isAlive: true},
&dummyServer{addr: "server2", isAlive: false},
&dummyServer{addr: "server3", isAlive: true},
}
lb := NewLoadBalancer("3000", servers)
// Round-robin count starts from 0
server1 := lb.getNextAvailableServer()
if server1.Address() != "server1" {
t.Errorf("Expected server1, got %s", server1.Address())
}
server2 := lb.getNextAvailableServer()
if server2.Address() != "server3" {
t.Errorf("Expected server3, got %s", server2.Address())
}
server3 := lb.getNextAvailableServer()
if server3.Address() != "server1" {
t.Errorf("Expected server1, got %s", server3.Address())
}
}
func TestLoadBalancerServeProxy(t *testing.T) {
servers := []Server{
&dummyServer{addr: "server1", isAlive: true},
&dummyServer{addr: "server2", isAlive: true},
}
lb := NewLoadBalancer("3000", servers)
// Create a mock request and recorder for testing
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
lb.serveProxy(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", rr.Code)
}
// TODO: Add more assertions for request forwarding to the correct server
// (This might be more complex to test due to httptest.NewRecorder limitations.)
}
// TODO: Write more test cases to cover additional scenarios, including edge cases.
func TestMain(m *testing.M) {
// Test setup code, if any.
// Run tests
m.Run()
}