Implement DialContext to afford caller option of managing cancelation

This commit is contained in:
Spencer Kimball 2016-08-15 13:17:09 -04:00
parent c2c110d5cf
commit b38541aeb0
2 changed files with 21 additions and 2 deletions

View File

@ -214,9 +214,14 @@ func WithUserAgent(s string) DialOption {
}
}
// Dial creates a client connection the given target.
// Dial creates a client connection to the given target.
func Dial(target string, opts ...DialOption) (*ClientConn, error) {
ctx := context.Background()
return DialContext(context.Background(), target, opts...)
}
// DialContext creates a client connection to the given target
// using the supplied context.
func DialContext(ctx context.Context, target string, opts ...DialOption) (*ClientConn, error) {
cc := &ClientConn{
target: target,
conns: make(map[Address]*addrConn),

View File

@ -37,6 +37,8 @@ import (
"testing"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/oauth"
)
@ -67,6 +69,18 @@ func TestTLSDialTimeout(t *testing.T) {
}
}
func TestDialContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
go cancel()
conn, err := DialContext(ctx, "Non-Existent.Server:80", WithBlock(), WithInsecure())
if err == nil {
conn.Close()
}
if err != context.Canceled {
t.Fatalf("DialContext(_, _) = %v, %v, want %v", conn, err, context.Canceled)
}
}
func TestCredentialsMisuse(t *testing.T) {
tlsCreds, err := credentials.NewClientTLSFromFile(tlsDir+"ca.pem", "x.test.youtube.com")
if err != nil {