/
random.go
92 lines (78 loc) · 2.14 KB
/
random.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
package testing_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/vinay03/loadbalancer/src"
"net/http"
_ "net/http/pprof"
)
var _ = Describe("Random Logic", func() {
var LbTestService LoadBalancerService
BeforeEach(func() {
LbTestService = LoadBalancerService{}
config := &LoadBalancerServiceParams{
DebugMode: DebugMode,
YAMLConfigString: `listeners:
- protocol: http
port: 8080
routes:
- routeprefix: "/"
mode: "Random"
targets:
- address: http://localhost:8091
- address: http://localhost:8092
- address: http://localhost:8093
- routeprefix: "/single"
mode: "Random"
targets:
- address: http://localhost:8091`,
}
LbTestService.SetParams(config)
LbTestService.Apply()
// Start Test Servers
StartTestServers(3)
})
AfterEach(func() {
LbTestService.Stop()
StopTestServers()
})
It("Algorithm with Multiple Targets", func() {
for i := 0; i < 12; i++ {
res, body := Request(LISTENER_8080_URL).Get()
// Check status code
Expect(res.StatusCode).To(Equal(http.StatusOK))
// Check replica ID
Expect(body.ReplicaId).To(BeElementOf([]int{1, 2, 3}))
}
})
It("Algorithm with Single Target", func() {
for i := 0; i < 12; i++ {
res, body := Request(LISTENER_8080_URL + "single").Get()
// Check status code
Expect(res.StatusCode).To(Equal(http.StatusOK))
// Check replica ID
Expect(body.ReplicaId).To(Equal(1))
}
})
It("With Multiple targets of mixed 'IsAlive' status ", func() {
TestServersPool[1].Stop()
for i := 0; i < 10; i++ {
res, body := Request(LISTENER_8080_URL).Get()
if res.StatusCode == http.StatusOK {
// Check replica ID
Expect(body.ReplicaId).To(BeElementOf([]int{1, 3}))
}
}
TestServersPool[0].Stop()
for i := 0; i < 10; i++ {
res, body := Request(LISTENER_8080_URL).Get()
if res.StatusCode == http.StatusOK {
// Check replica ID
Expect(body.ReplicaId).To(BeElementOf([]int{3}))
}
}
TestServersPool[2].Stop()
res, _ := Request(LISTENER_8080_URL).Get()
Expect(res.StatusCode).To(Equal(http.StatusBadGateway))
})
})