Skip to content

Commit 5178a02

Browse files
mightyiamjfly
andcommitted
chore: use async closures
Co-authored-by: Jeremy Fleischman <jeremyfleischman@gmail.com>
1 parent 838d681 commit 5178a02

File tree

19 files changed

+414
-444
lines changed

19 files changed

+414
-444
lines changed

Cargo.lock

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ anyhow = {features = ["backtrace"], version = "*"}
1212
camino = {features = ["serde1"], version = "*"}
1313
confique = {default-features = false, version = "*"}
1414
derive_more = {features = ["deref", "from"], version = "*"}
15-
futures = "*"
1615
getset = "*"
1716
gix = {features = ["revision"], version = "*"}
1817
serde = "*"

crates/api/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ sqlx = {default-features = false, features = ["macros", "migrate"], version = "*
77
tokio = "*"
88
anyhow.workspace = true
99
confique.workspace = true
10-
futures.workspace = true
1110
pr-tracker-store.workspace = true
1211
serde.workspace = true
1312
thiserror.workspace = true

crates/api/test-util.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use db_context::LogDestination;
2-
use futures::{future::LocalBoxFuture, FutureExt};
32
use poem::endpoint::BoxEndpoint;
43

54
#[derive(getset::Getters, getset::MutGetters)]
@@ -13,35 +12,23 @@ pub struct TestContext {
1312
}
1413

1514
impl TestContext {
16-
pub async fn with(test: impl FnOnce(TestContext) -> LocalBoxFuture<'static, ()> + 'static) {
15+
pub async fn with(test: impl AsyncFnOnce(TestContext)) {
1716
db_context::DatabaseContext::with(
18-
|db_context| {
19-
async {
20-
let db_url = db_context.db_url();
21-
let endpoint = pr_tracker_api::endpoint(&db_url).await;
17+
async |db_context| {
18+
let db_url = db_context.db_url();
19+
let endpoint = pr_tracker_api::endpoint(&db_url).await;
2220

23-
let api_client = poem::test::TestClient::new(endpoint);
21+
let api_client = poem::test::TestClient::new(endpoint);
2422

25-
let this = TestContext {
26-
db: db_context,
27-
client: api_client,
28-
};
23+
let this = TestContext {
24+
db: db_context,
25+
client: api_client,
26+
};
2927

30-
test(this).await
31-
}
32-
.boxed_local()
28+
test(this).await
3329
},
3430
LogDestination::Inherit,
3531
)
3632
.await;
3733
}
3834
}
39-
40-
macro_rules! test {
41-
($name:ident, $test:expr) => {
42-
#[tokio::test]
43-
async fn $name() {
44-
TestContext::with(|ctx| async { $test(ctx).await }.boxed()).await;
45-
}
46-
};
47-
}

crates/api/tests/healthcheck.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
#[macro_use]
33
mod test_util;
44

5-
use futures::FutureExt;
65
use poem::http::StatusCode;
76
use test_util::TestContext;
87

9-
test![healthcheck_ok, |ctx: TestContext| async move {
10-
let response = ctx.client().get("/api/v2/healthcheck").send().await;
11-
response.assert_status_is_ok();
12-
}];
8+
#[tokio::test]
9+
async fn healthcheck_ok() {
10+
TestContext::with(async |ctx| {
11+
let response = ctx.client().get("/api/v2/healthcheck").send().await;
12+
response.assert_status_is_ok();
13+
})
14+
.await;
15+
}
1316

14-
test![healthcheck_not_ok, |mut ctx: TestContext| async move {
15-
ctx.db_mut().kill_db().unwrap();
16-
let response = ctx.client().get("/api/v2/healthcheck").send().await;
17-
response.assert_status(StatusCode::SERVICE_UNAVAILABLE);
18-
}];
17+
#[tokio::test]
18+
async fn healthcheck_not_ok() {
19+
TestContext::with(async |mut ctx| {
20+
ctx.db_mut().kill_db().unwrap();
21+
let response = ctx.client().get("/api/v2/healthcheck").send().await;
22+
response.assert_status(StatusCode::SERVICE_UNAVAILABLE);
23+
})
24+
.await;
25+
}

crates/api/tests/landings.rs

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
#[macro_use]
33
mod test_util;
44

5-
use futures::FutureExt;
65
use poem::http::StatusCode;
76
use test_util::TestContext;
87

9-
test![negative_pr_number, |ctx: TestContext| async move {
10-
let response = ctx.client().get("/api/v2/landings/-1").send().await;
11-
response.assert_status(StatusCode::BAD_REQUEST);
12-
response
13-
.assert_text("Pull request number non-positive.")
14-
.await;
15-
}];
8+
#[tokio::test]
9+
async fn negative_pr_number() {
10+
TestContext::with(async |ctx| {
11+
let response = ctx.client().get("/api/v2/landings/-1").send().await;
12+
response.assert_status(StatusCode::BAD_REQUEST);
13+
response
14+
.assert_text("Pull request number non-positive.")
15+
.await;
16+
})
17+
.await;
18+
}
1619

1720
// https://github.com/molybdenumsoftware/pr-tracker/issues/241
1821
// async fn bork_db(ctx: &mut db_context::DatabaseContext) {
@@ -26,63 +29,79 @@ test![negative_pr_number, |ctx: TestContext| async move {
2629
// .await;
2730
// }
2831
//
29-
// test![internal_landed_error, |mut ctx: TestContext| async move {
30-
// bork_db(ctx.db_mut()).await;
31-
// let response = ctx.client().get("/api/v2/landings/1").send().await;
32-
// response.assert_status(StatusCode::SERVICE_UNAVAILABLE);
33-
// response.assert_text("Error. Sorry.").await;
34-
// }];
32+
// #[tokio::test]
33+
// async fn internal_landed_error() {
34+
// TestContext::with(async |mut ctx| {
35+
// bork_db(ctx.db_mut()).await;
36+
// let response = ctx.client().get("/api/v2/landings/1").send().await;
37+
// response.assert_status(StatusCode::SERVICE_UNAVAILABLE);
38+
// response.assert_text("Error. Sorry.").await;
39+
// })
40+
// .await;
41+
// }
3542

36-
test![pr_not_found, |ctx: TestContext| async move {
37-
let response = ctx.client().get("/api/v2/landings/2134").send().await;
38-
response.assert_status(StatusCode::NO_CONTENT);
39-
response.assert_text("Pull request not found.").await;
40-
}];
43+
#[tokio::test]
44+
async fn pr_not_found() {
45+
TestContext::with(async |ctx| {
46+
let response = ctx.client().get("/api/v2/landings/2134").send().await;
47+
response.assert_status(StatusCode::NO_CONTENT);
48+
response.assert_text("Pull request not found.").await;
49+
})
50+
.await;
51+
}
4152

42-
test![pr_not_landed, |ctx: TestContext| async move {
43-
let mut connection = ctx.db().connection().await.unwrap();
53+
#[tokio::test]
54+
async fn pr_not_landed() {
55+
TestContext::with(async |ctx| {
56+
let mut connection = ctx.db().connection().await.unwrap();
4457

45-
pr_tracker_store::Pr {
46-
number: 123.try_into().unwrap(),
47-
commit: Some("deadbeef".into()),
48-
}
49-
.upsert(&mut connection)
50-
.await
51-
.unwrap();
58+
pr_tracker_store::Pr {
59+
number: 123.try_into().unwrap(),
60+
commit: Some("deadbeef".into()),
61+
}
62+
.upsert(&mut connection)
63+
.await
64+
.unwrap();
5265

53-
let response = ctx.client().get("/api/v2/landings/123").send().await;
54-
response.assert_status_is_ok();
55-
response
56-
.assert_json(pr_tracker_api::LandedIn { branches: vec![] })
57-
.await;
58-
}];
66+
let response = ctx.client().get("/api/v2/landings/123").send().await;
67+
response.assert_status_is_ok();
68+
response
69+
.assert_json(pr_tracker_api::LandedIn { branches: vec![] })
70+
.await;
71+
})
72+
.await;
73+
}
5974

60-
test![pr_landed, |ctx: TestContext| async move {
61-
let connection = &mut ctx.db().connection().await.unwrap();
75+
#[tokio::test]
76+
async fn pr_landed() {
77+
TestContext::with(async |ctx| {
78+
let connection = &mut ctx.db().connection().await.unwrap();
6279

63-
let branch = pr_tracker_store::Branch::get_or_insert(connection, "nixos-unstable")
64-
.await
65-
.unwrap();
80+
let branch = pr_tracker_store::Branch::get_or_insert(connection, "nixos-unstable")
81+
.await
82+
.unwrap();
6683

67-
let github_pr = pr_tracker_store::Pr {
68-
number: 2134.try_into().unwrap(),
69-
commit: Some("deadbeef".into()),
70-
};
71-
github_pr.clone().upsert(connection).await.unwrap();
84+
let github_pr = pr_tracker_store::Pr {
85+
number: 2134.try_into().unwrap(),
86+
commit: Some("deadbeef".into()),
87+
};
88+
github_pr.clone().upsert(connection).await.unwrap();
7289

73-
let landing = pr_tracker_store::Landing {
74-
github_pr: github_pr.number,
75-
branch_id: branch.id(),
76-
};
90+
let landing = pr_tracker_store::Landing {
91+
github_pr: github_pr.number,
92+
branch_id: branch.id(),
93+
};
7794

78-
landing.upsert(connection).await.unwrap();
95+
landing.upsert(connection).await.unwrap();
7996

80-
let response = ctx.client().get("/api/v2/landings/2134").send().await;
81-
response.assert_status_is_ok();
97+
let response = ctx.client().get("/api/v2/landings/2134").send().await;
98+
response.assert_status_is_ok();
8299

83-
response
84-
.assert_json(pr_tracker_api::LandedIn {
85-
branches: vec![pr_tracker_api::Branch("nixos-unstable".to_owned())],
86-
})
87-
.await;
88-
}];
100+
response
101+
.assert_json(pr_tracker_api::LandedIn {
102+
branches: vec![pr_tracker_api::Branch("nixos-unstable".to_owned())],
103+
})
104+
.await;
105+
})
106+
.await;
107+
}

crates/api/tests/openapi.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,35 @@
22
#[macro_use]
33
mod test_util;
44

5-
use futures::FutureExt;
65
use poem::http::StatusCode;
76
use test_util::TestContext;
87

9-
test![json, |ctx: TestContext| async move {
10-
let response = ctx.client().get("/openapi.json").send().await;
11-
response.assert_status_is_ok();
12-
response.assert_content_type("application/json");
13-
}];
8+
#[tokio::test]
9+
async fn json() {
10+
TestContext::with(async |ctx| {
11+
let response = ctx.client().get("/openapi.json").send().await;
12+
response.assert_status_is_ok();
13+
response.assert_content_type("application/json");
14+
})
15+
.await;
16+
}
1417

15-
test![homepage_redirect, |ctx: TestContext| async move {
16-
let response = ctx.client().get("/").send().await;
17-
response.assert_status(StatusCode::SEE_OTHER);
18-
response.assert_header("Location", "/api-docs");
19-
}];
18+
#[tokio::test]
19+
async fn homepage_redirect() {
20+
TestContext::with(async |ctx| {
21+
let response = ctx.client().get("/").send().await;
22+
response.assert_status(StatusCode::SEE_OTHER);
23+
response.assert_header("Location", "/api-docs");
24+
})
25+
.await;
26+
}
2027

21-
test![api_docs, |ctx: TestContext| async move {
22-
let response = ctx.client().get("/api-docs").send().await;
23-
response.assert_status_is_ok();
24-
response.assert_content_type("text/html; charset=utf-8");
25-
}];
28+
#[tokio::test]
29+
async fn api_docs() {
30+
TestContext::with(async |ctx| {
31+
let response = ctx.client().get("/api-docs").send().await;
32+
response.assert_status_is_ok();
33+
response.assert_content_type("text/html; charset=utf-8");
34+
})
35+
.await;
36+
}

crates/api/tests/sd-notify.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use db_context::DatabaseContext;
22
use fragile_child::SpawnFragileChild;
3-
use futures::{self, FutureExt};
43
use std::process::Command;
54

65
#[tokio::test]
@@ -15,21 +14,18 @@ async fn notifies() {
1514
const EXPECTED: &[u8] = b"READY=1\n";
1615

1716
DatabaseContext::with(
18-
move |db_context| {
19-
async move {
20-
let mut child = Command::new(api_exe)
21-
.env("NOTIFY_SOCKET", &socket_path)
22-
.env("PR_TRACKER_API_DATABASE_URL", db_context.db_url())
23-
.env("PR_TRACKER_API_PORT", "4242") // Use a socket instead: https://github.com/molybdenumsoftware/pr-tracker/issues/216
24-
.spawn_fragile()
25-
.unwrap();
17+
async |db_context| {
18+
let mut child = Command::new(api_exe)
19+
.env("NOTIFY_SOCKET", &socket_path)
20+
.env("PR_TRACKER_API_DATABASE_URL", db_context.db_url())
21+
.env("PR_TRACKER_API_PORT", "4242") // Use a socket instead: https://github.com/molybdenumsoftware/pr-tracker/issues/216
22+
.spawn_fragile()
23+
.unwrap();
2624

27-
let mut buf = [0; EXPECTED.len()];
28-
sock.recv(&mut buf).unwrap();
29-
assert_eq!(buf, EXPECTED);
30-
child.kill().unwrap();
31-
}
32-
.boxed_local()
25+
let mut buf = [0; EXPECTED.len()];
26+
sock.recv(&mut buf).unwrap();
27+
assert_eq!(buf, EXPECTED);
28+
child.kill().unwrap();
3329
},
3430
db_context::LogDestination::Inherit,
3531
)

0 commit comments

Comments
 (0)