MCPcopy
hub / github.com/grpc/grpc-go / TestWeightedTarget

Method TestWeightedTarget

balancer/weightedtarget/weightedtarget_test.go:212–394  ·  view source on GitHub ↗

TestWeightedTarget covers the cases that a sub-balancer is added and a sub-balancer is removed. It verifies that the addresses and balancer configs are forwarded to the right sub-balancer. This test is intended to test the glue code in weighted_target. It also tests an empty target config update, wh

(t *testing.T)

Source from the content-addressed store, hash-verified

210// glue code in weighted_target. It also tests an empty target config update,
211// which should trigger a transient failure state update.
212func (s) TestWeightedTarget(t *testing.T) {
213 cc := testutils.NewBalancerClientConn(t)
214 wtb := wtbBuilder.Build(cc, balancer.BuildOptions{})
215 defer wtb.Close()
216
217 // Start with "cluster_1: round_robin".
218 config1, err := wtbParser.ParseConfig([]byte(`
219{
220 "targets": {
221 "cluster_1": {
222 "weight":1,
223 "childPolicy": [{"round_robin": ""}]
224 }
225 }
226}`))
227 if err != nil {
228 t.Fatalf("failed to parse balancer config: %v", err)
229 }
230
231 // Send the config, and an address with hierarchy path ["cluster_1"].
232 addr1 := resolver.Address{Addr: testBackendAddrStrs[1], Attributes: nil}
233 if err := wtb.UpdateClientConnState(balancer.ClientConnState{
234 ResolverState: resolver.State{Endpoints: []resolver.Endpoint{
235 hierarchy.SetInEndpoint(resolver.Endpoint{Addresses: []resolver.Address{addr1}}, []string{"cluster_1"}),
236 }},
237 BalancerConfig: config1,
238 }); err != nil {
239 t.Fatalf("failed to update ClientConn state: %v", err)
240 }
241 verifyAddressInNewSubConn(t, cc, addr1)
242
243 // Send subconn state change.
244 sc1 := <-cc.NewSubConnCh
245 sc1.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.Connecting})
246 <-cc.NewPickerCh
247 sc1.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.Ready})
248 p := <-cc.NewPickerCh
249
250 // Test pick with one backend.
251 for i := 0; i < 5; i++ {
252 gotSCSt, _ := p.Pick(balancer.PickInfo{})
253 if gotSCSt.SubConn != sc1 {
254 t.Fatalf("picker.Pick, got %v, want SubConn=%v", gotSCSt, sc1)
255 }
256 }
257
258 // Remove cluster_1, and add "cluster_2: test_config_balancer". The
259 // test_config_balancer adds an address attribute whose value is set to the
260 // config that is passed to it.
261 config2, err := wtbParser.ParseConfig([]byte(`
262{
263 "targets": {
264 "cluster_2": {
265 "weight":1,
266 "childPolicy": [{"test_config_balancer": "cluster_2"}]
267 }
268 }
269}`))

Callers

nothing calls this directly

Calls 12

NewBalancerClientConnFunction · 0.92
SetInEndpointFunction · 0.92
setConfigKeyFunction · 0.85
BuildMethod · 0.65
CloseMethod · 0.65
ParseConfigMethod · 0.65
FatalfMethod · 0.65
UpdateClientConnStateMethod · 0.65
UpdateStateMethod · 0.65
PickMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected