1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
|
/*
*
* Copyright 2022 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package gracefulswitch
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/serviceconfig"
)
const (
defaultTestTimeout = 5 * time.Second
defaultTestShortTimeout = 10 * time.Millisecond
)
type s struct {
grpctest.Tester
}
func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}
func setup(t *testing.T) (*testutils.TestClientConn, *Balancer) {
tcc := testutils.NewTestClientConn(t)
return tcc, NewBalancer(tcc, balancer.BuildOptions{})
}
// TestSuccessfulFirstUpdate tests a basic scenario for the graceful switch load
// balancer, where it is setup with a balancer which should populate the current
// load balancer. Any ClientConn updates should then be forwarded to this
// current load balancer.
func (s) TestSuccessfulFirstUpdate(t *testing.T) {
_, gsb := setup(t)
if err := gsb.SwitchTo(mockBalancerBuilder1{}); err != nil {
t.Fatalf("Balancer.SwitchTo failed with error: %v", err)
}
if gsb.balancerCurrent == nil {
t.Fatal("current balancer not populated after a successful call to SwitchTo()")
}
// This will be used to update the graceful switch balancer. This update
// should simply be forwarded down to the current load balancing policy.
ccs := balancer.ClientConnState{
BalancerConfig: mockBalancerConfig{},
}
// Updating ClientConnState should forward the update exactly as is to the
// current balancer.
if err := gsb.UpdateClientConnState(ccs); err != nil {
t.Fatalf("Balancer.UpdateClientConnState(%v) failed: %v", ccs, err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if err := gsb.balancerCurrent.Balancer.(*mockBalancer).waitForClientConnUpdate(ctx, ccs); err != nil {
t.Fatal(err)
}
}
// TestTwoBalancersSameType tests the scenario where there is a graceful switch
// load balancer setup with a current and pending load balancer of the same
// type. Any ClientConn update should be forwarded to the current lb if there is
// a current lb and no pending lb, and the only the pending lb if the graceful
// switch balancer contains both a current lb and a pending lb. The pending load
// balancer should also swap into current whenever it updates with a
// connectivity state other than CONNECTING.
func (s) TestTwoBalancersSameType(t *testing.T) {
tcc, gsb := setup(t)
// This will be used to update the graceful switch balancer. This update
// should simply be forwarded down to either the current or pending load
// balancing policy.
ccs := balancer.ClientConnState{
BalancerConfig: mockBalancerConfig{},
}
gsb.SwitchTo(mockBalancerBuilder1{})
gsb.UpdateClientConnState(ccs)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if err := gsb.balancerCurrent.Balancer.(*mockBalancer).waitForClientConnUpdate(ctx, ccs); err != nil {
t.Fatal(err)
}
// The current balancer reporting READY should cause this state
// to be forwarded to the ClientConn.
gsb.balancerCurrent.Balancer.(*mockBalancer).updateState(balancer.State{
ConnectivityState: connectivity.Ready,
Picker: &neverErrPicker{},
})
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for a UpdateState call on the ClientConn")
case state := <-tcc.NewStateCh:
if state != connectivity.Ready {
t.Fatalf("current balancer reports connectivity state %v, want %v", state, connectivity.Ready)
}
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for a UpdateState call on the ClientConn")
case picker := <-tcc.NewPickerCh:
// Should receive a never err picker.
if _, err := picker.Pick(balancer.PickInfo{}); err != nil {
t.Fatalf("ClientConn should have received a never err picker from an UpdateState call")
}
}
// An explicit call to switchTo, even if the same type, should cause the
// balancer to build a new balancer for pending.
gsb.SwitchTo(mockBalancerBuilder1{})
if gsb.balancerPending == nil {
t.Fatal("pending balancer not populated after another call to SwitchTo()")
}
// A ClientConn update received should be forwarded to the new pending LB
// policy, and not the current one.
gsb.UpdateClientConnState(ccs)
sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
if err := gsb.balancerCurrent.Balancer.(*mockBalancer).waitForClientConnUpdate(sCtx, ccs); err == nil {
t.Fatal("current balancer received a ClientConn update when there is a pending balancer")
}
if err := gsb.balancerPending.Balancer.(*mockBalancer).waitForClientConnUpdate(ctx, ccs); err != nil {
t.Fatal(err)
}
// If the pending load balancer reports that is CONNECTING, no update should
// be sent to the ClientConn.
gsb.balancerPending.Balancer.(*mockBalancer).updateState(balancer.State{
ConnectivityState: connectivity.Connecting,
})
sCtx, sCancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
select {
case <-tcc.NewStateCh:
t.Fatal("balancerPending reporting CONNECTING should not forward up to the ClientConn")
case <-sCtx.Done():
}
currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
// If the pending load balancer reports a state other than CONNECTING, the
// pending load balancer is logically warmed up, and the ClientConn should
// be updated with the State and Picker to start using the new policy. The
// pending load balancing policy should also be switched into the current
// load balancer.
gsb.balancerPending.Balancer.(*mockBalancer).updateState(balancer.State{
ConnectivityState: connectivity.Ready,
Picker: &neverErrPicker{},
})
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for a UpdateState call on the ClientConn")
case state := <-tcc.NewStateCh:
if state != connectivity.Ready {
t.Fatalf("pending balancer reports connectivity state %v, want %v", state, connectivity.Ready)
}
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for a UpdateState call on the ClientConn")
case picker := <-tcc.NewPickerCh:
// This picker should be the recent one sent from UpdateState(), a never
// err picker, not the nil picker from two updateState() calls previous.
if picker == nil {
t.Fatalf("ClientConn should have received a never err picker, which is the most recent picker, from an UpdateState call")
}
if _, err := picker.Pick(balancer.PickInfo{}); err != nil {
t.Fatalf("ClientConn should have received a never err picker, which is the most recent picker, from an UpdateState call")
}
}
// The current balancer should be closed as a result of the swap.
if err := currBal.waitForClose(ctx); err != nil {
t.Fatal(err)
}
}
// TestCurrentNotReadyPendingUpdate tests the scenario where there is a current
// and pending load balancer setup in the graceful switch load balancer, and the
// current LB is not in the connectivity state READY. Any update from the
// pending load balancer should cause the graceful switch load balancer to swap
// the pending into current, and update the ClientConn with the pending load
// balancers state.
func (s) TestCurrentNotReadyPendingUpdate(t *testing.T) {
tcc, gsb := setup(t)
gsb.SwitchTo(mockBalancerBuilder1{})
gsb.SwitchTo(mockBalancerBuilder1{})
if gsb.balancerPending == nil {
t.Fatal("pending balancer not populated after another call to SwitchTo()")
}
currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
// Due to the current load balancer not being in state READY, any update
// from the pending load balancer should cause that update to be forwarded
// to the ClientConn and also cause the pending load balancer to swap into
// the current one.
gsb.balancerPending.Balancer.(*mockBalancer).updateState(balancer.State{
ConnectivityState: connectivity.Connecting,
Picker: &neverErrPicker{},
})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
select {
case <-ctx.Done():
t.Fatalf("timeout waiting for an UpdateState call on the ClientConn")
case state := <-tcc.NewStateCh:
if state != connectivity.Connecting {
t.Fatalf("ClientConn received connectivity state %v, want %v (from pending)", state, connectivity.Connecting)
}
}
select {
case <-ctx.Done():
t.Fatalf("timeout waiting for an UpdateState call on the ClientConn")
case picker := <-tcc.NewPickerCh:
// Should receive a never err picker.
if _, err := picker.Pick(balancer.PickInfo{}); err != nil {
t.Fatalf("ClientConn should have received a never err picker from an UpdateState call")
}
}
// The current balancer should be closed as a result of the swap.
if err := currBal.waitForClose(ctx); err != nil {
t.Fatal(err)
}
}
// TestCurrentLeavingReady tests the scenario where there is a current and
// pending load balancer setup in the graceful switch load balancer, with the
// current load balancer being in the state READY, and the current load balancer
// then transitions into a state other than READY. This should cause the pending
// load balancer to swap into the current load balancer, and the ClientConn to
// be updated with the cached pending load balancing state. Also, once the
// current is cleared from the graceful switch load balancer, any updates sent
// should be intercepted and not forwarded to the ClientConn, as the balancer
// has already been cleared.
func (s) TestCurrentLeavingReady(t *testing.T) {
tcc, gsb := setup(t)
gsb.SwitchTo(mockBalancerBuilder1{})
currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
currBal.updateState(balancer.State{
ConnectivityState: connectivity.Ready,
})
gsb.SwitchTo(mockBalancerBuilder2{})
// Sends CONNECTING, shouldn't make it's way to ClientConn.
gsb.balancerPending.Balancer.(*mockBalancer).updateState(balancer.State{
ConnectivityState: connectivity.Connecting,
Picker: &neverErrPicker{},
})
// The current balancer leaving READY should cause the pending balancer to
// swap to the current balancer. This swap from current to pending should
// also update the ClientConn with the pending balancers cached state and
// picker.
currBal.updateState(balancer.State{
ConnectivityState: connectivity.Idle,
})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for a UpdateState call on the ClientConn")
case state := <-tcc.NewStateCh:
if state != connectivity.Connecting {
t.Fatalf("current balancer reports connectivity state %v, want %v", state, connectivity.Connecting)
}
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for a UpdateState call on the ClientConn")
case picker := <-tcc.NewPickerCh:
// Should receive a never err picker cached from pending LB's updateState() call, which
// was cached.
if _, err := picker.Pick(balancer.PickInfo{}); err != nil {
t.Fatalf("ClientConn should have received a never err picker, the cached picker, from an UpdateState call")
}
}
// The current balancer should be closed as a result of the swap.
if err := currBal.waitForClose(ctx); err != nil {
t.Fatal(err)
}
// The current balancer is now cleared from the graceful switch load
// balancer. Thus, any update from the old current should be intercepted by
// the graceful switch load balancer and not forward up to the ClientConn.
currBal.updateState(balancer.State{
ConnectivityState: connectivity.Ready,
Picker: &neverErrPicker{},
})
// This update should not be forwarded to the ClientConn.
sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
select {
case <-sCtx.Done():
case <-tcc.NewStateCh:
t.Fatal("UpdateState() from a cleared balancer should not make it's way to ClientConn")
}
if _, err := currBal.newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{}); err == nil {
t.Fatal("newSubConn() from a cleared balancer should have returned an error")
}
// This newSubConn call should also not reach the ClientConn.
sCtx, sCancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
select {
case <-sCtx.Done():
case <-tcc.NewSubConnCh:
t.Fatal("newSubConn() from a cleared balancer should not make it's way to ClientConn")
}
}
// TestBalancerSubconns tests the SubConn functionality of the graceful switch
// load balancer. This tests the SubConn update flow in both directions, and
// make sure updates end up at the correct component. Also, it tests that on an
// UpdateSubConnState() call from the ClientConn, the graceful switch load
// balancer forwards it to the correct child balancer.
func (s) TestBalancerSubconns(t *testing.T) {
tcc, gsb := setup(t)
gsb.SwitchTo(mockBalancerBuilder1{})
gsb.SwitchTo(mockBalancerBuilder2{})
// A child balancer creating a new SubConn should eventually be forwarded to
// the ClientConn held by the graceful switch load balancer.
sc1, err := gsb.balancerCurrent.Balancer.(*mockBalancer).newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
if err != nil {
t.Fatalf("error constructing newSubConn in gsb: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an NewSubConn call on the ClientConn")
case sc := <-tcc.NewSubConnCh:
if !cmp.Equal(sc1, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
t.Fatalf("NewSubConn, want %v, got %v", sc1, sc)
}
}
// The other child balancer creating a new SubConn should also be eventually
// be forwarded to the ClientConn held by the graceful switch load balancer.
sc2, err := gsb.balancerPending.Balancer.(*mockBalancer).newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
if err != nil {
t.Fatalf("error constructing newSubConn in gsb: %v", err)
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an NewSubConn call on the ClientConn")
case sc := <-tcc.NewSubConnCh:
if !cmp.Equal(sc2, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
t.Fatalf("NewSubConn, want %v, got %v", sc2, sc)
}
}
scState := balancer.SubConnState{ConnectivityState: connectivity.Ready}
// Updating the SubConnState for sc1 should cause the graceful switch
// balancer to forward the Update to balancerCurrent for sc1, as that is the
// balancer that created this SubConn.
gsb.UpdateSubConnState(sc1, scState)
// This update should get forwarded to balancerCurrent, as that is the LB
// that created this SubConn.
if err := gsb.balancerCurrent.Balancer.(*mockBalancer).waitForSubConnUpdate(ctx, subConnWithState{sc: sc1, state: scState}); err != nil {
t.Fatal(err)
}
// This update should not get forwarded to balancerPending, as that is not
// the LB that created this SubConn.
sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
if err := gsb.balancerPending.Balancer.(*mockBalancer).waitForSubConnUpdate(sCtx, subConnWithState{sc: sc1, state: scState}); err == nil {
t.Fatalf("balancerPending should not have received a subconn update for sc1")
}
// Updating the SubConnState for sc2 should cause the graceful switch
// balancer to forward the Update to balancerPending for sc2, as that is the
// balancer that created this SubConn.
gsb.UpdateSubConnState(sc2, scState)
// This update should get forwarded to balancerPending, as that is the LB
// that created this SubConn.
if err := gsb.balancerPending.Balancer.(*mockBalancer).waitForSubConnUpdate(ctx, subConnWithState{sc: sc2, state: scState}); err != nil {
t.Fatal(err)
}
// This update should not get forwarded to balancerCurrent, as that is not
// the LB that created this SubConn.
sCtx, sCancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
if err := gsb.balancerCurrent.Balancer.(*mockBalancer).waitForSubConnUpdate(sCtx, subConnWithState{sc: sc2, state: scState}); err == nil {
t.Fatalf("balancerCurrent should not have received a subconn update for sc2")
}
// Updating the addresses for both SubConns and removing both SubConns
// should get forwarded to the ClientConn.
// Updating the addresses for sc1 should get forwarded to the ClientConn.
gsb.balancerCurrent.Balancer.(*mockBalancer).updateAddresses(sc1, []resolver.Address{})
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
case <-tcc.UpdateAddressesAddrsCh:
}
// Updating the addresses for sc2 should also get forwarded to the ClientConn.
gsb.balancerPending.Balancer.(*mockBalancer).updateAddresses(sc2, []resolver.Address{})
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
case <-tcc.UpdateAddressesAddrsCh:
}
// balancerCurrent removing sc1 should get forwarded to the ClientConn.
gsb.balancerCurrent.Balancer.(*mockBalancer).removeSubConn(sc1)
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
case sc := <-tcc.RemoveSubConnCh:
if !cmp.Equal(sc1, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
t.Fatalf("RemoveSubConn, want %v, got %v", sc1, sc)
}
}
// balancerPending removing sc2 should get forwarded to the ClientConn.
gsb.balancerPending.Balancer.(*mockBalancer).removeSubConn(sc2)
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
case sc := <-tcc.RemoveSubConnCh:
if !cmp.Equal(sc2, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
t.Fatalf("RemoveSubConn, want %v, got %v", sc2, sc)
}
}
}
// TestBalancerClose tests the graceful switch balancer's Close() functionality.
// From the Close() call, the graceful switch balancer should remove any created
// Subconns and Close() the current and pending load balancers. This Close()
// call should also cause any other events (calls to entrance functions) to be
// no-ops.
func (s) TestBalancerClose(t *testing.T) {
// Setup gsb balancer with current, pending, and one created SubConn on both
// current and pending.
tcc, gsb := setup(t)
gsb.SwitchTo(mockBalancerBuilder1{})
gsb.SwitchTo(mockBalancerBuilder2{})
sc1, err := gsb.balancerCurrent.Balancer.(*mockBalancer).newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{}) // Will eventually get back a SubConn with an identifying property id 1
if err != nil {
t.Fatalf("error constructing newSubConn in gsb: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an NewSubConn call on the ClientConn")
case <-tcc.NewSubConnCh:
}
sc2, err := gsb.balancerPending.Balancer.(*mockBalancer).newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{}) // Will eventually get back a SubConn with an identifying property id 2
if err != nil {
t.Fatalf("error constructing newSubConn in gsb: %v", err)
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an NewSubConn call on the ClientConn")
case <-tcc.NewSubConnCh:
}
currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
pendBal := gsb.balancerPending.Balancer.(*mockBalancer)
// Closing the graceful switch load balancer should lead to removing any
// created SubConns, and closing both the current and pending load balancer.
gsb.Close()
// The order of SubConns the graceful switch load balancer tells the Client
// Conn to remove is non deterministic, as it is stored in a map. However,
// the first SubConn removed should be either sc1 or sc2.
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
case sc := <-tcc.RemoveSubConnCh:
if !cmp.Equal(sc1, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
if !cmp.Equal(sc2, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
t.Fatalf("RemoveSubConn, want either %v or %v, got %v", sc1, sc2, sc)
}
}
}
// The graceful switch load balancer should then tell the ClientConn to
// remove the other SubConn.
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses call on the ClientConn")
case sc := <-tcc.RemoveSubConnCh:
if !cmp.Equal(sc1, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
if !cmp.Equal(sc2, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
t.Fatalf("RemoveSubConn, want either %v or %v, got %v", sc1, sc2, sc)
}
}
}
// The current balancer should get closed as a result of the graceful switch balancer being closed.
if err := currBal.waitForClose(ctx); err != nil {
t.Fatal(err)
}
// The pending balancer should also get closed as a result of the graceful switch balancer being closed.
if err := pendBal.waitForClose(ctx); err != nil {
t.Fatal(err)
}
// Once the graceful switch load balancer has been closed, any entrance
// function should be a no-op and return errBalancerClosed if the function
// returns an error.
// SwitchTo() should return an error due to the graceful switch load
// balancer having been closed already.
if err := gsb.SwitchTo(mockBalancerBuilder1{}); err != errBalancerClosed {
t.Fatalf("gsb.SwitchTo(%v) returned error %v, want %v", mockBalancerBuilder1{}, err, errBalancerClosed)
}
// UpdateClientConnState() should return an error due to the graceful switch
// load balancer having been closed already.
ccs := balancer.ClientConnState{
BalancerConfig: mockBalancerConfig{},
}
if err := gsb.UpdateClientConnState(ccs); err != errBalancerClosed {
t.Fatalf("gsb.UpdateCLientConnState(%v) returned error %v, want %v", ccs, err, errBalancerClosed)
}
// After the graceful switch load balancer has been closed, any resolver error
// shouldn't forward to either balancer, as the resolver error is a no-op
// and also even if not, the balancers should have been cleared from the
// graceful switch load balancer.
gsb.ResolverError(balancer.ErrBadResolverState)
sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
if err := currBal.waitForResolverError(sCtx, balancer.ErrBadResolverState); !strings.Contains(err.Error(), sCtx.Err().Error()) {
t.Fatal("the current balancer should not have received the resolver error after close")
}
sCtx, sCancel = context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
if err := pendBal.waitForResolverError(sCtx, balancer.ErrBadResolverState); !strings.Contains(err.Error(), sCtx.Err().Error()) {
t.Fatal("the pending balancer should not have received the resolver error after close")
}
}
// TestResolverError tests the functionality of a Resolver Error. If there is a
// current balancer, but no pending, the error should be forwarded to the
// current balancer. If there is both a current and pending balancer, the error
// should be forwarded to only the pending balancer.
func (s) TestResolverError(t *testing.T) {
_, gsb := setup(t)
gsb.SwitchTo(mockBalancerBuilder1{})
currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
// If there is only a current balancer present, the resolver error should be
// forwarded to the current balancer.
gsb.ResolverError(balancer.ErrBadResolverState)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
if err := currBal.waitForResolverError(ctx, balancer.ErrBadResolverState); err != nil {
t.Fatal(err)
}
gsb.SwitchTo(mockBalancerBuilder1{})
// If there is a pending balancer present, then a resolver error should be
// forwarded to only the pending balancer, not the current.
pendBal := gsb.balancerPending.Balancer.(*mockBalancer)
gsb.ResolverError(balancer.ErrBadResolverState)
// The Resolver Error should not be forwarded to the current load balancer.
sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
if err := currBal.waitForResolverError(sCtx, balancer.ErrBadResolverState); !strings.Contains(err.Error(), sCtx.Err().Error()) {
t.Fatal("the current balancer should not have received the resolver error after close")
}
// The Resolver Error should be forwarded to the pending load balancer.
if err := pendBal.waitForResolverError(ctx, balancer.ErrBadResolverState); err != nil {
t.Fatal(err)
}
}
// TestPendingReplacedByAnotherPending tests the scenario where a graceful
// switch balancer has a current and pending load balancer, and receives a
// SwitchTo() call, which then replaces the pending. This should cause the
// graceful switch balancer to clear pending state, close old pending SubConns,
// and Close() the pending balancer being replaced.
func (s) TestPendingReplacedByAnotherPending(t *testing.T) {
tcc, gsb := setup(t)
gsb.SwitchTo(mockBalancerBuilder1{})
currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
currBal.updateState(balancer.State{
ConnectivityState: connectivity.Ready,
})
// Populate pending with a SwitchTo() call.
gsb.SwitchTo(mockBalancerBuilder2{})
pendBal := gsb.balancerPending.Balancer.(*mockBalancer)
sc1, err := pendBal.newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
if err != nil {
t.Fatalf("error constructing newSubConn in gsb: %v", err)
}
// This picker never returns an error, which can help this this test verify
// whether this cached state will get cleared on a new pending balancer
// (will replace it with a picker that always errors).
pendBal.updateState(balancer.State{
ConnectivityState: connectivity.Connecting,
Picker: &neverErrPicker{},
})
// Replace pending with a SwitchTo() call.
gsb.SwitchTo(mockBalancerBuilder2{})
// The pending balancer being replaced should cause the graceful switch
// balancer to Remove() any created SubConns for the old pending balancer
// and also Close() the old pending balancer.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for a RemoveSubConn call on the ClientConn")
case sc := <-tcc.RemoveSubConnCh:
if !cmp.Equal(sc1, sc, cmp.AllowUnexported(testutils.TestSubConn{})) {
t.Fatalf("RemoveSubConn, want %v, got %v", sc1, sc)
}
}
if err := pendBal.waitForClose(ctx); err != nil {
t.Fatal(err)
}
// Switching the current out of READY should cause the pending LB to swap
// into current, causing the graceful switch balancer to update the
// ClientConn with the cached pending state. Since the new pending hasn't
// sent an Update, the default state with connectivity state CONNECTING and
// an errPicker should be sent to the ClientConn.
currBal.updateState(balancer.State{
ConnectivityState: connectivity.Idle,
})
// The update should contain a default connectivity state CONNECTING for the
// state of the new pending LB policy.
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateState() call on the ClientConn")
case state := <-tcc.NewStateCh:
if state != connectivity.Connecting {
t.Fatalf("UpdateState(), want connectivity state %v, got %v", connectivity.Connecting, state)
}
}
// The update should contain a default picker ErrPicker in the picker sent
// for the state of the new pending LB policy.
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateState() call on the ClientConn")
case picker := <-tcc.NewPickerCh:
if _, err := picker.Pick(balancer.PickInfo{}); err != balancer.ErrNoSubConnAvailable {
t.Fatalf("ClientConn should have received a never err picker from an UpdateState call")
}
}
}
// Picker which never errors here for test purposes (can fill up tests further up with this)
type neverErrPicker struct{}
func (p *neverErrPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
return balancer.PickResult{}, nil
}
// TestUpdateSubConnStateRace tests the race condition when the graceful switch
// load balancer receives a SubConnUpdate concurrently with an UpdateState()
// call, which can cause the balancer to forward the update to to be closed and
// cleared. The balancer API guarantees to never call any method the balancer
// after a Close() call, and the test verifies that doesn't happen within the
// graceful switch load balancer.
func (s) TestUpdateSubConnStateRace(t *testing.T) {
tcc, gsb := setup(t)
gsb.SwitchTo(verifyBalancerBuilder{})
gsb.SwitchTo(mockBalancerBuilder1{})
currBal := gsb.balancerCurrent.Balancer.(*verifyBalancer)
currBal.t = t
pendBal := gsb.balancerPending.Balancer.(*mockBalancer)
sc, err := currBal.newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
if err != nil {
t.Fatalf("error constructing newSubConn in gsb: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an NewSubConn call on the ClientConn")
case <-tcc.NewSubConnCh:
}
// Spawn a goroutine that constantly calls UpdateSubConn for the current
// balancer, which will get deleted in this testing goroutine.
finished := make(chan struct{})
go func() {
for {
select {
case <-finished:
return
default:
}
gsb.UpdateSubConnState(sc, balancer.SubConnState{
ConnectivityState: connectivity.Ready,
})
}
}()
time.Sleep(time.Millisecond)
// This UpdateState call causes current to be closed/cleared.
pendBal.updateState(balancer.State{
ConnectivityState: connectivity.Ready,
})
// From this, either one of two things happen. Either the graceful switch
// load balancer doesn't Close() the current balancer before it forwards the
// SubConn update to the child, and the call gets forwarded down to the
// current balancer, or it can Close() the current balancer in between
// reading the balancer pointer and writing to it, and in that case the old
// current balancer should not be updated, as the balancer has already been
// closed and the balancer API guarantees it.
close(finished)
}
// TestInlineCallbackInBuild tests the scenario where a balancer calls back into
// the balancer.ClientConn API inline from it's build function.
func (s) TestInlineCallbackInBuild(t *testing.T) {
tcc, gsb := setup(t)
// This build call should cause all of the inline updates to forward to the
// ClientConn.
gsb.SwitchTo(buildCallbackBalancerBuilder{})
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateState() call on the ClientConn")
case <-tcc.NewStateCh:
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an NewSubConn() call on the ClientConn")
case <-tcc.NewSubConnCh:
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses() call on the ClientConn")
case <-tcc.UpdateAddressesAddrsCh:
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an RemoveSubConn() call on the ClientConn")
case <-tcc.RemoveSubConnCh:
}
oldCurrent := gsb.balancerCurrent.Balancer.(*buildCallbackBal)
// Since the callback reports a state READY, this new inline balancer should
// be swapped to the current.
gsb.SwitchTo(buildCallbackBalancerBuilder{})
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateState() call on the ClientConn")
case <-tcc.NewStateCh:
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an NewSubConn() call on the ClientConn")
case <-tcc.NewSubConnCh:
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an UpdateAddresses() call on the ClientConn")
case <-tcc.UpdateAddressesAddrsCh:
}
select {
case <-ctx.Done():
t.Fatalf("timeout while waiting for an RemoveSubConn() call on the ClientConn")
case <-tcc.RemoveSubConnCh:
}
// The current balancer should be closed as a result of the swap.
if err := oldCurrent.waitForClose(ctx); err != nil {
t.Fatalf("error waiting for balancer close: %v", err)
}
// The old balancer should be deprecated and any calls from it should be a no-op.
oldCurrent.newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
defer sCancel()
select {
case <-tcc.NewSubConnCh:
t.Fatal("Deprecated LB calling NewSubConn() should not forward up to the ClientConn")
case <-sCtx.Done():
}
}
// TestExitIdle tests the ExitIdle operation on the Graceful Switch Balancer for
// both possible codepaths, one where the child implements ExitIdler interface
// and one where the child doesn't implement ExitIdler interface.
func (s) TestExitIdle(t *testing.T) {
_, gsb := setup(t)
// switch to a balancer that implements ExitIdle{} (will populate current).
gsb.SwitchTo(mockBalancerBuilder1{})
currBal := gsb.balancerCurrent.Balancer.(*mockBalancer)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
// exitIdle on the Graceful Switch Balancer should get forwarded to the
// current child as it implements exitIdle.
gsb.ExitIdle()
if err := currBal.waitForExitIdle(ctx); err != nil {
t.Fatal(err)
}
// switch to a balancer that doesn't implement ExitIdle{} (will populate
// pending).
gsb.SwitchTo(verifyBalancerBuilder{})
// call exitIdle concurrently with newSubConn to make sure there is not a
// data race.
done := make(chan struct{})
go func() {
gsb.ExitIdle()
close(done)
}()
pendBal := gsb.balancerPending.Balancer.(*verifyBalancer)
for i := 0; i < 10; i++ {
pendBal.newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
}
<-done
}
const balancerName1 = "mock_balancer_1"
const balancerName2 = "mock_balancer_2"
const verifyBalName = "verifyNoSubConnUpdateAfterCloseBalancer"
const buildCallbackBalName = "callbackInBuildBalancer"
type mockBalancerBuilder1 struct{}
func (mockBalancerBuilder1) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
return &mockBalancer{
ccsCh: testutils.NewChannel(),
scStateCh: testutils.NewChannel(),
resolverErrCh: testutils.NewChannel(),
closeCh: testutils.NewChannel(),
exitIdleCh: testutils.NewChannel(),
cc: cc,
}
}
func (mockBalancerBuilder1) Name() string {
return balancerName1
}
type mockBalancerConfig struct {
serviceconfig.LoadBalancingConfig
}
// mockBalancer is a fake balancer used to verify different actions from
// the gracefulswitch. It contains a bunch of channels to signal different events
// to the test.
type mockBalancer struct {
// ccsCh is a channel used to signal the receipt of a ClientConn update.
ccsCh *testutils.Channel
// scStateCh is a channel used to signal the receipt of a SubConn update.
scStateCh *testutils.Channel
// resolverErrCh is a channel used to signal a resolver error.
resolverErrCh *testutils.Channel
// closeCh is a channel used to signal the closing of this balancer.
closeCh *testutils.Channel
// exitIdleCh is a channel used to signal the receipt of an ExitIdle call.
exitIdleCh *testutils.Channel
// Hold onto ClientConn wrapper to communicate with it
cc balancer.ClientConn
}
type subConnWithState struct {
sc balancer.SubConn
state balancer.SubConnState
}
func (mb1 *mockBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error {
// Need to verify this call...use a channel?...all of these will need verification
mb1.ccsCh.Send(ccs)
return nil
}
func (mb1 *mockBalancer) ResolverError(err error) {
mb1.resolverErrCh.Send(err)
}
func (mb1 *mockBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
mb1.scStateCh.Send(subConnWithState{sc: sc, state: state})
}
func (mb1 *mockBalancer) Close() {
mb1.closeCh.Send(struct{}{})
}
func (mb1 *mockBalancer) ExitIdle() {
mb1.exitIdleCh.Send(struct{}{})
}
// waitForClientConnUpdate verifies if the mockBalancer receives the
// provided ClientConnState within a reasonable amount of time.
func (mb1 *mockBalancer) waitForClientConnUpdate(ctx context.Context, wantCCS balancer.ClientConnState) error {
ccs, err := mb1.ccsCh.Receive(ctx)
if err != nil {
return fmt.Errorf("error waiting for ClientConnUpdate: %v", err)
}
gotCCS := ccs.(balancer.ClientConnState)
if diff := cmp.Diff(gotCCS, wantCCS, cmpopts.IgnoreFields(resolver.State{}, "Attributes")); diff != "" {
return fmt.Errorf("error in ClientConnUpdate: received unexpected ClientConnState, diff (-got +want): %v", diff)
}
return nil
}
// waitForSubConnUpdate verifies if the mockBalancer receives the provided
// SubConn update before the context expires.
func (mb1 *mockBalancer) waitForSubConnUpdate(ctx context.Context, wantSCS subConnWithState) error {
scs, err := mb1.scStateCh.Receive(ctx)
if err != nil {
return fmt.Errorf("error waiting for SubConnUpdate: %v", err)
}
gotSCS := scs.(subConnWithState)
if !cmp.Equal(gotSCS, wantSCS, cmp.AllowUnexported(subConnWithState{}, testutils.TestSubConn{})) {
return fmt.Errorf("error in SubConnUpdate: received SubConnState: %+v, want %+v", gotSCS, wantSCS)
}
return nil
}
// waitForResolverError verifies if the mockBalancer receives the provided
// resolver error before the context expires.
func (mb1 *mockBalancer) waitForResolverError(ctx context.Context, wantErr error) error {
gotErr, err := mb1.resolverErrCh.Receive(ctx)
if err != nil {
return fmt.Errorf("error waiting for resolver error: %v", err)
}
if gotErr != wantErr {
return fmt.Errorf("received resolver error: %v, want %v", gotErr, wantErr)
}
return nil
}
// waitForClose verifies that the mockBalancer is closed before the context
// expires.
func (mb1 *mockBalancer) waitForClose(ctx context.Context) error {
if _, err := mb1.closeCh.Receive(ctx); err != nil {
return fmt.Errorf("error waiting for Close(): %v", err)
}
return nil
}
// waitForExitIdle verifies that ExitIdle gets called on the mockBalancer before
// the context expires.
func (mb1 *mockBalancer) waitForExitIdle(ctx context.Context) error {
if _, err := mb1.exitIdleCh.Receive(ctx); err != nil {
return fmt.Errorf("error waiting for ExitIdle(): %v", err)
}
return nil
}
func (mb1 *mockBalancer) updateState(state balancer.State) {
mb1.cc.UpdateState(state)
}
func (mb1 *mockBalancer) newSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
return mb1.cc.NewSubConn(addrs, opts)
}
func (mb1 *mockBalancer) updateAddresses(sc balancer.SubConn, addrs []resolver.Address) {
mb1.cc.UpdateAddresses(sc, addrs)
}
func (mb1 *mockBalancer) removeSubConn(sc balancer.SubConn) {
mb1.cc.RemoveSubConn(sc)
}
type mockBalancerBuilder2 struct{}
func (mockBalancerBuilder2) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
return &mockBalancer{
ccsCh: testutils.NewChannel(),
scStateCh: testutils.NewChannel(),
resolverErrCh: testutils.NewChannel(),
closeCh: testutils.NewChannel(),
cc: cc,
}
}
func (mockBalancerBuilder2) Name() string {
return balancerName2
}
type verifyBalancerBuilder struct{}
func (verifyBalancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
return &verifyBalancer{
closed: grpcsync.NewEvent(),
cc: cc,
}
}
func (verifyBalancerBuilder) Name() string {
return verifyBalName
}
// verifyBalancer is a balancer that verifies that after a Close() call, an
// updateSubConnState() call never happens.
type verifyBalancer struct {
closed *grpcsync.Event
// Hold onto the ClientConn wrapper to communicate with it.
cc balancer.ClientConn
// To fail the test if UpdateSubConnState gets called after Close().
t *testing.T
}
func (vb *verifyBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error {
return nil
}
func (vb *verifyBalancer) ResolverError(err error) {}
func (vb *verifyBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
if vb.closed.HasFired() {
vb.t.Fatal("UpdateSubConnState was called after Close(), which breaks the balancer API")
}
}
func (vb *verifyBalancer) Close() {
vb.closed.Fire()
}
func (vb *verifyBalancer) newSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
return vb.cc.NewSubConn(addrs, opts)
}
type buildCallbackBalancerBuilder struct{}
func (buildCallbackBalancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
b := &buildCallbackBal{
cc: cc,
closeCh: testutils.NewChannel(),
}
b.updateState(balancer.State{
ConnectivityState: connectivity.Connecting,
})
sc, err := b.newSubConn([]resolver.Address{}, balancer.NewSubConnOptions{})
if err != nil {
return nil
}
b.updateAddresses(sc, []resolver.Address{})
b.removeSubConn(sc)
return b
}
func (buildCallbackBalancerBuilder) Name() string {
return buildCallbackBalName
}
type buildCallbackBal struct {
// Hold onto the ClientConn wrapper to communicate with it.
cc balancer.ClientConn
// closeCh is a channel used to signal the closing of this balancer.
closeCh *testutils.Channel
}
func (bcb *buildCallbackBal) UpdateClientConnState(ccs balancer.ClientConnState) error {
return nil
}
func (bcb *buildCallbackBal) ResolverError(err error) {}
func (bcb *buildCallbackBal) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {}
func (bcb *buildCallbackBal) Close() {
bcb.closeCh.Send(struct{}{})
}
func (bcb *buildCallbackBal) updateState(state balancer.State) {
bcb.cc.UpdateState(state)
}
func (bcb *buildCallbackBal) newSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
return bcb.cc.NewSubConn(addrs, opts)
}
func (bcb *buildCallbackBal) updateAddresses(sc balancer.SubConn, addrs []resolver.Address) {
bcb.cc.UpdateAddresses(sc, addrs)
}
func (bcb *buildCallbackBal) removeSubConn(sc balancer.SubConn) {
bcb.cc.RemoveSubConn(sc)
}
// waitForClose verifies that the mockBalancer is closed before the context
// expires.
func (bcb *buildCallbackBal) waitForClose(ctx context.Context) error {
if _, err := bcb.closeCh.Receive(ctx); err != nil {
return err
}
return nil
}
|