-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
87 lines (79 loc) · 2.16 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package gorpc
import (
"context"
"net"
"os"
"runtime"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
)
func TestClient_DialTimeout(t *testing.T) {
t.Parallel()
l, _ := net.Listen("tcp", "127.0.0.1:0")
f := func(conn net.Conn, option *Option) (client *Client, err error) {
conn.Close()
time.Sleep(time.Second * 2)
return nil, nil
}
t.Run("timeout", func(t *testing.T) {
_, err := dialTimeout(f, "tcp", l.Addr().String(), &Option{ConnectTimeout: time.Second})
_assert(err != nil && strings.Contains(err.Error(), "connection time out"), "expect a timeout error")
})
t.Run("0", func(t *testing.T) {
_, err := dialTimeout(f, "tcp", l.Addr().String(), &Option{ConnectTimeout: 0})
_assert(err == nil, "no limit")
})
}
type Bar int
func (b Bar) Timeout(argv int, reply *int) error {
time.Sleep(time.Second * 2)
return nil
}
func StartServer(addr chan string) {
var bar Bar
Register(&bar)
l, _ := net.Listen("tcp", "127.0.0.1:0")
addr <- l.Addr().String()
Accept(l)
}
func TestClient_Call(t *testing.T) {
t.Parallel()
addr := make(chan string)
go StartServer(addr)
ad := <-addr
time.Sleep(time.Second)
t.Run("client timeout", func(t *testing.T) {
client, _ := Dial("tcp", ad)
cntxt, _ := context.WithTimeout(context.Background(), time.Second)
var reply int
err := client.Call(cntxt, "Bar.Timeout", 1, &reply)
_assert(err != nil && strings.Contains(err.Error(), cntxt.Err().Error()), "expect a timeout error")
})
t.Run("server handle timeout", func(t *testing.T) {
client, _ := Dial("tcp", ad, &Option{HandleTimeOout: time.Second})
var reply int
err := client.Call(context.Background(), "Bar.Timeout", 1, &reply)
_assert(err != nil && strings.Contains(err.Error(), "timeout"), "expect a timeout error")
})
}
func TestXDial(t *testing.T) {
if runtime.GOOS == "linux" {
ch := make(chan struct{})
addr := "/tmp/goprc.sock"
go func() {
os.Remove(addr)
l, err := net.Listen("unix", addr)
if err != nil {
panic("fail to listen unix socket" + err.Error())
}
ch <- struct{}{}
logrus.Info("xxxxx")
Accept(l)
}()
<-ch
_, err := XDial("unix@" + addr)
_assert(err == nil, "fail to connect unix socket")
}
}