Skip to content

Commit

Permalink
review(app/inbound): use ServiceExt::oneshot(..)
Browse files Browse the repository at this point in the history
#3428 (comment)

we can simplify these statements sending requests, by using
`ServiceExt::oneshot(..)`.

Signed-off-by: katelyn martin <[email protected]>
  • Loading branch information
cratelyn committed Dec 6, 2024
1 parent ebc3832 commit d16ccc0
Showing 1 changed file with 26 additions and 73 deletions.
99 changes: 26 additions & 73 deletions linkerd/app/inbound/src/http/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use linkerd_app_test::connect::ConnectFuture;
use linkerd_tracing::test::trace_init;
use std::{net::SocketAddr, sync::Arc};
use tokio::time;
use tower::{Service, ServiceExt};
use tower::ServiceExt;
use tracing::Instrument;

fn build_server<I>(
Expand Down Expand Up @@ -66,26 +66,22 @@ async fn unmeshed_http1_hello_world() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::UNMESHED_HTTP1);
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

let req = Request::builder()
.method(http::Method::GET)
.uri("http://foo.svc.cluster.local:5550")
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
assert_eq!(rsp.status(), http::StatusCode::OK);
let body = http_util::body_to_string(rsp.into_body()).await.unwrap();
assert_eq!(body, "Hello world!");

drop(client);
bg.await.expect("background task failed");
}

Expand All @@ -112,7 +108,7 @@ async fn downgrade_origin_form() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::UNMESHED_H2);
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

let req = Request::builder()
.method(http::Method::GET)
Expand All @@ -122,18 +118,14 @@ async fn downgrade_origin_form() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
assert_eq!(rsp.status(), http::StatusCode::OK);
let body = http_util::body_to_string(rsp.into_body()).await.unwrap();
assert_eq!(body, "Hello world!");

drop(client);
bg.await.expect("background task failed");
}

Expand All @@ -159,7 +151,7 @@ async fn downgrade_absolute_form() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::UNMESHED_H2);
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

let req = Request::builder()
.method(http::Method::GET)
Expand All @@ -169,18 +161,14 @@ async fn downgrade_absolute_form() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
assert_eq!(rsp.status(), http::StatusCode::OK);
let body = http_util::body_to_string(rsp.into_body()).await.unwrap();
assert_eq!(body, "Hello world!");

drop(client);
bg.await.expect("background task failed");
}

Expand All @@ -202,7 +190,7 @@ async fn http1_bad_gateway_meshed_response_error_header() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::meshed_http1());
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is a BAD_GATEWAY with the expected
// header message.
Expand All @@ -212,10 +200,7 @@ async fn http1_bad_gateway_meshed_response_error_header() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -226,7 +211,6 @@ async fn http1_bad_gateway_meshed_response_error_header() {
// logical error context is added.
check_error_header(rsp.headers(), "server is not listening");

drop(client);
bg.await.expect("background task failed");
}

Expand All @@ -248,7 +232,7 @@ async fn http1_bad_gateway_unmeshed_response() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::UNMESHED_HTTP1);
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is a BAD_GATEWAY with the expected
// header message.
Expand All @@ -258,10 +242,7 @@ async fn http1_bad_gateway_unmeshed_response() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -271,7 +252,6 @@ async fn http1_bad_gateway_unmeshed_response() {
"response must not contain L5D_PROXY_ERROR header"
);

drop(client);
bg.await.expect("background task failed");
}

Expand All @@ -297,7 +277,7 @@ async fn http1_connect_timeout_meshed_response_error_header() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::meshed_http1());
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is a GATEWAY_TIMEOUT with the
// expected header message.
Expand All @@ -307,10 +287,7 @@ async fn http1_connect_timeout_meshed_response_error_header() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -322,7 +299,6 @@ async fn http1_connect_timeout_meshed_response_error_header() {
// logical error context is added.
check_error_header(rsp.headers(), "connect timed out after 1s");

drop(client);
bg.await.expect("background task failed");
}

