Add example requiring async testing.

This commit is contained in:
Sergio Benitez 2020-07-08 18:36:28 -07:00
parent 96bafd8ac5
commit 832408ea9b
4 changed files with 54 additions and 21 deletions

View File

@ -43,7 +43,7 @@ atomic = "0.4"
[dependencies.tokio] [dependencies.tokio]
version = "0.2.9" version = "0.2.9"
features = ["fs", "io-std", "io-util", "rt-threaded", "sync", "signal"] features = ["fs", "io-std", "io-util", "rt-threaded", "sync", "signal", "macros"]
[build-dependencies] [build-dependencies]
yansi = "0.5" yansi = "0.5"

View File

@ -0,0 +1,40 @@
use rocket::State;
use rocket::fairing::AdHoc;
use rocket::tokio::sync::Barrier;
#[get("/barrier")]
async fn rendezvous(barrier: State<'_, Barrier>) -> &'static str {
println!("Waiting for second task...");
barrier.wait().await;
"Rendezvous reached."
}
pub fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![rendezvous])
.attach(AdHoc::on_attach("Add Channel", |rocket| async {
Ok(rocket.manage(Barrier::new(2)))
}))
}
#[cfg(test)]
mod test {
use super::rocket;
use rocket::http::Status;
#[rocket::async_test]
async fn test_rendezvous() {
use rocket::local::asynchronous::Client;
let client = Client::new(rocket()).await.unwrap();
let req = client.get("/barrier");
let (r1, r2) = rocket::tokio::join!(req.clone().dispatch(), req.dispatch());
assert_eq!(r1.status(), r2.status());
assert_eq!(r1.status(), Status::Ok);
let (s1, s2) = (r1.into_string().await, r2.into_string().await);
assert_eq!(s1, s2);
assert_eq!(s1.unwrap(), "Rendezvous reached.");
}
}

View File

@ -2,14 +2,16 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
mod async_required;
#[get("/")] #[get("/")]
fn hello() -> &'static str { fn hello() -> &'static str {
"Hello, world!" "Hello, world!"
} }
#[rocket::launch] #[launch]
fn rocket() -> rocket::Rocket { fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", routes![hello]) async_required::rocket().mount("/", routes![hello])
} }
#[cfg(test)] #[cfg(test)]
@ -17,18 +19,8 @@ mod test {
use super::rocket; use super::rocket;
use rocket::http::Status; use rocket::http::Status;
#[rocket::async_test]
async fn test_hello_async() {
use rocket::local::asynchronous::Client;
let client = Client::new(rocket()).await.unwrap();
let response = client.get("/").dispatch().await;
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_string().await, Some("Hello, world!".into()));
}
#[test] #[test]
fn test_hello_blocking() { fn test_hello() {
use rocket::local::blocking::Client; use rocket::local::blocking::Client;
let client = Client::new(rocket()).unwrap(); let client = Client::new(rocket()).unwrap();

View File

@ -258,16 +258,17 @@ The tests can be run with `cargo test`. You can find the full source code to
You may have noticed the use of a "`blocking`" API in these examples, even You may have noticed the use of a "`blocking`" API in these examples, even
though `Rocket` is an `async` web framework. In most situations, the `blocking` though `Rocket` is an `async` web framework. In most situations, the `blocking`
testing API is easier to use. When concurrent execution of two or more requests testing API is easier to use and should be preferred. However, when concurrent
is required for the server to make progress, you will need the more flexible execution of two or more requests is required for the server to make progress,
`asynchronous` API; the `blocking` API is not capable of dispatching multiple you will need the more flexible `asynchronous` API; the `blocking` API is not
requests simultaneously. For more information, see the [`rocket::local`] and capable of dispatching multiple requests simultaneously. While synthetic, the
[`rocket::local::asynchronous`] documentation as well as the asynchronous [`async_required` `testing` example] uses an `async` barrier to demonstrate such
version of [the `testing` example]. a case. For more information, see the [`rocket::local`] and
[`rocket::local::asynchronous`] documentation.
[`rocket::local`]: @api/rocket/local/index.html [`rocket::local`]: @api/rocket/local/index.html
[`rocket::local::asynchronous`]: @api/rocket/local/asynchronous/index.html [`rocket::local::asynchronous`]: @api/rocket/local/asynchronous/index.html
[the `testing` example]: @example/testing/src/main.rs [`async_required` `testing` example]: @example/testing/src/async_required.rs
## Codegen Debug ## Codegen Debug