Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 9ee77f4

Browse files
b1tamaraameowlia
authored andcommitted
test: Make test more readable
1 parent 53fcecd commit 9ee77f4

File tree

2 files changed

+11
-42
lines changed

2 files changed

+11
-42
lines changed

route/roundrobin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func (r *RoundRobin) next(attempt int) *endpointElem {
9696
if r.nextIdx == -1 {
9797
r.nextIdx = r.pool.NextIndex()
9898
}
99+
// Check the next index of iterator in case the pool size decreased
100+
if r.nextIdx >= poolSize {
101+
r.nextIdx = 0
102+
}
99103

100104
startingIndex := r.nextIdx
101105
currentIndex := startingIndex
@@ -107,7 +111,7 @@ func (r *RoundRobin) next(attempt int) *endpointElem {
107111

108112
// We tried using the actual modulo operator, but it has a 10x performance penalty
109113
nextIndex = currentIndex + 1
110-
if nextIndex == poolSize {
114+
if nextIndex >= poolSize {
111115
nextIndex = 0
112116
}
113117

route/roundrobin_test.go

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -80,46 +80,6 @@ var _ = Describe("RoundRobin", func() {
8080
Entry("When the next index is 0", 0),
8181
)
8282

83-
DescribeTable("it performs round-robin consecutively through the endpoints",
84-
func(nextIdx int) {
85-
pool.NextIdx = nextIdx
86-
e1 := route.NewEndpoint(&route.EndpointOpts{Host: "1.2.3.4", Port: 5678})
87-
e2 := route.NewEndpoint(&route.EndpointOpts{Host: "5.6.7.8", Port: 1234})
88-
e3 := route.NewEndpoint(&route.EndpointOpts{Host: "1.2.7.8", Port: 1234})
89-
endpoints := []*route.Endpoint{e1, e2, e3}
90-
91-
for _, e := range endpoints {
92-
pool.Put(e)
93-
}
94-
95-
iter := route.NewRoundRobin(logger.Logger, pool, "", false, false, "meow-az")
96-
97-
iteratedEndpoints := make([]*route.Endpoint, len(endpoints))
98-
for i := 0; i < len(endpoints); i += 1 {
99-
n := iter.Next(i)
100-
for _, e := range endpoints {
101-
if e == n {
102-
iteratedEndpoints[i] = e
103-
break
104-
}
105-
}
106-
}
107-
108-
currentIndex := nextIdx
109-
for i := 0; i < len(endpoints); i += 1 {
110-
if currentIndex >= len(endpoints) {
111-
currentIndex = 0
112-
}
113-
Expect(iteratedEndpoints[i]).To(Equal(endpoints[currentIndex]))
114-
currentIndex++
115-
}
116-
117-
},
118-
Entry("When the next index is 0", 0),
119-
Entry("When the next index is 1", 1),
120-
Entry("When the next index is 2", 2),
121-
)
122-
12383
DescribeTable("it performs round-robin through the endpoints for two parallel-running iterators",
12484
func(nextIdx int) {
12585
pool.NextIdx = nextIdx
@@ -131,11 +91,14 @@ var _ = Describe("RoundRobin", func() {
13191
pool.Put(e)
13292
}
13393

94+
By("Create two iterators running over the same endpoint pool")
13495
iter1 := route.NewRoundRobin(logger.Logger, pool, "", false, false, "meow-az")
13596
iter2 := route.NewRoundRobin(logger.Logger, pool, "", false, false, "meow-az")
13697

13798
iteratedEndpoints1 := make(map[*route.Endpoint]int)
13899
iteratedEndpoints2 := make(map[*route.Endpoint]int)
100+
101+
By("Simulate retrying with attempts = endpoint number and count how many times both iterators iterate over every endpoint")
139102
for i := 0; i < len(endpoints); i += 1 {
140103
n := iter1.Next(i)
141104
k := iter2.Next(i)
@@ -148,10 +111,12 @@ var _ = Describe("RoundRobin", func() {
148111
}
149112
}
150113
}
151-
114+
By("Expect that first round robin iterator iterates over every endpoint exactly one time")
152115
for e := range iteratedEndpoints1 {
153116
Expect(iteratedEndpoints1[e]).To(Equal(1))
154117
}
118+
119+
By("Expect that second round robin iterator iterates over every endpoint exactly one time")
155120
for e := range iteratedEndpoints2 {
156121
Expect(iteratedEndpoints2[e]).To(Equal(1))
157122
}

0 commit comments

Comments
 (0)