123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- // +build go1.12
- /*
- *
- * Copyright 2019 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 xds
- import (
- "encoding/json"
- "reflect"
- "sync"
- "testing"
- "time"
- "github.com/golang/protobuf/proto"
- "google.golang.org/grpc/balancer"
- discoverypb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/discovery"
- edspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/eds"
- "google.golang.org/grpc/connectivity"
- "google.golang.org/grpc/internal/grpctest"
- "google.golang.org/grpc/internal/leakcheck"
- "google.golang.org/grpc/resolver"
- )
- var lbABuilder *balancerABuilder
- func init() {
- lbABuilder = &balancerABuilder{}
- balancer.Register(lbABuilder)
- balancer.Register(&balancerBBuilder{})
- }
- type s struct{}
- func (s) Teardown(t *testing.T) {
- leakcheck.Check(t)
- }
- func Test(t *testing.T) {
- grpctest.RunSubTests(t, s{})
- }
- type lbPolicy string
- const (
- fakeBalancerA lbPolicy = "fake_balancer_A"
- fakeBalancerB lbPolicy = "fake_balancer_B"
- fakeBalancerC lbPolicy = "fake_balancer_C"
- )
- var (
- testBalancerNameFooBar = "foo.bar"
- testBalancerConfigFooBar, _ = json.Marshal(&testBalancerConfig{
- BalancerName: testBalancerNameFooBar,
- ChildPolicy: []lbPolicy{fakeBalancerA},
- FallbackPolicy: []lbPolicy{fakeBalancerA},
- })
- specialAddrForBalancerA = resolver.Address{Addr: "this.is.balancer.A"}
- specialAddrForBalancerB = resolver.Address{Addr: "this.is.balancer.B"}
- // mu protects the access of latestFakeEdsBalancer
- mu sync.Mutex
- latestFakeEdsBalancer *fakeEDSBalancer
- )
- type testBalancerConfig struct {
- BalancerName string `json:"balancerName,omitempty"`
- ChildPolicy []lbPolicy `json:"childPolicy,omitempty"`
- FallbackPolicy []lbPolicy `json:"fallbackPolicy,omitempty"`
- }
- func (l *lbPolicy) UnmarshalJSON(b []byte) error {
- // no need to implement, not used.
- return nil
- }
- func (l lbPolicy) MarshalJSON() ([]byte, error) {
- m := make(map[string]struct{})
- m[string(l)] = struct{}{}
- return json.Marshal(m)
- }
- type balancerABuilder struct {
- mu sync.Mutex
- lastBalancer *balancerA
- }
- func (b *balancerABuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
- b.mu.Lock()
- b.lastBalancer = &balancerA{cc: cc, subconnStateChange: make(chan *scStateChange, 10)}
- b.mu.Unlock()
- return b.lastBalancer
- }
- func (b *balancerABuilder) Name() string {
- return string(fakeBalancerA)
- }
- func (b *balancerABuilder) getLastBalancer() *balancerA {
- b.mu.Lock()
- defer b.mu.Unlock()
- return b.lastBalancer
- }
- func (b *balancerABuilder) clearLastBalancer() {
- b.mu.Lock()
- defer b.mu.Unlock()
- b.lastBalancer = nil
- }
- type balancerBBuilder struct{}
- func (b *balancerBBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
- return &balancerB{cc: cc}
- }
- func (*balancerBBuilder) Name() string {
- return string(fakeBalancerB)
- }
- type balancerA struct {
- cc balancer.ClientConn
- subconnStateChange chan *scStateChange
- }
- func (b *balancerA) HandleSubConnStateChange(sc balancer.SubConn, state connectivity.State) {
- b.subconnStateChange <- &scStateChange{sc: sc, state: state}
- }
- func (b *balancerA) HandleResolvedAddrs(addrs []resolver.Address, err error) {
- _, _ = b.cc.NewSubConn(append(addrs, specialAddrForBalancerA), balancer.NewSubConnOptions{})
- }
- func (b *balancerA) Close() {}
- type balancerB struct {
- cc balancer.ClientConn
- }
- func (balancerB) HandleSubConnStateChange(sc balancer.SubConn, state connectivity.State) {
- panic("implement me")
- }
- func (b *balancerB) HandleResolvedAddrs(addrs []resolver.Address, err error) {
- _, _ = b.cc.NewSubConn(append(addrs, specialAddrForBalancerB), balancer.NewSubConnOptions{})
- }
- func (balancerB) Close() {}
- func newTestClientConn() *testClientConn {
- return &testClientConn{
- newSubConns: make(chan []resolver.Address, 10),
- }
- }
- type testClientConn struct {
- newSubConns chan []resolver.Address
- }
- func (t *testClientConn) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
- t.newSubConns <- addrs
- return nil, nil
- }
- func (testClientConn) RemoveSubConn(balancer.SubConn) {
- }
- func (testClientConn) UpdateBalancerState(s connectivity.State, p balancer.Picker) {
- }
- func (testClientConn) ResolveNow(resolver.ResolveNowOption) {}
- func (testClientConn) Target() string {
- return testServiceName
- }
- type scStateChange struct {
- sc balancer.SubConn
- state connectivity.State
- }
- type fakeEDSBalancer struct {
- cc balancer.ClientConn
- edsChan chan *edspb.ClusterLoadAssignment
- childPolicy chan *loadBalancingConfig
- fallbackPolicy chan *loadBalancingConfig
- subconnStateChange chan *scStateChange
- }
- func (f *fakeEDSBalancer) HandleSubConnStateChange(sc balancer.SubConn, state connectivity.State) {
- f.subconnStateChange <- &scStateChange{sc: sc, state: state}
- }
- func (f *fakeEDSBalancer) Close() {
- mu.Lock()
- defer mu.Unlock()
- latestFakeEdsBalancer = nil
- }
- func (f *fakeEDSBalancer) HandleEDSResponse(edsResp *edspb.ClusterLoadAssignment) {
- f.edsChan <- edsResp
- }
- func (f *fakeEDSBalancer) HandleChildPolicy(name string, config json.RawMessage) {
- f.childPolicy <- &loadBalancingConfig{
- Name: name,
- Config: config,
- }
- }
- func newFakeEDSBalancer(cc balancer.ClientConn) edsBalancerInterface {
- lb := &fakeEDSBalancer{
- cc: cc,
- edsChan: make(chan *edspb.ClusterLoadAssignment, 10),
- childPolicy: make(chan *loadBalancingConfig, 10),
- fallbackPolicy: make(chan *loadBalancingConfig, 10),
- subconnStateChange: make(chan *scStateChange, 10),
- }
- mu.Lock()
- latestFakeEdsBalancer = lb
- mu.Unlock()
- return lb
- }
- func getLatestEdsBalancer() *fakeEDSBalancer {
- mu.Lock()
- defer mu.Unlock()
- return latestFakeEdsBalancer
- }
- type fakeSubConn struct{}
- func (*fakeSubConn) UpdateAddresses([]resolver.Address) {
- panic("implement me")
- }
- func (*fakeSubConn) Connect() {
- panic("implement me")
- }
- func (s) TestXdsBalanceHandleResolvedAddrs(t *testing.T) {
- startupTimeout = 500 * time.Millisecond
- defer func() { startupTimeout = defaultTimeout }()
- builder := balancer.Get("xds")
- cc := newTestClientConn()
- lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
- if !ok {
- t.Fatalf("unable to type assert to *xdsBalancer")
- }
- defer lb.Close()
- if err := lb.HandleBalancerConfig(json.RawMessage(testBalancerConfigFooBar)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(testBalancerConfigFooBar), err)
- }
- addrs := []resolver.Address{{Addr: "1.1.1.1:10001"}, {Addr: "2.2.2.2:10002"}, {Addr: "3.3.3.3:10003"}}
- for i := 0; i < 3; i++ {
- lb.HandleResolvedAddrs(addrs, nil)
- select {
- case nsc := <-cc.newSubConns:
- if !reflect.DeepEqual(append(addrs, specialAddrForBalancerA), nsc) {
- t.Fatalf("got new subconn address %v, want %v", nsc, append(addrs, specialAddrForBalancerA))
- }
- case <-time.After(2 * time.Second):
- t.Fatalf("timeout when geting new subconn result")
- }
- addrs = addrs[:2-i]
- }
- }
- func (s) TestXdsBalanceHandleBalancerConfigBalancerNameUpdate(t *testing.T) {
- startupTimeout = 500 * time.Millisecond
- originalNewEDSBalancer := newEDSBalancer
- newEDSBalancer = newFakeEDSBalancer
- defer func() {
- startupTimeout = defaultTimeout
- newEDSBalancer = originalNewEDSBalancer
- }()
- builder := balancer.Get("xds")
- cc := newTestClientConn()
- lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
- if !ok {
- t.Fatalf("unable to type assert to *xdsBalancer")
- }
- defer lb.Close()
- if err := lb.HandleBalancerConfig(json.RawMessage(testBalancerConfigFooBar)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(testBalancerConfigFooBar), err)
- }
- addrs := []resolver.Address{{Addr: "1.1.1.1:10001"}, {Addr: "2.2.2.2:10002"}, {Addr: "3.3.3.3:10003"}}
- lb.HandleResolvedAddrs(addrs, nil)
- // verify fallback takes over
- select {
- case nsc := <-cc.newSubConns:
- if !reflect.DeepEqual(append(addrs, specialAddrForBalancerA), nsc) {
- t.Fatalf("got new subconn address %v, want %v", nsc, append(addrs, specialAddrForBalancerA))
- }
- case <-time.After(2 * time.Second):
- t.Fatalf("timeout when geting new subconn result")
- }
- var cleanups []func()
- defer func() {
- for _, cleanup := range cleanups {
- cleanup()
- }
- }()
- // In the first iteration, an eds balancer takes over fallback balancer
- // In the second iteration, a new xds client takes over previous one.
- for i := 0; i < 2; i++ {
- addr, td, cleanup := setupServer(t)
- cleanups = append(cleanups, cleanup)
- workingBalancerConfig, _ := json.Marshal(&testBalancerConfig{
- BalancerName: addr,
- ChildPolicy: []lbPolicy{fakeBalancerA},
- FallbackPolicy: []lbPolicy{fakeBalancerA},
- })
- if err := lb.HandleBalancerConfig(json.RawMessage(workingBalancerConfig)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(workingBalancerConfig), err)
- }
- td.sendResp(&response{resp: testEDSRespWithoutEndpoints})
- var j int
- for j = 0; j < 10; j++ {
- if edsLB := getLatestEdsBalancer(); edsLB != nil { // edsLB won't change between the two iterations
- select {
- case gotEDS := <-edsLB.edsChan:
- if !proto.Equal(gotEDS, testClusterLoadAssignmentWithoutEndpoints) {
- t.Fatalf("edsBalancer got eds: %v, want %v", gotEDS, testClusterLoadAssignmentWithoutEndpoints)
- }
- case <-time.After(time.Second):
- t.Fatal("haven't got EDS update after 1s")
- }
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- if j == 10 {
- t.Fatal("edsBalancer instance has not been created or updated after 1s")
- }
- }
- }
- // switch child policy, lb stays the same
- // cds->eds or eds -> cds, restart xdsClient, lb stays the same
- func (s) TestXdsBalanceHandleBalancerConfigChildPolicyUpdate(t *testing.T) {
- originalNewEDSBalancer := newEDSBalancer
- newEDSBalancer = newFakeEDSBalancer
- defer func() {
- newEDSBalancer = originalNewEDSBalancer
- }()
- builder := balancer.Get("xds")
- cc := newTestClientConn()
- lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
- if !ok {
- t.Fatalf("unable to type assert to *xdsBalancer")
- }
- defer lb.Close()
- var cleanups []func()
- defer func() {
- for _, cleanup := range cleanups {
- cleanup()
- }
- }()
- for _, test := range []struct {
- cfg *testBalancerConfig
- responseToSend *discoverypb.DiscoveryResponse
- expectedChildPolicy *loadBalancingConfig
- }{
- {
- cfg: &testBalancerConfig{
- ChildPolicy: []lbPolicy{fakeBalancerA},
- },
- responseToSend: testEDSRespWithoutEndpoints,
- expectedChildPolicy: &loadBalancingConfig{
- Name: string(fakeBalancerA),
- Config: json.RawMessage(`{}`),
- },
- },
- {
- cfg: &testBalancerConfig{
- ChildPolicy: []lbPolicy{fakeBalancerB},
- },
- expectedChildPolicy: &loadBalancingConfig{
- Name: string(fakeBalancerB),
- Config: json.RawMessage(`{}`),
- },
- },
- {
- cfg: &testBalancerConfig{},
- responseToSend: testCDSResp,
- expectedChildPolicy: &loadBalancingConfig{
- Name: "ROUND_ROBIN",
- },
- },
- } {
- addr, td, cleanup := setupServer(t)
- cleanups = append(cleanups, cleanup)
- test.cfg.BalancerName = addr
- workingBalancerConfig, _ := json.Marshal(test.cfg)
- if err := lb.HandleBalancerConfig(json.RawMessage(workingBalancerConfig)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(workingBalancerConfig), err)
- }
- if test.responseToSend != nil {
- td.sendResp(&response{resp: test.responseToSend})
- }
- var i int
- for i = 0; i < 10; i++ {
- if edsLB := getLatestEdsBalancer(); edsLB != nil {
- select {
- case childPolicy := <-edsLB.childPolicy:
- if !reflect.DeepEqual(childPolicy, test.expectedChildPolicy) {
- t.Fatalf("got childPolicy %v, want %v", childPolicy, test.expectedChildPolicy)
- }
- case <-time.After(time.Second):
- t.Fatal("haven't got policy update after 1s")
- }
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- if i == 10 {
- t.Fatal("edsBalancer instance has not been created or updated after 1s")
- }
- }
- }
- // not in fallback mode, overwrite fallback info.
- // in fallback mode, update config or switch balancer.
- func (s) TestXdsBalanceHandleBalancerConfigFallbackUpdate(t *testing.T) {
- originalNewEDSBalancer := newEDSBalancer
- newEDSBalancer = newFakeEDSBalancer
- defer func() {
- newEDSBalancer = originalNewEDSBalancer
- }()
- builder := balancer.Get("xds")
- cc := newTestClientConn()
- lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
- if !ok {
- t.Fatalf("unable to type assert to *xdsBalancer")
- }
- defer lb.Close()
- addr, td, cleanup := setupServer(t)
- cfg := &testBalancerConfig{
- BalancerName: addr,
- ChildPolicy: []lbPolicy{fakeBalancerA},
- FallbackPolicy: []lbPolicy{fakeBalancerA},
- }
- workingBalancerConfig, _ := json.Marshal(cfg)
- if err := lb.HandleBalancerConfig(json.RawMessage(workingBalancerConfig)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(workingBalancerConfig), err)
- }
- cfg.FallbackPolicy = []lbPolicy{fakeBalancerB}
- workingBalancerConfig, _ = json.Marshal(cfg)
- if err := lb.HandleBalancerConfig(json.RawMessage(workingBalancerConfig)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(workingBalancerConfig), err)
- }
- td.sendResp(&response{resp: testEDSRespWithoutEndpoints})
- var i int
- for i = 0; i < 10; i++ {
- if edsLB := getLatestEdsBalancer(); edsLB != nil {
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- if i == 10 {
- t.Fatal("edsBalancer instance has not been created and assigned to lb.xdsLB after 1s")
- }
- cleanup()
- addrs := []resolver.Address{{Addr: "1.1.1.1:10001"}, {Addr: "2.2.2.2:10002"}, {Addr: "3.3.3.3:10003"}}
- lb.HandleResolvedAddrs(addrs, nil)
- // verify fallback balancer B takes over
- select {
- case nsc := <-cc.newSubConns:
- if !reflect.DeepEqual(append(addrs, specialAddrForBalancerB), nsc) {
- t.Fatalf("got new subconn address %v, want %v", nsc, append(addrs, specialAddrForBalancerB))
- }
- case <-time.After(5 * time.Second):
- t.Fatalf("timeout when geting new subconn result")
- }
- cfg.FallbackPolicy = []lbPolicy{fakeBalancerA}
- workingBalancerConfig, _ = json.Marshal(cfg)
- if err := lb.HandleBalancerConfig(json.RawMessage(workingBalancerConfig)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(workingBalancerConfig), err)
- }
- // verify fallback balancer A takes over
- select {
- case nsc := <-cc.newSubConns:
- if !reflect.DeepEqual(append(addrs, specialAddrForBalancerA), nsc) {
- t.Fatalf("got new subconn address %v, want %v", nsc, append(addrs, specialAddrForBalancerA))
- }
- case <-time.After(2 * time.Second):
- t.Fatalf("timeout when geting new subconn result")
- }
- }
- func (s) TestXdsBalancerHandlerSubConnStateChange(t *testing.T) {
- originalNewEDSBalancer := newEDSBalancer
- newEDSBalancer = newFakeEDSBalancer
- defer func() {
- newEDSBalancer = originalNewEDSBalancer
- }()
- builder := balancer.Get("xds")
- cc := newTestClientConn()
- lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
- if !ok {
- t.Fatalf("unable to type assert to *xdsBalancer")
- }
- defer lb.Close()
- addr, td, cleanup := setupServer(t)
- defer cleanup()
- cfg := &testBalancerConfig{
- BalancerName: addr,
- ChildPolicy: []lbPolicy{fakeBalancerA},
- FallbackPolicy: []lbPolicy{fakeBalancerA},
- }
- workingBalancerConfig, _ := json.Marshal(cfg)
- if err := lb.HandleBalancerConfig(json.RawMessage(workingBalancerConfig)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(workingBalancerConfig), err)
- }
- td.sendResp(&response{resp: testEDSRespWithoutEndpoints})
- expectedScStateChange := &scStateChange{
- sc: &fakeSubConn{},
- state: connectivity.Ready,
- }
- var i int
- for i = 0; i < 10; i++ {
- if edsLB := getLatestEdsBalancer(); edsLB != nil {
- lb.HandleSubConnStateChange(expectedScStateChange.sc, expectedScStateChange.state)
- select {
- case scsc := <-edsLB.subconnStateChange:
- if !reflect.DeepEqual(scsc, expectedScStateChange) {
- t.Fatalf("got subconn state change %v, want %v", scsc, expectedScStateChange)
- }
- case <-time.After(time.Second):
- t.Fatal("haven't got subconn state change after 1s")
- }
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- if i == 10 {
- t.Fatal("edsBalancer instance has not been created and assigned to lb.xdsLB after 1s")
- }
- // lbAbuilder has a per binary record what's the last balanceA created. We need to clear the record
- // to make sure there's a new one created and get the pointer to it.
- lbABuilder.clearLastBalancer()
- cleanup()
- // switch to fallback
- // fallback balancer A takes over
- for i = 0; i < 10; i++ {
- if fblb := lbABuilder.getLastBalancer(); fblb != nil {
- lb.HandleSubConnStateChange(expectedScStateChange.sc, expectedScStateChange.state)
- select {
- case scsc := <-fblb.subconnStateChange:
- if !reflect.DeepEqual(scsc, expectedScStateChange) {
- t.Fatalf("got subconn state change %v, want %v", scsc, expectedScStateChange)
- }
- case <-time.After(time.Second):
- t.Fatal("haven't got subconn state change after 1s")
- }
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- if i == 10 {
- t.Fatal("balancerA instance has not been created after 1s")
- }
- }
- func (s) TestXdsBalancerFallbackSignalFromEdsBalancer(t *testing.T) {
- originalNewEDSBalancer := newEDSBalancer
- newEDSBalancer = newFakeEDSBalancer
- defer func() {
- newEDSBalancer = originalNewEDSBalancer
- }()
- builder := balancer.Get("xds")
- cc := newTestClientConn()
- lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
- if !ok {
- t.Fatalf("unable to type assert to *xdsBalancer")
- }
- defer lb.Close()
- addr, td, cleanup := setupServer(t)
- defer cleanup()
- cfg := &testBalancerConfig{
- BalancerName: addr,
- ChildPolicy: []lbPolicy{fakeBalancerA},
- FallbackPolicy: []lbPolicy{fakeBalancerA},
- }
- workingBalancerConfig, _ := json.Marshal(cfg)
- if err := lb.HandleBalancerConfig(json.RawMessage(workingBalancerConfig)); err != nil {
- t.Fatalf("failed to HandleBalancerConfig(%v), due to err: %v", string(workingBalancerConfig), err)
- }
- td.sendResp(&response{resp: testEDSRespWithoutEndpoints})
- expectedScStateChange := &scStateChange{
- sc: &fakeSubConn{},
- state: connectivity.Ready,
- }
- var i int
- for i = 0; i < 10; i++ {
- if edsLB := getLatestEdsBalancer(); edsLB != nil {
- lb.HandleSubConnStateChange(expectedScStateChange.sc, expectedScStateChange.state)
- select {
- case scsc := <-edsLB.subconnStateChange:
- if !reflect.DeepEqual(scsc, expectedScStateChange) {
- t.Fatalf("got subconn state change %v, want %v", scsc, expectedScStateChange)
- }
- case <-time.After(time.Second):
- t.Fatal("haven't got subconn state change after 1s")
- }
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- if i == 10 {
- t.Fatal("edsBalancer instance has not been created and assigned to lb.xdsLB after 1s")
- }
- // lbAbuilder has a per binary record what's the last balanceA created. We need to clear the record
- // to make sure there's a new one created and get the pointer to it.
- lbABuilder.clearLastBalancer()
- cleanup()
- // switch to fallback
- // fallback balancer A takes over
- for i = 0; i < 10; i++ {
- if fblb := lbABuilder.getLastBalancer(); fblb != nil {
- lb.HandleSubConnStateChange(expectedScStateChange.sc, expectedScStateChange.state)
- select {
- case scsc := <-fblb.subconnStateChange:
- if !reflect.DeepEqual(scsc, expectedScStateChange) {
- t.Fatalf("got subconn state change %v, want %v", scsc, expectedScStateChange)
- }
- case <-time.After(time.Second):
- t.Fatal("haven't got subconn state change after 1s")
- }
- break
- }
- time.Sleep(100 * time.Millisecond)
- }
- if i == 10 {
- t.Fatal("balancerA instance has not been created after 1s")
- }
- }
- func (s) TestXdsBalancerConfigParsingSelectingLBPolicy(t *testing.T) {
- tesCfg := &testBalancerConfig{
- BalancerName: "fake.foo.bar",
- ChildPolicy: []lbPolicy{fakeBalancerC, fakeBalancerA, fakeBalancerB}, // selects fakeBalancerA
- FallbackPolicy: []lbPolicy{fakeBalancerC, fakeBalancerB, fakeBalancerA}, // selects fakeBalancerB
- }
- js, _ := json.Marshal(tesCfg)
- var xdsCfg xdsConfig
- if err := json.Unmarshal(js, &xdsCfg); err != nil {
- t.Fatal("unable to unmarshal balancer config into xds config")
- }
- wantChildPolicy := &loadBalancingConfig{Name: string(fakeBalancerA), Config: json.RawMessage(`{}`)}
- if !reflect.DeepEqual(xdsCfg.ChildPolicy, wantChildPolicy) {
- t.Fatalf("got child policy %v, want %v", xdsCfg.ChildPolicy, wantChildPolicy)
- }
- wantFallbackPolicy := &loadBalancingConfig{Name: string(fakeBalancerB), Config: json.RawMessage(`{}`)}
- if !reflect.DeepEqual(xdsCfg.FallBackPolicy, wantFallbackPolicy) {
- t.Fatalf("got fallback policy %v, want %v", xdsCfg.FallBackPolicy, wantFallbackPolicy)
- }
- }
|