mirror of https://github.com/grpc/grpc-go.git
test,transport: simplify
This commit is contained in:
parent
6732fdf9f8
commit
2342e38669
|
@ -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 {
|
||||
|
|
|
@ -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}
|
||||
)
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue