use blazingly::prelude::*;
use futures_lite::future;
#[get("/direct", id = "sync.direct")]
fn direct_handler() -> Json<String> {
Json(current_thread_name())
}
#[get("/offloaded", id = "sync.offloaded")]
async fn offloaded_handler() -> Json<String> {
blazingly::run_blocking(current_thread_name)
.await
.map_or_else(|error| Json(format!("failed: {error}")), Json)
}
fn current_thread_name() -> String {
std::thread::current()
.name()
.unwrap_or("unnamed")
.to_owned()
}
#[test]
fn a_sync_handler_runs_inline_while_run_blocking_still_reaches_the_pool() {
let executable = ExecutableApp::new(routes![direct_handler, offloaded_handler])
.expect("sync path routes should compile");
let app = TestApp::new(&executable);
let direct: String = future::block_on(app.call(Request::get("/direct")))
.json()
.expect("worker name");
assert!(
!direct.starts_with("blazingly-blocking-"),
"a sync handler must not be offloaded to the blocking pool, ran on {direct}"
);
let offloaded: String = future::block_on(app.call(Request::get("/offloaded")))
.json()
.expect("worker name");
assert!(
offloaded.starts_with("blazingly-blocking-"),
"run_blocking must still reach a named pool worker, ran on {offloaded}"
);
}