Skip to content

Commit 12905c5

Browse files
authored
refactor(app): MockBody convenience functions for test code (#3349)
* refactor(app): add `pending()` helper for mock body in timeout tests, we construct mock bodies that are backed by a `Pending<T>` future that will never yield any data. this introduces a small `pending()` helper to the `MockBody` type used in test code in the outbound proxy. Signed-off-by: katelyn martin <[email protected]> * refactor(app): add `error()` helper for mock body in metrics tests, we construct mock bodies that will yield an error when polled, to examine how telemetry measures certain edge cases. this introduces a small `error()` helper to the `MockBody` type used in test code in the outbound proxy. Signed-off-by: katelyn martin <[email protected]> * refactor(app): add `grpc_status()` helper for mock body in metrics tests, we construct mock bodies that will yield an gRPC status code in its trailers section when polled, to examine how telemetry measures gRPC responses. this refactors the `trailers()` function to more narrowly focus on the test coverage needed, to help reduce boilerplate in test code. Signed-off-by: katelyn martin <[email protected]> --------- Signed-off-by: katelyn martin <[email protected]>
1 parent 4dc979f commit 12905c5

File tree

5 files changed

+38
-56
lines changed

5 files changed

+38
-56
lines changed

linkerd/app/outbound/src/http/logical/policy/route/backend/metrics/tests.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ async fn http_request_statuses() {
101101
tx.send_response(
102102
http::Response::builder()
103103
.status(200)
104-
.body(BoxBody::new(MockBody::new(async {
105-
Err("a spooky ghost".into())
106-
})))
104+
.body(BoxBody::new(MockBody::error("a spooky ghost")))
107105
.unwrap(),
108106
)
109107
})
@@ -150,11 +148,7 @@ async fn grpc_request_statuses_ok() {
150148
|tx| {
151149
tx.send_response(
152150
http::Response::builder()
153-
.body(BoxBody::new(MockBody::trailers(async move {
154-
let mut trailers = http::HeaderMap::new();
155-
trailers.insert("grpc-status", http::HeaderValue::from_static("0"));
156-
Ok(Some(trailers))
157-
})))
151+
.body(BoxBody::new(MockBody::grpc_status(0)))
158152
.unwrap(),
159153
)
160154
},
@@ -193,11 +187,7 @@ async fn grpc_request_statuses_not_found() {
193187
|tx| {
194188
tx.send_response(
195189
http::Response::builder()
196-
.body(BoxBody::new(MockBody::trailers(async move {
197-
let mut trailers = http::HeaderMap::new();
198-
trailers.insert("grpc-status", http::HeaderValue::from_static("5"));
199-
Ok(Some(trailers))
200-
})))
190+
.body(BoxBody::new(MockBody::grpc_status(5)))
201191
.unwrap(),
202192
)
203193
},
@@ -267,9 +257,7 @@ async fn grpc_request_statuses_error_body() {
267257
|tx| {
268258
tx.send_response(
269259
http::Response::builder()
270-
.body(BoxBody::new(MockBody::new(async {
271-
Err("a spooky ghost".into())
272-
})))
260+
.body(BoxBody::new(MockBody::error("a spooky ghost")))
273261
.unwrap(),
274262
)
275263
},

linkerd/app/outbound/src/http/logical/policy/route/metrics/tests.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ async fn http_request_statuses() {
9292
tx.send_response(
9393
http::Response::builder()
9494
.status(200)
95-
.body(BoxBody::new(MockBody::new(async {
96-
Err("a spooky ghost".into())
97-
})))
95+
.body(BoxBody::new(MockBody::error("a spooky ghost")))
9896
.unwrap(),
9997
)
10098
})
@@ -264,11 +262,7 @@ async fn grpc_request_statuses_ok() {
264262
|tx| {
265263
tx.send_response(
266264
http::Response::builder()
267-
.body(BoxBody::new(MockBody::trailers(async move {
268-
let mut trailers = http::HeaderMap::new();
269-
trailers.insert("grpc-status", http::HeaderValue::from_static("0"));
270-
Ok(Some(trailers))
271-
})))
265+
.body(BoxBody::new(MockBody::grpc_status(0)))
272266
.unwrap(),
273267
)
274268
},
@@ -310,11 +304,7 @@ async fn grpc_request_statuses_not_found() {
310304
|tx| {
311305
tx.send_response(
312306
http::Response::builder()
313-
.body(BoxBody::new(MockBody::trailers(async move {
314-
let mut trailers = http::HeaderMap::new();
315-
trailers.insert("grpc-status", http::HeaderValue::from_static("5"));
316-
Ok(Some(trailers))
317-
})))
307+
.body(BoxBody::new(MockBody::grpc_status(5)))
318308
.unwrap(),
319309
)
320310
},
@@ -388,9 +378,7 @@ async fn grpc_request_statuses_error_body() {
388378
|tx| {
389379
tx.send_response(
390380
http::Response::builder()
391-
.body(BoxBody::new(MockBody::new(async {
392-
Err("a spooky ghost".into())
393-
})))
381+
.body(BoxBody::new(MockBody::error("a spooky ghost")))
394382
.unwrap(),
395383
)
396384
},

linkerd/app/outbound/src/http/logical/tests.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ async fn mk_grpc_rsp(code: tonic::Code) -> Result<Response> {
131131
Ok(http::Response::builder()
132132
.version(::http::Version::HTTP_2)
133133
.header("content-type", "application/grpc")
134-
.body(BoxBody::new(MockBody::trailers(async move {
135-
let mut trls = http::HeaderMap::default();
136-
trls.insert("grpc-status", (code as u8).to_string().parse().unwrap());
137-
Ok(Some(trls))
138-
})))
134+
.body(BoxBody::new(MockBody::grpc_status(code as u8)))
139135
.unwrap())
140136
}
141137

linkerd/app/outbound/src/http/logical/tests/timeouts.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ async fn request_timeout_request_body() {
7474
svc.clone(),
7575
http::Request::builder()
7676
.method("POST")
77-
.body(BoxBody::new(MockBody::new(async move {
78-
futures::future::pending().await
79-
})))
77+
.body(BoxBody::new(MockBody::pending()))
8078
.unwrap(),
8179
);
8280

@@ -119,9 +117,7 @@ async fn request_timeout_response_body() {
119117
future::ok(
120118
http::Response::builder()
121119
.status(200)
122-
.body(http::BoxBody::new(MockBody::new(async move {
123-
futures::future::pending().await
124-
})))
120+
.body(BoxBody::new(MockBody::pending()))
125121
.unwrap(),
126122
),
127123
)
@@ -203,9 +199,7 @@ async fn response_timeout_response_body() {
203199
info!("Serving a response that never completes");
204200
Ok(http::Response::builder()
205201
.status(200)
206-
.body(http::BoxBody::new(MockBody::new(async move {
207-
futures::future::pending().await
208-
})))
202+
.body(http::BoxBody::new(MockBody::pending()))
209203
.unwrap())
210204
})
211205
.await;
@@ -252,9 +246,7 @@ async fn response_timeout_ignores_request_body() {
252246
info!("Serving a response that never completes");
253247
Ok(http::Response::builder()
254248
.status(200)
255-
.body(http::BoxBody::new(MockBody::new(async move {
256-
futures::future::pending().await
257-
})))
249+
.body(http::BoxBody::new(MockBody::pending()))
258250
.unwrap())
259251
})
260252
.await;
@@ -286,9 +278,7 @@ async fn idle_timeout_response_body() {
286278
info!("Serving a response that never completes");
287279
Ok(http::Response::builder()
288280
.status(200)
289-
.body(http::BoxBody::new(MockBody::new(async move {
290-
futures::future::pending().await
291-
})))
281+
.body(http::BoxBody::new(MockBody::pending()))
292282
.unwrap())
293283
})
294284
.await;

linkerd/app/outbound/src/test_util.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,32 @@ mod mock_body {
9898
}
9999
}
100100

101-
pub fn trailers(
102-
trailers: impl Future<Output = Result<Option<http::HeaderMap>>> + Send + 'static,
103-
) -> Self {
101+
/// Returns a [`MockBody`] that never yields any data.
102+
pub fn pending() -> Self {
103+
let fut = futures::future::pending();
104+
Self::new(fut)
105+
}
106+
107+
/// Returns a [`MockBody`] that yields an error when polled.
108+
pub fn error(msg: &'static str) -> Self {
109+
let err = Err(msg.into());
110+
let fut = futures::future::ready(err);
111+
Self::new(fut)
112+
}
113+
114+
/// Returns a [`MockBody`] that yields this gRPC code in its trailers section.
115+
pub fn grpc_status(code: u8) -> Self {
116+
let trailers = {
117+
let mut trailers = http::HeaderMap::with_capacity(1);
118+
let status = code.to_string().parse().unwrap();
119+
trailers.insert("grpc-status", status);
120+
trailers
121+
};
122+
let fut = futures::future::ready(Ok(Some(trailers)));
123+
104124
Self {
105125
data: None,
106-
trailers: Some(Box::pin(trailers)),
126+
trailers: Some(Box::pin(fut)),
107127
}
108128
}
109129
}

0 commit comments

Comments
 (0)