fix: run async runtime when dropping TestContext in BlockingTestContext

This commit is contained in:
Daniil Polyakov
2026-02-12 19:35:07 +03:00
parent 378d53f152
commit d2ce0cd51b
2 changed files with 34 additions and 14 deletions
+23 -3
View File
@@ -321,15 +321,22 @@ impl Drop for TestContext {
/// A test context to be used in normal #[test] tests
pub struct BlockingTestContext {
pub ctx: TestContext,
pub runtime: tokio::runtime::Runtime,
ctx: Option<TestContext>,
runtime: tokio::runtime::Runtime,
}
impl BlockingTestContext {
pub fn new() -> Result<Self> {
let runtime = tokio::runtime::Runtime::new().unwrap();
let ctx = runtime.block_on(TestContext::new())?;
Ok(Self { ctx, runtime })
Ok(Self {
ctx: Some(ctx),
runtime,
})
}
pub fn ctx(&self) -> &TestContext {
self.ctx.as_ref().expect("TestContext is set")
}
}
@@ -370,6 +377,19 @@ impl TestContextBuilder {
}
}
impl Drop for BlockingTestContext {
fn drop(&mut self) {
let Self { ctx, runtime } = self;
// Ensure async cleanup of TestContext by blocking on its drop in the runtime.
runtime.block_on(async {
if let Some(ctx) = ctx.take() {
drop(ctx);
}
})
}
}
pub fn format_public_account_id(account_id: AccountId) -> String {
format!("Public/{account_id}")
}