-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathclient.rs
410 lines (353 loc) · 13.3 KB
/
client.rs
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::{
pb::test_service_client::*, pb::unimplemented_service_client::*, pb::*, test_assert,
TestAssertion,
};
use tokio::sync::mpsc;
use tokio_stream::StreamExt;
use tonic::transport::Channel;
use tonic::{metadata::MetadataValue, Code, Request, Response, Status};
pub type TestClient = TestServiceClient<Channel>;
pub type UnimplementedClient = UnimplementedServiceClient<Channel>;
const LARGE_REQ_SIZE: usize = 271_828;
const LARGE_RSP_SIZE: i32 = 314_159;
const REQUEST_LENGTHS: &[i32] = &[27182, 8, 1828, 45904];
const RESPONSE_LENGTHS: &[i32] = &[31415, 9, 2653, 58979];
const TEST_STATUS_MESSAGE: &str = "test status message";
const SPECIAL_TEST_STATUS_MESSAGE: &str =
"\t\ntest with whitespace\r\nand Unicode BMP ☺ and non-BMP 😈\t\n";
pub async fn empty_unary(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let result = client.empty_call(Request::new(Empty {})).await;
assertions.push(test_assert!(
"call must be successful",
result.is_ok(),
format!("result={:?}", result)
));
if let Ok(response) = result {
let body = response.into_inner();
assertions.push(test_assert!(
"body must not be null",
body == Empty {},
format!("body={:?}", body)
));
}
}
pub async fn large_unary(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
use std::mem;
let payload = crate::client_payload(LARGE_REQ_SIZE);
let req = SimpleRequest {
response_type: PayloadType::Compressable as i32,
response_size: LARGE_RSP_SIZE,
payload: Some(payload),
..Default::default()
};
let result = client.unary_call(Request::new(req)).await;
assertions.push(test_assert!(
"call must be successful",
result.is_ok(),
format!("result={:?}", result)
));
if let Ok(response) = result {
let body = response.into_inner();
let payload_len = body.payload.as_ref().map(|p| p.body.len()).unwrap_or(0);
assertions.push(test_assert!(
"body must be 314159 bytes",
payload_len == LARGE_RSP_SIZE as usize,
format!("mem::size_of_val(&body)={:?}", mem::size_of_val(&body))
));
}
}
// pub async fn cachable_unary(client: &mut Client, assertions: &mut Vec<TestAssertion>) {
// let payload = Payload {
// r#type: PayloadType::Compressable as i32,
// body: format!("{:?}", std::time::Instant::now()).into_bytes(),
// };
// let req = SimpleRequest {
// response_type: PayloadType::Compressable as i32,
// payload: Some(payload),
// ..Default::default()
// };
// client.
// }
pub async fn client_streaming(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let requests = REQUEST_LENGTHS.iter().map(|len| StreamingInputCallRequest {
payload: Some(crate::client_payload(*len as usize)),
..Default::default()
});
let stream = tokio_stream::iter(requests);
let result = client.streaming_input_call(Request::new(stream)).await;
assertions.push(test_assert!(
"call must be successful",
result.is_ok(),
format!("result={:?}", result)
));
if let Ok(response) = result {
let body = response.into_inner();
assertions.push(test_assert!(
"aggregated payload size must be 74922 bytes",
body.aggregated_payload_size == 74922,
format!("aggregated_payload_size={:?}", body.aggregated_payload_size)
));
}
}
pub async fn server_streaming(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let req = StreamingOutputCallRequest {
response_parameters: RESPONSE_LENGTHS
.iter()
.map(|len| ResponseParameters::with_size(*len))
.collect(),
..Default::default()
};
let req = Request::new(req);
let result = client.streaming_output_call(req).await;
assertions.push(test_assert!(
"call must be successful",
result.is_ok(),
format!("result={:?}", result)
));
if let Ok(response) = result {
let responses = response
.into_inner()
.filter_map(|m| m.ok())
.collect::<Vec<_>>()
.await;
let actual_response_lengths = crate::response_lengths(&responses);
let asserts = vec![
test_assert!(
"there should be four responses",
responses.len() == 4,
format!("responses.len()={:?}", responses.len())
),
test_assert!(
"the response payload sizes should match input",
RESPONSE_LENGTHS == actual_response_lengths.as_slice(),
format!("{:?}={:?}", RESPONSE_LENGTHS, actual_response_lengths)
),
];
assertions.extend(asserts);
}
}
pub async fn ping_pong(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let (tx, rx) = mpsc::unbounded_channel();
tx.send(make_ping_pong_request(0)).unwrap();
let result = client
.full_duplex_call(Request::new(
tokio_stream::wrappers::UnboundedReceiverStream::new(rx),
))
.await;
assertions.push(test_assert!(
"call must be successful",
result.is_ok(),
format!("result={:?}", result)
));
if let Ok(mut response) = result.map(Response::into_inner) {
let mut responses = Vec::new();
loop {
match response.next().await {
Some(result) => {
responses.push(result.unwrap());
if responses.len() == REQUEST_LENGTHS.len() {
drop(tx);
break;
} else {
tx.send(make_ping_pong_request(responses.len())).unwrap();
}
}
None => {
assertions.push(TestAssertion::Failed {
description:
"server should keep the stream open until the client closes it",
expression: "Stream terminated unexpectedly early",
why: None,
});
break;
}
}
}
let actual_response_lengths = crate::response_lengths(&responses);
assertions.push(test_assert!(
"there should be four responses",
responses.len() == RESPONSE_LENGTHS.len(),
format!("{:?}={:?}", responses.len(), RESPONSE_LENGTHS.len())
));
assertions.push(test_assert!(
"the response payload sizes should match input",
RESPONSE_LENGTHS == actual_response_lengths.as_slice(),
format!("{:?}={:?}", RESPONSE_LENGTHS, actual_response_lengths)
));
}
}
pub async fn empty_stream(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let stream = tokio_stream::empty();
let result = client.full_duplex_call(Request::new(stream)).await;
assertions.push(test_assert!(
"call must be successful",
result.is_ok(),
format!("result={:?}", result)
));
if let Ok(response) = result.map(Response::into_inner) {
let responses = response.collect::<Vec<_>>().await;
assertions.push(test_assert!(
"there should be no responses",
responses.is_empty(),
format!("responses.len()={:?}", responses.len())
));
}
}
pub async fn status_code_and_message(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
fn validate_response<T>(result: Result<T, Status>, assertions: &mut Vec<TestAssertion>)
where
T: std::fmt::Debug,
{
assertions.push(test_assert!(
"call must fail with unknown status code",
match &result {
Err(status) => status.code() == Code::Unknown,
_ => false,
},
format!("result={:?}", result)
));
assertions.push(test_assert!(
"call must respsond with expected status message",
match &result {
Err(status) => status.message() == TEST_STATUS_MESSAGE,
_ => false,
},
format!("result={:?}", result)
));
}
let simple_req = SimpleRequest {
response_status: Some(EchoStatus {
code: 2,
message: TEST_STATUS_MESSAGE.to_string(),
}),
..Default::default()
};
let duplex_req = StreamingOutputCallRequest {
response_status: Some(EchoStatus {
code: 2,
message: TEST_STATUS_MESSAGE.to_string(),
}),
..Default::default()
};
let result = client.unary_call(Request::new(simple_req)).await;
validate_response(result, assertions);
let stream = tokio_stream::once(duplex_req);
let result = match client.full_duplex_call(Request::new(stream)).await {
Ok(response) => {
let stream = response.into_inner();
let responses = stream.collect::<Vec<_>>().await;
Ok(responses)
}
Err(e) => Err(e),
};
validate_response(result, assertions);
}
pub async fn special_status_message(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let req = SimpleRequest {
response_status: Some(EchoStatus {
code: 2,
message: SPECIAL_TEST_STATUS_MESSAGE.to_string(),
}),
..Default::default()
};
let result = client.unary_call(Request::new(req)).await;
assertions.push(test_assert!(
"call must fail with unknown status code",
match &result {
Err(status) => status.code() == Code::Unknown,
_ => false,
},
format!("result={:?}", result)
));
assertions.push(test_assert!(
"call must respsond with expected status message",
match &result {
Err(status) => status.message() == SPECIAL_TEST_STATUS_MESSAGE,
_ => false,
},
format!("result={:?}", result)
));
}
pub async fn unimplemented_method(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let result = client.unimplemented_call(Request::new(Empty {})).await;
assertions.push(test_assert!(
"call must fail with unimplemented status code",
match &result {
Err(status) => status.code() == Code::Unimplemented,
_ => false,
},
format!("result={:?}", result)
));
}
pub async fn unimplemented_service(
client: &mut UnimplementedClient,
assertions: &mut Vec<TestAssertion>,
) {
let result = client.unimplemented_call(Request::new(Empty {})).await;
assertions.push(test_assert!(
"call must fail with unimplemented status code",
match &result {
Err(status) => status.code() == Code::Unimplemented,
_ => false,
},
format!("result={:?}", result)
));
}
pub async fn custom_metadata(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
let key1 = "x-grpc-test-echo-initial";
let value1: MetadataValue<_> = "test_initial_metadata_value".parse().unwrap();
let key2 = "x-grpc-test-echo-trailing-bin";
let value2 = MetadataValue::from_bytes(&[0xab, 0xab, 0xab]);
let req = SimpleRequest {
response_type: PayloadType::Compressable as i32,
response_size: LARGE_RSP_SIZE,
payload: Some(crate::client_payload(LARGE_REQ_SIZE)),
..Default::default()
};
let mut req_unary = Request::new(req);
req_unary.metadata_mut().insert(key1, value1.clone());
req_unary.metadata_mut().insert_bin(key2, value2.clone());
let stream = tokio_stream::once(make_ping_pong_request(0));
let mut req_stream = Request::new(stream);
req_stream.metadata_mut().insert(key1, value1.clone());
req_stream.metadata_mut().insert_bin(key2, value2.clone());
let response = client
.unary_call(req_unary)
.await
.expect("call should pass.");
assertions.push(test_assert!(
"metadata string must match in unary",
response.metadata().get(key1) == Some(&value1),
format!("result={:?}", response.metadata().get(key1))
));
assertions.push(test_assert!(
"metadata bin must match in unary",
response.metadata().get_bin(key2) == Some(&value2),
format!("result={:?}", response.metadata().get_bin(key1))
));
let response = client
.full_duplex_call(req_stream)
.await
.expect("call should pass.");
assertions.push(test_assert!(
"metadata string must match in unary",
response.metadata().get(key1) == Some(&value1),
format!("result={:?}", response.metadata().get(key1))
));
let mut stream = response.into_inner();
let trailers = stream.trailers().await.unwrap().unwrap();
assertions.push(test_assert!(
"metadata bin must match in unary",
trailers.get_bin(key2) == Some(&value2),
format!("result={:?}", trailers.get_bin(key1))
));
}
fn make_ping_pong_request(idx: usize) -> StreamingOutputCallRequest {
let req_len = REQUEST_LENGTHS[idx];
let resp_len = RESPONSE_LENGTHS[idx];
StreamingOutputCallRequest {
response_parameters: vec![ResponseParameters::with_size(resp_len)],
payload: Some(crate::client_payload(req_len as usize)),
..Default::default()
}
}