Skip to content

Commit

Permalink
cleanup config
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Mar 25, 2019
1 parent 5b87927 commit 38706b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion balancer/grpclb/grpclb.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (lb *lbBalancer) handleServiceConfig(sc string) {
defer lb.mu.Unlock()

// TODO: FIXME: actually parse the service config.
newUsePickFirst := sc == `{"loadBalancingPolicy": "grpclb_with_pickfirst"}`
newUsePickFirst := strings.Contains(sc, `{"grpclb":{"childPolicy":[{"pickfirst":{}}]}}`)
if lb.usePickFirst == newUsePickFirst {
return
}
Expand Down
50 changes: 24 additions & 26 deletions balancer/grpclb/grpclb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ func TestFallback(t *testing.T) {
}

func TestGRPCLBPickFirst(t *testing.T) {
const grpclbServiceConfigWithPickFirst = `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"pickfirst":{}}]}}]}`

defer leakcheck.Check(t)

r, cleanup := manual.GenerateAndRegisterManualResolver()
Expand Down Expand Up @@ -794,38 +796,21 @@ func TestGRPCLBPickFirst(t *testing.T) {
defer cc.Close()
testC := testpb.NewTestServiceClient(cc)

r.UpdateState(resolver.State{
Addresses: []resolver.Address{{
Addr: tss.lbAddr,
Type: resolver.GRPCLB,
ServerName: lbServerName,
}},
})

// TODO: FIXME: test roundrobin

var p peer.Peer

var (
p peer.Peer
result string
)
tss.ls.sls <- &lbpb.ServerList{Servers: beServers[0:3]}
seq := "012012012"
var result string
for i := 0; i < 1000; i++ {
if _, err := testC.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true), grpc.Peer(&p)); err != nil {
t.Fatalf("%v.EmptyCall(_, _) = _, %v, want _, <nil>", testC, err)
}
result += strconv.Itoa(portsToIndex[p.Addr.(*net.TCPAddr).Port])
}
if !strings.Contains(result, strings.Repeat(seq, 2)) {
t.Errorf("got result sequence %q, want patten %q", result, seq)
}

// Start with sub policy pick_first.

r.UpdateState(resolver.State{
Addresses: []resolver.Address{{
Addr: tss.lbAddr,
Type: resolver.GRPCLB,
ServerName: lbServerName,
}},
ServiceConfig: `{"loadBalancingPolicy": "grpclb_with_pickfirst"}`,
ServiceConfig: grpclbServiceConfigWithPickFirst,
})

result = ""
Expand Down Expand Up @@ -863,16 +848,17 @@ func TestGRPCLBPickFirst(t *testing.T) {
t.Errorf("got result sequence %q, want patten %q", result, seq)
}

// Switch sub policy to roundrobin.

r.UpdateState(resolver.State{
Addresses: []resolver.Address{{
Addr: tss.lbAddr,
Type: resolver.GRPCLB,
ServerName: lbServerName,
}},
ServiceConfig: `{"loadBalancingPolicy": "grpclb_with_rr"}`,
ServiceConfig: `{}`,
})

// TODO: FIXME: test roundrobin again
result = ""
for i := 0; i < 1000; i++ {
if _, err := testC.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true), grpc.Peer(&p)); err != nil {
Expand All @@ -883,6 +869,18 @@ func TestGRPCLBPickFirst(t *testing.T) {
if seq := "121212"; !strings.Contains(result, strings.Repeat(seq, 100)) {
t.Errorf("got result sequence %q, want patten %q", result, seq)
}

tss.ls.sls <- &lbpb.ServerList{Servers: beServers[0:3]}
result = ""
for i := 0; i < 1000; i++ {
if _, err := testC.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true), grpc.Peer(&p)); err != nil {
t.Fatalf("%v.EmptyCall(_, _) = _, %v, want _, <nil>", testC, err)
}
result += strconv.Itoa(portsToIndex[p.Addr.(*net.TCPAddr).Port])
}
if seq := "012012012"; !strings.Contains(result, strings.Repeat(seq, 2)) {
t.Errorf("got result sequence %q, want patten %q", result, seq)
}
}

type failPreRPCCred struct{}
Expand Down

0 comments on commit 38706b7

Please sign in to comment.