@@ -9,9 +9,10 @@ use crate::storage::{AsyncStorage, Storage, StorageKind};
99use crate :: web:: { build_axum_app, cache, page:: TemplateData } ;
1010use crate :: { BuildQueue , Config , Context , Index , InstanceMetrics , RegistryApi , ServiceMetrics } ;
1111use anyhow:: Context as _;
12+ use axum:: async_trait;
1213use fn_error_context:: context;
1314use futures_util:: FutureExt ;
14- use once_cell:: unsync :: OnceCell ;
15+ use once_cell:: sync :: OnceCell ;
1516use postgres:: Client as Connection ;
1617use reqwest:: {
1718 blocking:: { Client , ClientBuilder , RequestBuilder , Response } ,
@@ -263,7 +264,7 @@ pub(crate) struct TestEnvironment {
263264 config : OnceCell < Arc < Config > > ,
264265 db : tokio:: sync:: OnceCell < TestDatabase > ,
265266 storage : OnceCell < Arc < Storage > > ,
266- async_storage : OnceCell < Arc < AsyncStorage > > ,
267+ async_storage : tokio :: sync :: OnceCell < Arc < AsyncStorage > > ,
267268 cdn : OnceCell < Arc < CdnBackend > > ,
268269 index : OnceCell < Arc < Index > > ,
269270 registry_api : OnceCell < Arc < RegistryApi > > ,
@@ -298,7 +299,7 @@ impl TestEnvironment {
298299 config : OnceCell :: new ( ) ,
299300 db : tokio:: sync:: OnceCell :: new ( ) ,
300301 storage : OnceCell :: new ( ) ,
301- async_storage : OnceCell :: new ( ) ,
302+ async_storage : tokio :: sync :: OnceCell :: new ( ) ,
302303 cdn : OnceCell :: new ( ) ,
303304 index : OnceCell :: new ( ) ,
304305 registry_api : OnceCell :: new ( ) ,
@@ -391,25 +392,29 @@ impl TestEnvironment {
391392 . clone ( )
392393 }
393394
394- pub ( crate ) fn async_storage ( & self ) -> Arc < AsyncStorage > {
395+ pub ( crate ) async fn async_storage ( & self ) -> Arc < AsyncStorage > {
395396 self . async_storage
396- . get_or_init ( || {
397+ . get_or_init ( || async {
398+ let db = self . async_db ( ) . await ;
397399 Arc :: new (
398- self . runtime ( )
399- . block_on ( AsyncStorage :: new (
400- self . db ( ) . pool ( ) ,
401- self . instance_metrics ( ) ,
402- self . config ( ) ,
403- ) )
400+ AsyncStorage :: new ( db. pool ( ) , self . instance_metrics ( ) , self . config ( ) )
401+ . await
404402 . expect ( "failed to initialize the async storage" ) ,
405403 )
406404 } )
405+ . await
407406 . clone ( )
408407 }
409408
410409 pub ( crate ) fn storage ( & self ) -> Arc < Storage > {
410+ let runtime = self . runtime ( ) ;
411411 self . storage
412- . get_or_init ( || Arc :: new ( Storage :: new ( self . async_storage ( ) , self . runtime ( ) ) ) )
412+ . get_or_init ( || {
413+ Arc :: new ( Storage :: new (
414+ runtime. block_on ( self . async_storage ( ) ) ,
415+ runtime,
416+ ) )
417+ } )
413418 . clone ( )
414419 }
415420
@@ -515,6 +520,7 @@ impl TestEnvironment {
515520 }
516521}
517522
523+ #[ async_trait]
518524impl Context for TestEnvironment {
519525 fn config ( & self ) -> Result < Arc < Config > > {
520526 Ok ( TestEnvironment :: config ( self ) )
@@ -528,8 +534,8 @@ impl Context for TestEnvironment {
528534 Ok ( TestEnvironment :: storage ( self ) )
529535 }
530536
531- fn async_storage ( & self ) -> Result < Arc < AsyncStorage > > {
532- Ok ( TestEnvironment :: async_storage ( self ) )
537+ async fn async_storage ( & self ) -> Result < Arc < AsyncStorage > > {
538+ Ok ( TestEnvironment :: async_storage ( self ) . await )
533539 }
534540
535541 fn cdn ( & self ) -> Result < Arc < CdnBackend > > {
0 commit comments