Issue with Integration Tests for actix-ws and sqlx Compatibility #3161
-
Dear maintainers, First of all, I want to express my gratitude for the fantastic work on this project. Problem DescriptionI am currently working on integration tests for an actix-ws that utilizes the sqlx using [actix-test] project with (https://crates.io/crates/actix-test). Since actix-ws (actorless) relies on a single-threaded runtime for its Websocket functionality, which is different from Rust's multithreaded tests. Consequently, I consistently encounter the error message mentioned above. I've made attempts to resolve this issue by wrapping the test with LocalSet::new().run_until(async {test here}).await. Unfortunately, these attempts have resulted in tests getting stuck midway through execution. Furthermore, I've observed that the Request for GuidanceI would greatly appreciate any guidance or suggestions on how to resolve this issue effectively. I believe this problem may be related to the integration of Actix-WS and Sqlx within the testing framework. Thank you for your time and assistance in advance. #[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let config = get_configuration().unwrap();
let pool = get_pool(&config.db);
let http_server = HttpServer::new(move || {
App::new()
.app_data(web::Data::new(...))
.app_data(web::Data::new(pool.clone()))
.service(web::resource("/websocket").route(web::get().to(spawn_ws)))
})
.bind(("0.0.0.0", port))?
.run();
}
pub async fn spawn_ws(
req: HttpRequest,
body: web::Payload,
my_server_handle: web::Data<MyServerHandle>,
) -> Result<HttpResponse, MyError> {
let (response, session, msg_stream) =
actix_ws::handle(&req, body).map_err(|e| MyError::InternalServerError)?;
tokio::task::spawn_local(
// calls something
);
Ok(response)
}
fn setup_test_client () {
let mut client = actix_test::start(move || {
App::new()
.app_data(web::Data::new(...))
.app_data(web::Data::new(pool.clone()))
.service(web::resource("/websocket").route(web::get().to(spawn_ws)))
})
}
#[sqlx::test(fixtures("game_config", "game_player", "game_watcher"))]
async fn should_join_the_room(pool: Pool<Postgres>) {
let mut framed = client.ws_at("/websocket").await.unwrap();
let text = ClientMessage::JoinChannel { channel_id }.to_string();
framed.send(ws::Message::Text(text.into())).await.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(...)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
See discussion here: launchbadge/sqlx#2105 |
Beta Was this translation helpful? Give feedback.
See discussion here: launchbadge/sqlx#2105