feat: implement server and client

This commit is contained in:
2022-04-23 01:24:17 +08:00
parent cf8fa335a6
commit 74ef998a9b
13 changed files with 631 additions and 120 deletions

40
client_test.go Normal file
View File

@ -0,0 +1,40 @@
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")
}
})
}
}