forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxclient_test.go
136 lines (108 loc) ยท 2.92 KB
/
xclient_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package client
import (
"context"
"errors"
"testing"
"time"
"fmt"
testutils "github.com/smallnest/rpcx/_testutils"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/server"
"github.com/smallnest/rpcx/share"
)
func TestXClient_Thrift(t *testing.T) {
s := server.NewServer()
s.RegisterName("Arith", new(Arith), "")
go s.Serve("tcp", "127.0.0.1:0")
defer s.Close()
time.Sleep(500 * time.Millisecond)
addr := s.Address().String()
opt := Option{
Retries: 1,
RPCPath: share.DefaultRPCPath,
ConnectTimeout: 10 * time.Second,
SerializeType: protocol.Thrift,
CompressType: protocol.None,
BackupLatency: 10 * time.Millisecond,
}
d, err := NewPeer2PeerDiscovery("tcp@"+addr, "desc=a test service")
if err != nil {
t.Fatalf("failed to NewPeer2PeerDiscovery: %v", err)
}
xclient := NewXClient("Arith", Failtry, RandomSelect, d, opt)
defer xclient.Close()
args := testutils.ThriftArgs_{}
args.A = 200
args.B = 100
reply := testutils.ThriftReply{}
err = xclient.Call(context.Background(), "ThriftMul", &args, &reply)
if err != nil {
t.Fatalf("failed to call: %v", err)
}
fmt.Println(reply.C)
if reply.C != 20000 {
t.Fatalf("expect 20000 but got %d", reply.C)
}
}
func TestXClient_IT(t *testing.T) {
s := server.NewServer()
s.RegisterName("Arith", new(Arith), "")
go s.Serve("tcp", "127.0.0.1:0")
defer s.Close()
time.Sleep(500 * time.Millisecond)
addr := s.Address().String()
d, err := NewPeer2PeerDiscovery("tcp@"+addr, "desc=a test service")
if err != nil {
t.Fatalf("failed to NewPeer2PeerDiscovery: %v", err)
}
xclient := NewXClient("Arith", Failtry, RandomSelect, d, DefaultOption)
defer xclient.Close()
args := &Args{
A: 10,
B: 20,
}
reply := &Reply{}
err = xclient.Call(context.Background(), "Mul", args, reply)
if err != nil {
t.Fatalf("failed to call: %v", err)
}
if reply.C != 200 {
t.Fatalf("expect 200 but got %d", reply.C)
}
}
func TestXClient_filterByStateAndGroup(t *testing.T) {
servers := map[string]string{"a": "", "b": "state=inactive&ops=10", "c": "ops=20", "d": "group=test1&group=test&ops=20"}
filterByStateAndGroup("test", servers)
if _, ok := servers["b"]; ok {
t.Error("has not remove inactive node")
}
if _, ok := servers["a"]; ok {
t.Error("has not remove inactive node")
}
if _, ok := servers["c"]; ok {
t.Error("has not remove inactive node")
}
if _, ok := servers["d"]; !ok {
t.Error("node must be removed")
}
filterByStateAndGroup("test1", servers)
if _, ok := servers["d"]; !ok {
t.Error("node must be removed")
}
}
func TestUncoverError(t *testing.T) {
var e error = strErr("error")
if uncoverError(e) {
t.Fatalf("expect false but get true")
}
if uncoverError(context.DeadlineExceeded) {
t.Fatalf("expect false but get true")
}
if uncoverError(context.Canceled) {
t.Fatalf("expect false but get true")
}
e = errors.New("error")
if !uncoverError(e) {
t.Fatalf("expect true but get false")
}
}