Skip to content

Commit

Permalink
test,transport: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Jul 27, 2016
1 parent 6732fdf commit 2342e38
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 31 deletions.
7 changes: 4 additions & 3 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,13 @@ func (ac *addrConn) resetTransport(closeTransport bool) error {
t.Close()
}
sleepTime := ac.dopts.bs.backoff(retries)
ac.dopts.copts.Timeout = sleepTime
copts := ac.dopts.copts
copts.Timeout = sleepTime
if sleepTime < minConnectTimeout {
ac.dopts.copts.Timeout = minConnectTimeout
copts.Timeout = minConnectTimeout
}
connectTime := time.Now()
newTransport, err := transport.NewClientTransport(ac.addr.Addr, &ac.dopts.copts)
newTransport, err := transport.NewClientTransport(ac.addr.Addr, copts)
if err != nil {
ac.mu.Lock()
if ac.state == Shutdown {
Expand Down
24 changes: 6 additions & 18 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,39 +300,29 @@ func (s *testServer) HalfDuplexCall(stream testpb.TestService_HalfDuplexCallServ

const tlsDir = "testdata/"

func unixDialer(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}

type env struct {
name string
network string // The type of network such as tcp, unix, etc.
dialer func(addr string, timeout time.Duration) (net.Conn, error)
security string // The security protocol such as TLS, SSH, etc.
httpHandler bool // whether to use the http.Handler ServerTransport; requires TLS
}

func (e env) runnable() bool {
if runtime.GOOS == "windows" && strings.HasPrefix(e.name, "unix-") {
if runtime.GOOS == "windows" && e.network == "unix" {
return false
}
return true
}

func (e env) getDialer() func(addr string, timeout time.Duration) (net.Conn, error) {
if e.dialer != nil {
return e.dialer
}
return func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
}
func (e env) dialer(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout(e.network, addr, timeout)
}

var (
tcpClearEnv = env{name: "tcp-clear", network: "tcp"}
tcpTLSEnv = env{name: "tcp-tls", network: "tcp", security: "tls"}
unixClearEnv = env{name: "unix-clear", network: "unix", dialer: unixDialer}
unixTLSEnv = env{name: "unix-tls", network: "unix", dialer: unixDialer, security: "tls"}
unixClearEnv = env{name: "unix-clear", network: "unix"}
unixTLSEnv = env{name: "unix-tls", network: "unix", security: "tls"}
handlerEnv = env{name: "handler-tls", network: "tcp", security: "tls", httpHandler: true}
allEnv = []env{tcpClearEnv, tcpTLSEnv, unixClearEnv, unixTLSEnv, handlerEnv}
)
Expand Down Expand Up @@ -515,9 +505,7 @@ func (te *test) declareLogNoise(phrases ...string) {
}

func (te *test) withServerTester(fn func(st *serverTester)) {
var c net.Conn
var err error
c, err = te.e.getDialer()(te.srvAddr, 10*time.Second)
c, err := te.e.dialer(te.srvAddr, 10*time.Second)
if err != nil {
te.t.Fatal(err)
}
Expand Down
17 changes: 9 additions & 8 deletions transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,21 @@ type http2Client struct {
prevGoAwayID uint32
}

func dial(fn func(string, time.Duration) (net.Conn, error), addr string, timeout time.Duration) (net.Conn, error) {
if fn != nil {
return fn(addr, timeout)
}
return net.DialTimeout("tcp", addr, timeout)
}

// newHTTP2Client constructs a connected ClientTransport to addr based on HTTP2
// and starts to receive messages on it. Non-nil error returns if construction
// fails.
func newHTTP2Client(addr string, opts *ConnectOptions) (_ ClientTransport, err error) {
if opts.Dialer == nil {
// Set the default Dialer.
opts.Dialer = func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
}
}
func newHTTP2Client(addr string, opts ConnectOptions) (_ ClientTransport, err error) {
scheme := "http"
startT := time.Now()
timeout := opts.Timeout
conn, connErr := opts.Dialer(addr, timeout)
conn, connErr := dial(opts.Dialer, addr, timeout)
if connErr != nil {
return nil, ConnectionErrorf("transport: %v", connErr)
}
Expand Down
2 changes: 1 addition & 1 deletion transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ type ConnectOptions struct {

// NewClientTransport establishes the transport with the required ConnectOptions
// and returns it to the caller.
func NewClientTransport(target string, opts *ConnectOptions) (ClientTransport, error) {
func NewClientTransport(target string, opts ConnectOptions) (ClientTransport, error) {
return newHTTP2Client(target, opts)
}

Expand Down
2 changes: 1 addition & 1 deletion transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func setUp(t *testing.T, port int, maxStreams uint32, ht hType) (*server, Client
ct ClientTransport
connErr error
)
ct, connErr = NewClientTransport(addr, &ConnectOptions{})
ct, connErr = NewClientTransport(addr, ConnectOptions{})
if connErr != nil {
t.Fatalf("failed to create transport: %v", connErr)
}
Expand Down

0 comments on commit 2342e38

Please sign in to comment.