-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathintegration_test.go
375 lines (337 loc) · 9.53 KB
/
integration_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright 2024 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//
// Package integration_test implements a client to exercise the pw_grpc server implementation
package integration_test
import (
"bufio"
"context"
"fmt"
"hash/crc32"
"io"
"os/exec"
"strconv"
"strings"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
pb "google.golang.org/grpc/examples/features/proto/echo"
"google.golang.org/grpc/status"
)
const port = "3402"
func TestUnaryEcho(t *testing.T) {
const num_connections = 1
cmd, reader, err := launchServer(t, num_connections)
if err != nil {
t.Errorf("Failed to launch %v", err)
}
defer cmd.Wait()
conn, echo_client, err := connectServer()
if err != nil {
t.Errorf("Failed to connect %v", err)
}
defer conn.Close()
go logServer(t, reader)
testRPC(t, func(t *testing.T, ctx context.Context, msg string) {
t.Logf("call UnaryEcho(%v)", msg)
resp, err := echo_client.UnaryEcho(ctx, &pb.EchoRequest{Message: msg})
if err != nil {
t.Logf("... failed with error: %v", err.Error())
if msg != "quiet" || status.Convert(err).Code() != codes.Canceled {
t.Errorf("Error unexpected %v", err)
}
} else {
t.Logf("... Recv %v", resp)
if resp.Message != msg {
t.Errorf("Unexpected response %v", resp)
}
}
})
}
func TestFragmentedMessage(t *testing.T) {
// Test sending successively larger messages, larger than the maximum
// HTTP2 data frame size (16384), ensuring messages are fragmented across
// frames.
const num_connections = 1
cmd, reader, err := launchServer(t, num_connections)
if err != nil {
t.Errorf("Failed to launch %v", err)
}
defer cmd.Wait()
conn, echo_client, err := connectServer()
if err != nil {
t.Errorf("Failed to connect %v", err)
}
defer conn.Close()
go logServer(t, reader)
const num_calls = 4
for i := 0; i < num_calls; i++ {
t.Run(fmt.Sprintf("%d of %d", i+1, num_calls), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
msg := "crc32:" + strings.Repeat("testmessage!", 1500*(i+1))
checksum := strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(msg))), 10)
done := make(chan struct{})
go func() {
t.Logf("call UnaryChecksum")
resp, err := echo_client.UnaryEcho(ctx, &pb.EchoRequest{Message: msg})
if err != nil {
t.Logf("... failed with error: %v", err.Error())
if msg != "quiet" || status.Convert(err).Code() != codes.Canceled {
t.Errorf("Error unexpected %v", err)
}
} else {
t.Logf("... Recv %v", resp)
if resp.Message != checksum {
t.Errorf("Unexpected response %v", resp)
}
}
close(done)
}()
<-done
})
}
}
func TestMultipleConnections(t *testing.T) {
const num_connections = 3
cmd, reader, err := launchServer(t, num_connections)
if err != nil {
t.Errorf("Failed to launch %v", err)
}
defer cmd.Wait()
go logServer(t, reader)
for i := 0; i < num_connections; i++ {
t.Run(fmt.Sprintf("connection %d of %d", i+1, num_connections), func(t *testing.T) {
conn, echo_client, err := connectServer()
if err != nil {
t.Errorf("Failed to connect %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := echo_client.UnaryEcho(ctx, &pb.EchoRequest{Message: "message0"})
if err != nil {
t.Errorf("... failed with error: %v", err.Error())
} else {
t.Logf("... Recv %v", resp)
if resp.Message != "message0" {
t.Errorf("Unexpected response %v", resp)
}
}
conn.Close()
})
}
}
func TestServerStreamingEcho(t *testing.T) {
const num_connections = 1
cmd, reader, err := launchServer(t, num_connections)
if err != nil {
t.Errorf("Failed to launch %v", err)
}
defer cmd.Wait()
conn, echo_client, err := connectServer()
if err != nil {
t.Errorf("Failed to connect %v", err)
}
defer conn.Close()
go logServer(t, reader)
testRPC(t, func(t *testing.T, ctx context.Context, msg string) {
t.Logf("call ServerStreamingEcho(%v)", msg)
client, err := echo_client.ServerStreamingEcho(ctx, &pb.EchoRequest{Message: msg})
if err != nil {
t.Errorf("... failed with error: %v", err)
return
}
for {
resp, err := client.Recv()
if err == io.EOF {
t.Logf("... completed")
return
}
if err != nil {
t.Logf("... Recv failed with error: %v", err)
if msg != "quiet" || status.Convert(err).Code() != codes.Canceled {
t.Errorf("Error unexpected %v", err)
}
return
}
t.Logf("... Recv %v", resp)
if resp.Message != msg && resp.Message != "done" {
t.Errorf("Unexpected response %v", resp)
}
}
})
}
func TestClientStreamingEcho(t *testing.T) {
const num_connections = 1
cmd, reader, err := launchServer(t, num_connections)
if err != nil {
t.Errorf("Failed to launch %v", err)
}
defer cmd.Wait()
conn, echo_client, err := connectServer()
if err != nil {
t.Errorf("Failed to connect %v", err)
}
defer conn.Close()
go logServer(t, reader)
testRPC(t, func(t *testing.T, ctx context.Context, msg string) {
t.Logf("call ClientStreamingEcho()")
client, err := echo_client.ClientStreamingEcho(ctx)
if err != nil {
t.Errorf("... failed with error: %v", err)
return
}
for i := 0; i < 3; i++ {
t.Logf("... Send %v", msg)
if err := client.Send(&pb.EchoRequest{Message: msg}); err != nil {
t.Errorf("... Send failed with error: %v", err)
return
}
}
if err := client.CloseSend(); err != nil {
t.Errorf("... CloseSend failed with error: %v", err)
return
}
resp, err := client.CloseAndRecv()
if err != nil {
t.Logf("... CloseAndRecv failed with error: %v", err)
if msg != "quiet" || status.Convert(err).Code() != codes.Canceled {
t.Errorf("Error unexpected %v", err)
}
} else {
t.Logf("... CloseAndRecv %v", resp)
if resp.Message != "done" {
t.Errorf("Unexpected response %v", resp)
}
}
})
}
func TestBidirectionalStreamingEcho(t *testing.T) {
const num_connections = 1
cmd, reader, err := launchServer(t, num_connections)
if err != nil {
t.Errorf("Failed to launch %v", err)
}
defer cmd.Wait()
conn, echo_client, err := connectServer()
if err != nil {
t.Errorf("Failed to connect %v", err)
}
defer conn.Close()
go logServer(t, reader)
testRPC(t, func(t *testing.T, ctx context.Context, msg string) {
t.Logf("call BidirectionalStreamingEcho()")
client, err := echo_client.BidirectionalStreamingEcho(ctx)
if err != nil {
t.Logf("... failed with error: %v", err)
return
}
for i := 0; i < 3; i++ {
t.Logf("... Send %v", msg)
if err := client.Send(&pb.EchoRequest{Message: msg}); err != nil {
t.Errorf("... Send failed with error: %v", err)
return
}
}
if err := client.CloseSend(); err != nil {
t.Logf("... CloseSend failed with error: %v", err)
return
}
for {
resp, err := client.Recv()
if err == io.EOF {
t.Logf("... completed")
return
}
if err != nil {
t.Logf("... Recv failed with error: %v", err)
if msg != "quiet" || status.Convert(err).Code() != codes.Canceled {
t.Errorf("Error unexpected %v", err)
}
return
}
t.Logf("... Recv %v", resp)
if resp.Message != msg {
t.Errorf("Unexpected response %v", resp)
}
}
})
}
func logServer(t *testing.T, reader *bufio.Reader) {
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
t.Logf("SERVER: %v", line)
}
}
func launchServer(t *testing.T, num_connections int) (*exec.Cmd, *bufio.Reader, error) {
cmd := exec.Command("./test_pw_rpc_server", port, strconv.Itoa(num_connections))
output, err := cmd.StdoutPipe()
if err != nil {
t.Errorf("Failed to get stdout of server %v", err)
return nil, nil, err
}
if err := cmd.Start(); err != nil {
t.Errorf("Failed to launch server %v", err)
return nil, nil, err
}
reader := bufio.NewReader(output)
for {
line, _ := reader.ReadString('\n')
if strings.Contains(line, "Accept") {
break
}
}
return cmd, reader, nil
}
func connectServer() (*grpc.ClientConn, pb.EchoClient, error) {
addr := "localhost:" + port
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, nil, err
}
echo_client := pb.NewEchoClient(conn)
return conn, echo_client, nil
}
func testRPC(t *testing.T, call func(t *testing.T, ctx context.Context, msg string)) {
const num_calls = 30
for i := 0; i < num_calls; i++ {
t.Run(fmt.Sprintf("%d of %d", i+1, num_calls), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
msg := fmt.Sprintf("message%d", i)
if i == num_calls-1 {
msg = "quiet"
}
done := make(chan struct{})
go func() {
call(t, ctx, msg)
close(done)
}()
// Test cancellation. When we sent "quiet", the server won't echo anything
// back and instead will hold onto the request. Sleep a bit to make sure
// the server doesn't respond. Then cancel the request, which should
// complete the RPC.
if msg == "quiet" {
time.Sleep(100 * time.Millisecond)
cancel()
}
<-done
})
}
}