forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
203 lines (165 loc) ยท 4.18 KB
/
server_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package server
import (
"bytes"
"context"
"encoding/json"
"io"
"net"
"testing"
"time"
testutils "github.com/smallnest/rpcx/_testutils"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/share"
"github.com/stretchr/testify/assert"
)
type Args struct {
A int
B int
}
type Reply struct {
C int
}
type Arith int
func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
reply.C = args.A * args.B
return nil
}
func (t *Arith) ThriftMul(ctx context.Context, args *testutils.ThriftArgs_, reply *testutils.ThriftReply) error {
reply.C = args.A * args.B
return nil
}
func (t *Arith) ConsumingOperation(ctx context.Context, args *testutils.ThriftArgs_, reply *testutils.ThriftReply) error {
reply.C = args.A * args.B
time.Sleep(10 * time.Second)
return nil
}
func TestShutdownHook(t *testing.T) {
s := NewServer()
var cancel1 context.CancelFunc
s.RegisterOnShutdown(func(s *Server) {
var ctx context.Context
ctx, cancel1 = context.WithTimeout(context.Background(), 155*time.Second)
s.Shutdown(ctx)
})
s.RegisterName("Arith2", new(Arith), "")
s.Register(new(Arith), "")
go s.Serve("tcp", ":0")
time.Sleep(time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
s.Shutdown(ctx)
cancel()
if cancel1 != nil {
cancel1()
}
}
func TestHandleRequest(t *testing.T) {
// use jsoncodec
req := protocol.NewMessage()
req.SetVersion(0)
req.SetMessageType(protocol.Request)
req.SetHeartbeat(false)
req.SetOneway(false)
req.SetCompressType(protocol.None)
req.SetMessageStatusType(protocol.Normal)
req.SetSerializeType(protocol.JSON)
req.SetSeq(1234567890)
req.ServicePath = "Arith"
req.ServiceMethod = "Mul"
argv := &Args{
A: 10,
B: 20,
}
data, err := json.Marshal(argv)
if err != nil {
t.Fatal(err)
}
req.Payload = data
server := NewServer()
server.RegisterName("Arith", new(Arith), "")
res, err := server.handleRequest(context.Background(), req)
if err != nil {
t.Fatalf("failed to hand request: %v", err)
}
if res.Payload == nil {
t.Fatalf("expect reply but got %s", res.Payload)
}
reply := &Reply{}
codec := share.Codecs[res.SerializeType()]
if codec == nil {
t.Fatalf("can not find codec %c", codec)
}
err = codec.Decode(res.Payload, reply)
if err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if reply.C != 200 {
t.Fatalf("expect 200 but got %d", reply.C)
}
}
func TestHandler(t *testing.T) {
// use jsoncodec
req := protocol.NewMessage()
req.SetVersion(0)
req.SetMessageType(protocol.Request)
req.SetHeartbeat(false)
req.SetOneway(false)
req.SetCompressType(protocol.None)
req.SetMessageStatusType(protocol.Normal)
req.SetSerializeType(protocol.JSON)
req.SetSeq(1234567890)
req.ServicePath = "Arith"
req.ServiceMethod = "Mul"
argv := &Args{
A: 10,
B: 20,
}
data, err := json.Marshal(argv)
if err != nil {
t.Fatal(err)
}
req.Payload = data
serverConn, clientConn := net.Pipe()
handler := func(ctx *Context) error {
req := &Args{}
res := &Reply{}
ctx.Bind(req)
res.C = req.A * req.B
return ctx.Write(res)
}
go func() {
ctx := NewContext(share.NewContext(context.Background()), serverConn, req, false)
err = handler(ctx)
assert.NoError(t, err)
serverConn.Close()
}()
data, err = io.ReadAll(clientConn)
assert.NoError(t, err)
resp, err := protocol.Read(bytes.NewReader(data))
assert.NoError(t, err)
assert.Equal(t, "Arith", resp.ServicePath)
assert.Equal(t, "Mul", resp.ServiceMethod)
assert.Equal(t, req.Seq(), resp.Seq())
assert.Equal(t, "{\"C\":200}", string(resp.Payload))
}
func Test_validIP6(t *testing.T) {
type args struct {
ipAddress string
}
tests := []struct {
name string
args args
want bool
}{
{"1", args{ipAddress: "[CDCD:910A:2222:5498:8475:1111:3900:2020]:8080"}, true},
{"2", args{ipAddress: "[1030::C9B4:FF12:48AA:1A2B]:8080"}, true},
{"3", args{ipAddress: "[2000:0:0:0:0:0:0:1]:8080"}, true},
{"4", args{ipAddress: "127.0.0.1:8080"}, false},
{"5", args{ipAddress: "localhost:8080"}, false},
{"6", args{ipAddress: "127.1:8080"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, validIP6(tt.args.ipAddress), "validIP6(%v)", tt.args.ipAddress)
})
}
}