@@ -16,6 +16,7 @@ import (
16
16
)
17
17
18
18
type conn struct {
19
+ loc string
19
20
uid uint64
20
21
id uint64
21
22
alive bool
@@ -144,6 +145,91 @@ func TestAddDel(t *testing.T) {
144
145
}
145
146
}
146
147
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
+
147
233
func TestTreeFillAndGet (t * testing.T ) {
148
234
type args struct {
149
235
children int
0 commit comments