Skip to content

Commit

Permalink
One more test.
Browse files Browse the repository at this point in the history
  • Loading branch information
MakMukhi committed Nov 9, 2017
1 parent f139211 commit 9a60fe7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
1 change: 0 additions & 1 deletion clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func UseCompressor(name string) CallOption {
func WithWaitForServerSettings() DialOption {
return func(o *dialOptions) {
o.waitForServerSettings = true
o.block = true
}
}

Expand Down
86 changes: 82 additions & 4 deletions clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package grpc

import (
"fmt"
"io"
"math"
"net"
Expand All @@ -32,6 +33,8 @@ import (
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/naming"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
_ "google.golang.org/grpc/resolver/passthrough"
"google.golang.org/grpc/test/leakcheck"
"google.golang.org/grpc/testdata"
Expand All @@ -46,6 +49,81 @@ func assertState(wantState connectivity.State, cc *ClientConn) (connectivity.Sta
return state, state == wantState
}

func TestDialWithMultipleBackendsNotSendingServerPreface(t *testing.T) {
defer leakcheck.Check(t)
numServers := 2
servers := make([]net.Listener, numServers)
var err error
for i := 0; i < numServers; i++ {
servers[i], err = net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Error while listening. Err: %v", err)
}
}
dones := make([]chan struct{}, numServers)
for i := 0; i < numServers; i++ {
dones[i] = make(chan struct{})
}
for i := 0; i < numServers; i++ {
go func(i int) {
var (
err error
conn net.Conn
)
defer func() {
close(dones[i])
}()
conn, err = servers[i].Accept()
if err != nil {
t.Errorf("Error while accepting. Err: %v", err)
return
}
defer conn.Close()
switch i {
case 0: // 1st server accepts the connection and immediately closes it.
case 1: // 2nd server accepts the connection and sends settings frames.
framer := http2.NewFramer(conn, conn)
if err = framer.WriteSettings(http2.Setting{}); err != nil {
t.Errorf("Error while writing settings frame. %v", err)
return
}
conn.SetDeadline(time.Now().Add(time.Second))
buf := make([]byte, 1024)
for { // Make sure the connection stays healthy.
_, err = conn.Read(buf)
if err == nil {
continue
}
if nerr, ok := err.(net.Error); !ok || !nerr.Timeout() {
err = fmt.Errorf("Server expected the conn.Read(_) to timeout instead got error: %v", err)
}
return
}
}
}(i)
}
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
resolvedAddrs := make([]resolver.Address, numServers)
for i := 0; i < numServers; i++ {
resolvedAddrs[i] = resolver.Address{Addr: servers[i].Addr().String()}
}
r.BootstrapWithAddrs(resolvedAddrs)
client, err := Dial(r.Scheme()+":///test.server", WithInsecure())
if err != nil {
t.Errorf("Dial failed. Err: %v", err)
} else {
defer client.Close()
}
time.Sleep(time.Second) // Close the servers after a second for cleanup.
for _, s := range servers {
s.Close()
}
for _, done := range dones {
<-done
}
}

func TestDialWaitsForServerSettings(t *testing.T) {
defer leakcheck.Check(t)
server, err := net.Listen("tcp", "localhost:0")
Expand Down Expand Up @@ -78,7 +156,7 @@ func TestDialWaitsForServerSettings(t *testing.T) {
<-dialDone // Close conn only after dial returns.
}()
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
client, err := DialContext(ctx, server.Addr().String(), WithInsecure(), WithWaitForServerSettings())
client, err := DialContext(ctx, server.Addr().String(), WithInsecure(), WithWaitForServerSettings(), WithBlock())
close(dialDone)
if err != nil {
cancel()
Expand Down Expand Up @@ -197,15 +275,15 @@ func TestBackoffWhenNoServerPrefaceReceived(t *testing.T) {
t.Errorf("Error while accepting. Err: %v", err)
return
}
now := time.Now()
meow := time.Now()
conn.Close()
dr := now.Sub(prevAt)
dr := meow.Sub(prevAt)
if dr <= prevDuration {
t.Errorf("Client backoff did not increase with retries. Previoud duration: %v, current duration: %v", prevDuration, dr)
return
}
prevDuration = dr
prevAt = now
prevAt = meow
}
}()
client, err := Dial(server.Addr().String(), WithInsecure())
Expand Down
12 changes: 11 additions & 1 deletion resolver/manual/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ type Resolver struct {
scheme string

// Fields actually belong to the resolver.
cc resolver.ClientConn
cc resolver.ClientConn
bootstrapAddrs []resolver.Address
}

// BootstrapWithAddrs adds resloved addresses to the resolver so that
// NewAddress doesn't need to be explicitly called after Dial.
func (r *Resolver) BootstrapWithAddrs(addrs []resolver.Address) {
r.bootstrapAddrs = addrs
}

// Build returns itself for Resolver, because it's both a builder and a resolver.
func (r *Resolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) {
r.cc = cc
if r.bootstrapAddrs != nil {
r.NewAddress(r.bootstrapAddrs)
}
return r, nil
}

Expand Down

0 comments on commit 9a60fe7

Please sign in to comment.