Skip to content

Commit 978e2a4

Browse files
committed
update test to use custom lb policy
1 parent 69769dc commit 978e2a4

1 file changed

Lines changed: 44 additions & 35 deletions

File tree

internal/xds/balancer/clusterimpl/tests/balancer_test.go

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
"google.golang.org/protobuf/types/known/durationpb"
5656
"google.golang.org/protobuf/types/known/wrapperspb"
5757

58+
v3xdsxdstypepb "github.com/cncf/xds/go/xds/type/v3"
5859
v3clusterpb "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
5960
v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
6061
v3endpointpb "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
@@ -64,6 +65,7 @@ import (
6465
v3lrspb "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v3"
6566
testgrpc "google.golang.org/grpc/interop/grpc_testing"
6667
testpb "google.golang.org/grpc/interop/grpc_testing"
68+
"google.golang.org/protobuf/types/known/structpb"
6769

6870
_ "google.golang.org/grpc/xds"
6971
)
@@ -1124,6 +1126,40 @@ func (s) TestUpdateLRSServerToNil(t *testing.T) {
11241126
// Test verifies that child policy was updated on receipt of
11251127
// configuration update.
11261128
func (s) TestChildPolicyChangeOnConfigUpdate(t *testing.T) {
1129+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1130+
defer cancel()
1131+
1132+
const customLBPolicy = "test_custom_lb_policy"
1133+
1134+
// Register stub customLBPolicy LB policy so that we can catch config changes.
1135+
pfBuilder := balancer.Get(pickfirst.Name)
1136+
lbCfgCh := make(chan serviceconfig.LoadBalancingConfig, 1)
1137+
var updatedChildPolicy atomic.Pointer[string]
1138+
1139+
stub.Register(customLBPolicy, stub.BalancerFuncs{
1140+
ParseConfig: func(lbCfg json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
1141+
return pfBuilder.(balancer.ConfigParser).ParseConfig(lbCfg)
1142+
},
1143+
Init: func(bd *stub.BalancerData) {
1144+
bd.ChildBalancer = pfBuilder.Build(bd.ClientConn, bd.BuildOptions)
1145+
},
1146+
UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error {
1147+
name := customLBPolicy
1148+
updatedChildPolicy.Store(&name)
1149+
select {
1150+
case lbCfgCh <- ccs.BalancerConfig:
1151+
case <-ctx.Done():
1152+
t.Error("Timed out while waiting for BalancerConfig, context deadline exceeded")
1153+
}
1154+
return bd.ChildBalancer.UpdateClientConnState(ccs)
1155+
},
1156+
Close: func(bd *stub.BalancerData) {
1157+
bd.ChildBalancer.Close()
1158+
},
1159+
})
1160+
1161+
defer internal.BalancerUnregister(customLBPolicy)
1162+
11271163
// Create an xDS management server.
11281164
mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{AllowResourceSubset: true})
11291165
defer mgmtServer.Stop()
@@ -1158,8 +1194,6 @@ func (s) TestChildPolicyChangeOnConfigUpdate(t *testing.T) {
11581194
Port: testutils.ParsePort(t, server.Address),
11591195
})
11601196

1161-
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1162-
defer cancel()
11631197
if err := mgmtServer.Update(ctx, resources); err != nil {
11641198
t.Fatalf("Failed to update xDS resources: %v", err)
11651199
}
@@ -1175,39 +1209,14 @@ func (s) TestChildPolicyChangeOnConfigUpdate(t *testing.T) {
11751209
t.Fatalf("client.EmptyCall() failed: %v", err)
11761210
}
11771211

1178-
// Register stub pickfirst LB policy so that we can catch config changes.
1179-
pfBuilder := balancer.Get(pickfirst.Name)
1180-
internal.BalancerUnregister(pfBuilder.Name())
1181-
lbCfgCh := make(chan serviceconfig.LoadBalancingConfig, 1)
1182-
var updatedChildPolicy atomic.Pointer[string]
1183-
stub.Register(pfBuilder.Name(), stub.BalancerFuncs{
1184-
ParseConfig: func(lbCfg json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
1185-
return pfBuilder.(balancer.ConfigParser).ParseConfig(lbCfg)
1186-
},
1187-
Init: func(bd *stub.BalancerData) {
1188-
bd.ChildBalancer = pfBuilder.Build(bd.ClientConn, bd.BuildOptions)
1189-
},
1190-
UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error {
1191-
name := pfBuilder.Name()
1192-
updatedChildPolicy.Store(&name)
1193-
select {
1194-
case lbCfgCh <- ccs.BalancerConfig:
1195-
case <-ctx.Done():
1196-
t.Error("Timed out while waiting for BalancerConfig, context deadline exceeded")
1197-
}
1198-
return bd.ChildBalancer.UpdateClientConnState(ccs)
1199-
},
1200-
Close: func(bd *stub.BalancerData) {
1201-
bd.ChildBalancer.Close()
1202-
},
1203-
})
1204-
defer balancer.Register(pfBuilder)
1205-
1206-
// Now update the cluster to use "pick_first" as the endpoint picking policy.
1212+
// Update the cluster to use customLBPolicy as the endpoint picking policy.
12071213
resources.Clusters[0].LoadBalancingPolicy = &v3clusterpb.LoadBalancingPolicy{
12081214
Policies: []*v3clusterpb.LoadBalancingPolicy_Policy{{
12091215
TypedExtensionConfig: &v3corepb.TypedExtensionConfig{
1210-
TypedConfig: testutils.MarshalAny(t, &v3pickfirstpb.PickFirst{}),
1216+
TypedConfig: testutils.MarshalAny(t, &v3xdsxdstypepb.TypedStruct{
1217+
TypeUrl: "type.googleapis.com/" + customLBPolicy,
1218+
Value: &structpb.Struct{},
1219+
}),
12111220
},
12121221
}},
12131222
}
@@ -1217,16 +1226,16 @@ func (s) TestChildPolicyChangeOnConfigUpdate(t *testing.T) {
12171226

12181227
select {
12191228
case <-ctx.Done():
1220-
t.Fatalf("Timeout waiting for pickfirst child policy config")
1229+
t.Fatalf("Timeout waiting for child policy config")
12211230
case <-lbCfgCh:
12221231
}
12231232

1224-
if p := updatedChildPolicy.Load(); p == nil || *p != pfBuilder.Name() {
1233+
if p := updatedChildPolicy.Load(); p == nil || *p != customLBPolicy {
12251234
var got string
12261235
if p != nil {
12271236
got = *p
12281237
}
1229-
t.Fatalf("Unexpected child policy after config update, got %q, want %q", got, pfBuilder.Name())
1238+
t.Fatalf("Unexpected child policy after config update, got %q, want %q", got, customLBPolicy)
12301239
}
12311240

12321241
// New RPC should still be routed successfully

0 commit comments

Comments
 (0)