@@ -20,6 +20,7 @@ package clusterimpl
20
20
21
21
import (
22
22
"context"
23
+ "encoding/json"
23
24
"errors"
24
25
"fmt"
25
26
"strings"
@@ -40,6 +41,7 @@ import (
40
41
"google.golang.org/grpc/internal/xds"
41
42
"google.golang.org/grpc/internal/xds/bootstrap"
42
43
"google.golang.org/grpc/resolver"
44
+ "google.golang.org/grpc/serviceconfig"
43
45
xdsinternal "google.golang.org/grpc/xds/internal"
44
46
"google.golang.org/grpc/xds/internal/testutils/fakeclient"
45
47
"google.golang.org/grpc/xds/internal/xdsclient"
@@ -839,6 +841,108 @@ func (s) TestUpdateLRSServer(t *testing.T) {
839
841
}
840
842
}
841
843
844
+ // Test verifies that child policies was updated on receipt of
845
+ // configuration update.
846
+ func (s ) TestChildPolicyUpdatedOnConfigUpdate (t * testing.T ) {
847
+ xdsC := fakeclient .NewClient ()
848
+
849
+ builder := balancer .Get (Name )
850
+ cc := testutils .NewBalancerClientConn (t )
851
+ b := builder .Build (cc , balancer.BuildOptions {})
852
+ defer b .Close ()
853
+
854
+ // Keep track of which child policy was updated
855
+ updatedChildPolicy := ""
856
+
857
+ // Create stub balancers to track config updates
858
+ const (
859
+ childPolicyName1 = "stubBalancer1"
860
+ childPolicyName2 = "stubBalancer2"
861
+ )
862
+
863
+ stub .Register (childPolicyName1 , stub.BalancerFuncs {
864
+ UpdateClientConnState : func (_ * stub.BalancerData , _ balancer.ClientConnState ) error {
865
+ updatedChildPolicy = childPolicyName1
866
+ return nil
867
+ },
868
+ })
869
+
870
+ stub .Register (childPolicyName2 , stub.BalancerFuncs {
871
+ UpdateClientConnState : func (_ * stub.BalancerData , _ balancer.ClientConnState ) error {
872
+ updatedChildPolicy = childPolicyName2
873
+ return nil
874
+ },
875
+ })
876
+
877
+ // Initial config update with childPolicyName1
878
+ if err := b .UpdateClientConnState (balancer.ClientConnState {
879
+ ResolverState : xdsclient .SetClient (resolver.State {Addresses : testBackendAddrs }, xdsC ),
880
+ BalancerConfig : & LBConfig {
881
+ Cluster : testClusterName ,
882
+ ChildPolicy : & internalserviceconfig.BalancerConfig {
883
+ Name : childPolicyName1 ,
884
+ },
885
+ },
886
+ }); err != nil {
887
+ t .Fatalf ("Error updating the config: %v" , err )
888
+ }
889
+
890
+ if updatedChildPolicy != childPolicyName1 {
891
+ t .Fatal ("Child policy 1 was not updated on initial configuration update." )
892
+ }
893
+
894
+ // Second config update with childPolicyName2
895
+ if err := b .UpdateClientConnState (balancer.ClientConnState {
896
+ ResolverState : xdsclient .SetClient (resolver.State {Addresses : testBackendAddrs }, xdsC ),
897
+ BalancerConfig : & LBConfig {
898
+ Cluster : testClusterName ,
899
+ ChildPolicy : & internalserviceconfig.BalancerConfig {
900
+ Name : childPolicyName2 ,
901
+ },
902
+ },
903
+ }); err != nil {
904
+ t .Fatalf ("Error updating the config: %v" , err )
905
+ }
906
+
907
+ if updatedChildPolicy != childPolicyName2 {
908
+ t .Fatal ("Child policy 2 was not updated after child policy name change." )
909
+ }
910
+ }
911
+
912
+ // Test verifies that config update fails if child policy config
913
+ // failed to parse.
914
+ func (s ) TestFailedToParseChildPolicyConfig (t * testing.T ) {
915
+ xdsC := fakeclient .NewClient ()
916
+
917
+ builder := balancer .Get (Name )
918
+ cc := testutils .NewBalancerClientConn (t )
919
+ b := builder .Build (cc , balancer.BuildOptions {})
920
+ defer b .Close ()
921
+
922
+ // Create a stub balancer which fails to ParseConfig.
923
+ const parseConfigError = "failed to parse config"
924
+ const childPolicyName = "stubBalancer-FailedToParseChildPolicyConfig"
925
+ stub .Register (childPolicyName , stub.BalancerFuncs {
926
+ ParseConfig : func (_ json.RawMessage ) (serviceconfig.LoadBalancingConfig , error ) {
927
+ return nil , errors .New (parseConfigError )
928
+ },
929
+ })
930
+
931
+ err := b .UpdateClientConnState (balancer.ClientConnState {
932
+ ResolverState : xdsclient .SetClient (resolver.State {Addresses : testBackendAddrs }, xdsC ),
933
+ BalancerConfig : & LBConfig {
934
+ Cluster : testClusterName ,
935
+ ChildPolicy : & internalserviceconfig.BalancerConfig {
936
+ Name : childPolicyName ,
937
+ },
938
+ },
939
+ })
940
+
941
+ if err == nil || ! strings .Contains (err .Error (), parseConfigError ) {
942
+ t .Fatalf ("Got error: %v, want error: %s" , err , parseConfigError )
943
+ }
944
+ }
945
+
842
946
func assertString (f func () (string , error )) string {
843
947
s , err := f ()
844
948
if err != nil {
0 commit comments