Skip to content

Commit b1c6716

Browse files
committed
test concurrent requests
1 parent 4821c4b commit b1c6716

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

tests/any_component.sql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
select $component as component;
2+
select
3+
'It works !' as title,
4+
'It works !' as description;
5+
6+
select 'divider' as component, 'the end' as contents;

tests/index.rs

+40-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async fn test_index_ok() {
1111
assert_eq!(resp.status(), http::StatusCode::OK);
1212
let body = test::read_body(resp).await;
1313
assert!(body.starts_with(b"<!DOCTYPE html>"));
14-
// the body should contain the strint "It works!" and should not contain the string "error"
14+
// the body should contain the string "It works!" and should not contain the string "error"
1515
let body = String::from_utf8(body.to_vec()).unwrap();
1616
assert!(body.contains("It works !"));
1717
assert!(!body.contains("error"));
@@ -43,6 +43,40 @@ async fn test_404() {
4343
}
4444
}
4545

46+
#[actix_web::test]
47+
async fn test_concurrent_requests() {
48+
// send 32 requests (less than the default postgres pool size)
49+
// at the same time to /tests/multiple_components.sql
50+
let components = [
51+
"table", "form", "card", "datagrid", "hero", "list", "timeline",
52+
];
53+
let reqs = (0..32)
54+
.map(|i| {
55+
let component = components[i % components.len()];
56+
req_path(format!("/tests/any_component.sql?component={}", component))
57+
})
58+
.collect::<Vec<_>>();
59+
// send all requests at the same time
60+
let results = futures_util::future::join_all(reqs).await;
61+
// check that all requests succeeded
62+
for result in results.into_iter() {
63+
let resp = result.unwrap();
64+
assert_eq!(resp.status(), http::StatusCode::OK);
65+
let body = test::read_body(resp).await;
66+
assert!(
67+
body.starts_with(b"<!DOCTYPE html>"),
68+
"Expected html doctype"
69+
);
70+
// the body should contain the string "It works!" and should not contain the string "error"
71+
let body = String::from_utf8(body.to_vec()).unwrap();
72+
assert!(
73+
body.contains("It works !"),
74+
"Expected to contain: It works !, but got: {body}"
75+
);
76+
assert!(!body.contains("error"));
77+
}
78+
}
79+
4680
#[actix_web::test]
4781
async fn test_files() {
4882
// Iterate over all the sql test files in the tests/ directory
@@ -59,7 +93,7 @@ async fn test_files() {
5993
let resp = req_path(&req_str).await.unwrap();
6094
let body = test::read_body(resp).await;
6195
assert!(body.starts_with(b"<!DOCTYPE html>"));
62-
// the body should contain the strint "It works!" and should not contain the string "error"
96+
// the body should contain the string "It works!" and should not contain the string "error"
6397
let body = String::from_utf8(body.to_vec()).unwrap();
6498
let lowercase_body = body.to_lowercase();
6599
if stem.starts_with("it_works") {
@@ -148,8 +182,10 @@ async fn get_request_to(path: &str) -> actix_web::Result<TestRequest> {
148182
Ok(test::TestRequest::get().uri(path).app_data(data))
149183
}
150184

151-
async fn req_path(path: &str) -> Result<actix_web::dev::ServiceResponse, actix_web::Error> {
152-
let req = get_request_to(path)
185+
async fn req_path(
186+
path: impl AsRef<str>,
187+
) -> Result<actix_web::dev::ServiceResponse, actix_web::Error> {
188+
let req = get_request_to(path.as_ref())
153189
.await?
154190
.insert_header(ContentType::plaintext())
155191
.to_srv_request();

0 commit comments

Comments
 (0)