Skip to content

Commit f5daac8

Browse files
committed
fix: use spawn instead of block_on in TestDatabase Drop to avoid runtime nesting
The tokio #[tokio::test] macro creates its own runtime, so calling block_on from Drop causes a 'Cannot start a runtime from within a runtime' panic in CI. Using spawn() detaches the cleanup task instead.
1 parent b1e9602 commit f5daac8

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

crates/tsql/tests/common/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,16 @@ impl TestDatabase {
7373

7474
Ok(client)
7575
}
76-
7776
}
7877

7978
impl Drop for TestDatabase {
8079
fn drop(&mut self) {
8180
let admin_client = self.admin_client.clone();
8281
let db_name = self.db_name.clone();
8382

84-
// Use block_on to run the cleanup synchronously in Drop
85-
// This ensures the database is dropped even if the test panics
86-
let _ = self.rt.block_on(async move {
83+
// Spawn the cleanup as a background task
84+
// We can't use block_on inside an async runtime, so we spawn and detach
85+
self.rt.spawn(async move {
8786
let client = admin_client.lock().await;
8887

8988
// Terminate connections
@@ -178,10 +177,7 @@ mod tests {
178177
});
179178

180179
let result = admin_client
181-
.query(
182-
"SELECT 1 FROM pg_database WHERE datname = $1",
183-
&[&db_name],
184-
)
180+
.query("SELECT 1 FROM pg_database WHERE datname = $1", &[&db_name])
185181
.await
186182
.unwrap();
187183

crates/tsql/tests/integration_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod common;
44

5-
use common::{TestDatabase, get_test_database_url};
5+
use common::{get_test_database_url, TestDatabase};
66

77
/// Test that we can connect to a PostgreSQL database successfully.
88
#[tokio::test]
@@ -47,7 +47,9 @@ async fn test_query_error_message() {
4747
let client = test_db.connect().await.unwrap();
4848

4949
// Execute an invalid query
50-
let result = client.simple_query("SELECT * FROM nonexistent_table_xyz").await;
50+
let result = client
51+
.simple_query("SELECT * FROM nonexistent_table_xyz")
52+
.await;
5153

5254
assert!(result.is_err(), "Query should fail");
5355

0 commit comments

Comments
 (0)