41 lines
784 B
Go
41 lines
784 B
Go
package krpc
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestClient_dialTimeout(t *testing.T) {
|
|
t.Parallel()
|
|
l, _ := net.Listen("tcp", ":0")
|
|
|
|
f := func(conn net.Conn, opts ...Option) (client *Client, err error) {
|
|
_ = conn.Close()
|
|
time.Sleep(time.Millisecond * 500)
|
|
return nil, nil
|
|
}
|
|
|
|
tests := map[string]time.Duration{
|
|
"timeout": time.Millisecond * 100,
|
|
"unlimited": 0,
|
|
}
|
|
for name, timeout := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
_, err := dialTimeout(f, "tcp", l.Addr().String(),
|
|
WithConnectTimeout(timeout))
|
|
if strings.Contains(name, "timeout") {
|
|
_assert(err != nil &&
|
|
strings.Contains(err.Error(), "timeout"),
|
|
"expect a timeout error",
|
|
)
|
|
} else {
|
|
_assert(err == nil, "0 means no limit")
|
|
}
|
|
|
|
})
|
|
}
|
|
|
|
}
|