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()
|
t.Close()
|
||||||
}
|
}
|
||||||
sleepTime := ac.dopts.bs.backoff(retries)
|
sleepTime := ac.dopts.bs.backoff(retries)
|
||||||
ac.dopts.copts.Timeout = sleepTime
|
copts := ac.dopts.copts
|
||||||
|
copts.Timeout = sleepTime
|
||||||
if sleepTime < minConnectTimeout {
|
if sleepTime < minConnectTimeout {
|
||||||
ac.dopts.copts.Timeout = minConnectTimeout
|
copts.Timeout = minConnectTimeout
|
||||||
}
|
}
|
||||||
connectTime := time.Now()
|
connectTime := time.Now()
|
||||||
newTransport, err := transport.NewClientTransport(ac.addr.Addr, &ac.dopts.copts)
|
newTransport, err := transport.NewClientTransport(ac.addr.Addr, copts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ac.mu.Lock()
|
ac.mu.Lock()
|
||||||
if ac.state == Shutdown {
|
if ac.state == Shutdown {
|
||||||
|
|
|
@ -300,39 +300,29 @@ func (s *testServer) HalfDuplexCall(stream testpb.TestService_HalfDuplexCallServ
|
||||||
|
|
||||||
const tlsDir = "testdata/"
|
const tlsDir = "testdata/"
|
||||||
|
|
||||||
func unixDialer(addr string, timeout time.Duration) (net.Conn, error) {
|
|
||||||
return net.DialTimeout("unix", addr, timeout)
|
|
||||||
}
|
|
||||||
|
|
||||||
type env struct {
|
type env struct {
|
||||||
name string
|
name string
|
||||||
network string // The type of network such as tcp, unix, etc.
|
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.
|
security string // The security protocol such as TLS, SSH, etc.
|
||||||
httpHandler bool // whether to use the http.Handler ServerTransport; requires TLS
|
httpHandler bool // whether to use the http.Handler ServerTransport; requires TLS
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e env) runnable() bool {
|
func (e env) runnable() bool {
|
||||||
if runtime.GOOS == "windows" && strings.HasPrefix(e.name, "unix-") {
|
if runtime.GOOS == "windows" && e.network == "unix" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e env) getDialer() func(addr string, timeout time.Duration) (net.Conn, error) {
|
func (e env) dialer(addr string, timeout time.Duration) (net.Conn, error) {
|
||||||
if e.dialer != nil {
|
return net.DialTimeout(e.network, addr, timeout)
|
||||||
return e.dialer
|
|
||||||
}
|
|
||||||
return func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
||||||
return net.DialTimeout("tcp", addr, timeout)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tcpClearEnv = env{name: "tcp-clear", network: "tcp"}
|
tcpClearEnv = env{name: "tcp-clear", network: "tcp"}
|
||||||
tcpTLSEnv = env{name: "tcp-tls", network: "tcp", security: "tls"}
|
tcpTLSEnv = env{name: "tcp-tls", network: "tcp", security: "tls"}
|
||||||
unixClearEnv = env{name: "unix-clear", network: "unix", dialer: unixDialer}
|
unixClearEnv = env{name: "unix-clear", network: "unix"}
|
||||||
unixTLSEnv = env{name: "unix-tls", network: "unix", dialer: unixDialer, security: "tls"}
|
unixTLSEnv = env{name: "unix-tls", network: "unix", security: "tls"}
|
||||||
handlerEnv = env{name: "handler-tls", network: "tcp", security: "tls", httpHandler: true}
|
handlerEnv = env{name: "handler-tls", network: "tcp", security: "tls", httpHandler: true}
|
||||||
allEnv = []env{tcpClearEnv, tcpTLSEnv, unixClearEnv, unixTLSEnv, handlerEnv}
|
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)) {
|
func (te *test) withServerTester(fn func(st *serverTester)) {
|
||||||
var c net.Conn
|
c, err := te.e.dialer(te.srvAddr, 10*time.Second)
|
||||||
var err error
|
|
||||||
c, err = te.e.getDialer()(te.srvAddr, 10*time.Second)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
te.t.Fatal(err)
|
te.t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,20 +107,21 @@ type http2Client struct {
|
||||||
prevGoAwayID uint32
|
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
|
// newHTTP2Client constructs a connected ClientTransport to addr based on HTTP2
|
||||||
// and starts to receive messages on it. Non-nil error returns if construction
|
// and starts to receive messages on it. Non-nil error returns if construction
|
||||||
// fails.
|
// fails.
|
||||||
func newHTTP2Client(addr string, opts *ConnectOptions) (_ ClientTransport, err error) {
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
scheme := "http"
|
scheme := "http"
|
||||||
startT := time.Now()
|
startT := time.Now()
|
||||||
timeout := opts.Timeout
|
timeout := opts.Timeout
|
||||||
conn, connErr := opts.Dialer(addr, timeout)
|
conn, connErr := dial(opts.Dialer, addr, timeout)
|
||||||
if connErr != nil {
|
if connErr != nil {
|
||||||
return nil, ConnectionErrorf("transport: %v", connErr)
|
return nil, ConnectionErrorf("transport: %v", connErr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -366,7 +366,7 @@ type ConnectOptions struct {
|
||||||
|
|
||||||
// NewClientTransport establishes the transport with the required ConnectOptions
|
// NewClientTransport establishes the transport with the required ConnectOptions
|
||||||
// and returns it to the caller.
|
// 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)
|
return newHTTP2Client(target, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -221,7 +221,7 @@ func setUp(t *testing.T, port int, maxStreams uint32, ht hType) (*server, Client
|
||||||
ct ClientTransport
|
ct ClientTransport
|
||||||
connErr error
|
connErr error
|
||||||
)
|
)
|
||||||
ct, connErr = NewClientTransport(addr, &ConnectOptions{})
|
ct, connErr = NewClientTransport(addr, ConnectOptions{})
|
||||||
if connErr != nil {
|
if connErr != nil {
|
||||||
t.Fatalf("failed to create transport: %v", connErr)
|
t.Fatalf("failed to create transport: %v", connErr)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue