Skip to content

Commit f28fded

Browse files
committed
add location fallback test
1 parent c5701ea commit f28fded

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

internal/transport/balancing/v3/grid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type (
3434
mx *sync.Mutex
3535

3636
locDta map[string][]PT // connections per location
37-
connIdxs map[string]int // nex available indexes per location
37+
connIdxs map[string]int // next available indexes per location
3838
blnIdxs map[string]int // round-robin indexes per location
3939

4040
locPrefM map[string]struct{} // locations with configured preference

internal/transport/balancing/v3/grid_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
type conn struct {
19+
loc string
1920
uid uint64
2021
id uint64
2122
alive bool
@@ -144,6 +145,91 @@ func TestAddDel(t *testing.T) {
144145
}
145146
}
146147

148+
func TestLocationFallback(t *testing.T) {
149+
balancer := NewGrid[*conn, conn](Config{
150+
ConnsPerEndpoint: 4,
151+
LocationPreference: []string{"aaa", "bbb", "ccc"},
152+
})
153+
154+
locMp := make(map[string][]*conn, 20)
155+
156+
require.NoError(t, balancer.Add("aaa", func() (*conn, error) {
157+
conn_ := &conn{alive: true, loc: "aaa"}
158+
locMp["aaa"] = append(locMp["aaa"], conn_)
159+
return conn_, nil
160+
}))
161+
require.NoError(t, balancer.Add("bbb", func() (*conn, error) {
162+
conn_ := &conn{alive: true, loc: "bbb"}
163+
locMp["bbb"] = append(locMp["bbb"], conn_)
164+
return conn_, nil
165+
}))
166+
require.NoError(t, balancer.Add("bbb", func() (*conn, error) {
167+
conn_ := &conn{alive: true, loc: "ccc"}
168+
locMp["ccc"] = append(locMp["ccc"], conn_)
169+
return conn_, nil
170+
}))
171+
require.NoError(t, balancer.Add("ddd", func() (*conn, error) {
172+
conn_ := &conn{alive: true, loc: "ddd"}
173+
locMp["ddd"] = append(locMp["ddd"], conn_)
174+
return conn_, nil
175+
}))
176+
177+
getConns := func(t *testing.T, loc string) {
178+
t.Helper()
179+
180+
for range len(locMp) {
181+
conn_ := balancer.GetConn()
182+
require.NotNil(t, conn_)
183+
require.Equal(t, loc, conn_.loc)
184+
}
185+
}
186+
187+
setAlive := func(t *testing.T, loc string, alive bool) {
188+
t.Helper()
189+
190+
for _, conn_ := range locMp[loc] {
191+
conn_.alive = alive
192+
}
193+
}
194+
195+
// first location should be used
196+
getConns(t, "aaa")
197+
// set aaa to be not alive
198+
setAlive(t, "aaa", false)
199+
// second location should be used
200+
getConns(t, "bbb")
201+
// set aaa to be not alive
202+
setAlive(t, "bbb", false)
203+
// third location should be used
204+
getConns(t, "ccc")
205+
// set ccc to be not alive
206+
setAlive(t, "ccc", false)
207+
// forth location should be used
208+
getConns(t, "ddd")
209+
// set ddd to be not alive
210+
setAlive(t, "ddd", false)
211+
// no connections should be available
212+
for range len(locMp) {
213+
require.Nil(t, balancer.GetConn())
214+
}
215+
// set ddd to be not alive
216+
setAlive(t, "ddd", true)
217+
// forth location should be used
218+
getConns(t, "ddd")
219+
// set ccc to alive
220+
setAlive(t, "ccc", true)
221+
// third location should be used
222+
getConns(t, "ccc")
223+
// set bbb to be not alive
224+
setAlive(t, "bbb", true)
225+
// second location should be used
226+
getConns(t, "bbb")
227+
// set aaa to be not alive
228+
setAlive(t, "aaa", true)
229+
// first location should be used
230+
getConns(t, "aaa")
231+
}
232+
147233
func TestTreeFillAndGet(t *testing.T) {
148234
type args struct {
149235
children int

0 commit comments

Comments
 (0)