Expand All @@ -348,7 +324,7 @@ async fn http1_connect_timeout_unmeshed_response_error_header() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::UNMESHED_HTTP1);
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is a GATEWAY_TIMEOUT with the
// expected header message.
Expand All @@ -358,10 +334,7 @@ async fn http1_connect_timeout_unmeshed_response_error_header() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -371,7 +344,6 @@ async fn http1_connect_timeout_unmeshed_response_error_header() {
"response must not contain L5D_PROXY_ERROR header"
);

drop(client);
bg.await.expect("background task failed");
}

Expand All @@ -393,7 +365,7 @@ async fn h2_response_meshed_error_header() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::meshed_h2());
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is SERVICE_UNAVAILABLE with the
// expected header message.
Expand All @@ -403,10 +375,7 @@ async fn h2_response_meshed_error_header() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -417,7 +386,6 @@ async fn h2_response_meshed_error_header() {
// Drop the client and discard the result of awaiting the proxy background
// task. The result is discarded because it hits an error that is related
// to the mock implementation and has no significance to the test.
drop(client);
let _ = bg.await;
}

Expand All @@ -439,7 +407,7 @@ async fn h2_response_unmeshed_error_header() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::UNMESHED_H2);
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is SERVICE_UNAVAILABLE with the
// expected header message.
Expand All @@ -449,10 +417,7 @@ async fn h2_response_unmeshed_error_header() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -465,7 +430,6 @@ async fn h2_response_unmeshed_error_header() {
// Drop the client and discard the result of awaiting the proxy background
// task. The result is discarded because it hits an error that is related
// to the mock implementation and has no significance to the test.
drop(client);
let _ = bg.await;
}

Expand All @@ -487,7 +451,7 @@ async fn grpc_meshed_response_error_header() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::meshed_h2());
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is OK with the expected header
// message.
Expand All @@ -498,10 +462,7 @@ async fn grpc_meshed_response_error_header() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -512,7 +473,6 @@ async fn grpc_meshed_response_error_header() {
// Drop the client and discard the result of awaiting the proxy background
// task. The result is discarded because it hits an error that is related
// to the mock implementation and has no significance to the test.
drop(client);
let _ = bg.await;
}

Expand All @@ -534,7 +494,7 @@ async fn grpc_unmeshed_response_error_header() {
let cfg = default_config();
let (rt, _shutdown) = runtime();
let server = build_server(cfg, rt, profiles, connect).new_service(Target::UNMESHED_H2);
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is OK with the expected header
// message.
Expand All @@ -545,10 +505,7 @@ async fn grpc_unmeshed_response_error_header() {
.body(Body::default())
.unwrap();
let rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand All @@ -561,7 +518,6 @@ async fn grpc_unmeshed_response_error_header() {
// Drop the client and discard the result of awaiting the proxy background
// task. The result is discarded because it hits an error that is related
// to the mock implementation and has no significance to the test.
drop(client);
let _ = bg.await;
}

Expand Down Expand Up @@ -596,7 +552,7 @@ async fn grpc_response_class() {
.http_endpoint
.into_report(time::Duration::from_secs(3600));
let server = build_server(cfg, rt, profiles, connect).new_service(Target::meshed_h2());
let (mut client, bg) = http_util::connect_and_accept(&mut client, server).await;
let (client, bg) = http_util::connect_and_accept(&mut client, server).await;

// Send a request and assert that it is OK with the expected header
// message.
Expand All @@ -608,10 +564,7 @@ async fn grpc_response_class() {
.unwrap();

let mut rsp = client
.ready()
.await
.expect("HTTP client poll_ready failed")
.call(req)
.oneshot(req)
.await
.expect("HTTP client request failed");
tracing::info!(?rsp);
Expand Down Expand Up @@ -649,7 +602,7 @@ async fn grpc_response_class() {
.expect("response_total not found");
assert_eq!(response_total, 1.0);

drop((client, bg));
drop(bg);
}

#[tracing::instrument]
Expand Down

0 comments on commit d16ccc0

Please sign in to comment.