@@ -344,17 +344,26 @@ func (s) TestThreeSubConnsAffinityMultiple(t *testing.T) {
344344 }
345345}
346346
347+ // TestAddrWeightChange covers the following scenarios after setting up the
348+ // balancer with 3 addresses [A, B, C]:
349+ // - updates balancer with [A, B, C], a new Picker should not be sent.
350+ // - updates balancer with [A, B] (C removed), a new Picker is sent and the
351+ // ring is updated.
352+ // - updates balancer with [A, B], but B has a weight of 2, a new Picker is
353+ // sent. And the new ring should contain the correct number of entries
354+ // and weights.
347355func (s ) TestAddrWeightChange (t * testing.T ) {
348- wantAddrs := []resolver.Address {
356+ addrs := []resolver.Address {
349357 {Addr : testBackendAddrStrs [0 ]},
350358 {Addr : testBackendAddrStrs [1 ]},
351359 {Addr : testBackendAddrStrs [2 ]},
352360 }
353- cc , b , p0 := setupTest (t , wantAddrs )
361+ cc , b , p0 := setupTest (t , addrs )
354362 ring0 := p0 .(* picker ).ring
355363
364+ // Update with the same addresses, should not send a new Picker.
356365 if err := b .UpdateClientConnState (balancer.ClientConnState {
357- ResolverState : resolver.State {Addresses : wantAddrs },
366+ ResolverState : resolver.State {Addresses : addrs },
358367 BalancerConfig : testConfig ,
359368 }); err != nil {
360369 t .Fatalf ("UpdateClientConnState returned err: %v" , err )
@@ -407,6 +416,27 @@ func (s) TestAddrWeightChange(t *testing.T) {
407416 if p2 .(* picker ).ring == ring1 {
408417 t .Fatalf ("new picker after changing address weight has the same ring as before, want different" )
409418 }
419+ // With the new update, the ring must look like this:
420+ // [
421+ // {idx:0 sc: {addr: testBackendAddrStrs[0], weight: 1}},
422+ // {idx:1 sc: {addr: testBackendAddrStrs[1], weight: 2}},
423+ // {idx:2 sc: {addr: testBackendAddrStrs[2], weight: 2}},
424+ // ].
425+ if len (p2 .(* picker ).ring .items ) != 3 {
426+ t .Fatalf ("new picker after changing address weight has %d entries, want 3" , len (p2 .(* picker ).ring .items ))
427+ }
428+ for _ , i := range p2 .(* picker ).ring .items {
429+ if i .sc .addr == testBackendAddrStrs [0 ] {
430+ if i .sc .weight != 1 {
431+ t .Fatalf ("new picker after changing address weight has weight %d for %v, want 1" , i .sc .weight , i .sc .addr )
432+ }
433+ }
434+ if i .sc .addr == testBackendAddrStrs [1 ] {
435+ if i .sc .weight != 2 {
436+ t .Fatalf ("new picker after changing address weight has weight %d for %v, want 2" , i .sc .weight , i .sc .addr )
437+ }
438+ }
439+ }
410440}
411441
412442// TestSubConnToConnectWhenOverallTransientFailure covers the situation when the
0 commit comments