Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a test flakiness in go1.7 #923

Merged
merged 1 commit into from
Oct 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions grpclb/grpclb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"strconv"
"strings"
"testing"
"time"

"golang.org/x/net/context"
"google.golang.org/grpc"
Expand Down Expand Up @@ -121,26 +122,25 @@ func (r *testNameResolver) Resolve(target string) (naming.Watcher, error) {
}

type serverNameCheckCreds struct {
t *testing.T
expected string
sn string
}

func (c *serverNameCheckCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
if _, err := io.WriteString(rawConn, c.sn); err != nil {
c.t.Errorf("Failed to write the server name %s to the client %v", c.sn, err)
fmt.Printf("Failed to write the server name %s to the client %v", c.sn, err)
return nil, nil, err
}
return rawConn, nil, nil
}
func (c *serverNameCheckCreds) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
b := make([]byte, len(c.expected))
if _, err := rawConn.Read(b); err != nil {
c.t.Errorf("Failed to read the server name from the server %v", err)
fmt.Printf("Failed to read the server name from the server %v", err)
return nil, nil, err
}
if c.expected != string(b) {
c.t.Errorf("Read the server name %s want %s", string(b), c.expected)
fmt.Printf("Read the server name %s want %s", string(b), c.expected)
return nil, nil, errors.New("received unexpected server name")
}
return rawConn, nil, nil
Expand All @@ -150,7 +150,6 @@ func (c *serverNameCheckCreds) Info() credentials.ProtocolInfo {
}
func (c *serverNameCheckCreds) Clone() credentials.TransportCredentials {
return &serverNameCheckCreds{
t: c.t,
expected: c.expected,
}
}
Expand Down Expand Up @@ -199,7 +198,6 @@ func (b *remoteBalancer) BalanceLoad(stream lbpb.LoadBalancer_BalanceLoadServer)
func startBackends(t *testing.T, sn string, lis ...net.Listener) (servers []*grpc.Server) {
for _, l := range lis {
creds := &serverNameCheckCreds{
t: t,
sn: sn,
}
s := grpc.NewServer(grpc.Creds(creds))
Expand Down Expand Up @@ -234,7 +232,6 @@ func TestGRPCLB(t *testing.T) {
t.Fatalf("Failed to create the listener for the load balancer %v", err)
}
lbCreds := &serverNameCheckCreds{
t: t,
sn: lbsn,
}
lb := grpc.NewServer(grpc.Creds(lbCreds))
Expand All @@ -260,10 +257,10 @@ func TestGRPCLB(t *testing.T) {
lb.Stop()
}()
creds := serverNameCheckCreds{
t: t,
expected: besn,
}
cc, err := grpc.Dial(besn, grpc.WithBalancer(Balancer(&testNameResolver{
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
cc, err := grpc.DialContext(ctx, besn, grpc.WithBalancer(Balancer(&testNameResolver{
addr: lbLis.Addr().String(),
})), grpc.WithBlock(), grpc.WithTransportCredentials(&creds))
if err != nil {
Expand Down