@@ -9,9 +9,10 @@ use crate::storage::{AsyncStorage, Storage, StorageKind};
9
9
use crate :: web:: { build_axum_app, cache, page:: TemplateData } ;
10
10
use crate :: { BuildQueue , Config , Context , Index , InstanceMetrics , RegistryApi , ServiceMetrics } ;
11
11
use anyhow:: Context as _;
12
+ use axum:: async_trait;
12
13
use fn_error_context:: context;
13
14
use futures_util:: FutureExt ;
14
- use once_cell:: unsync :: OnceCell ;
15
+ use once_cell:: sync :: OnceCell ;
15
16
use postgres:: Client as Connection ;
16
17
use reqwest:: {
17
18
blocking:: { Client , ClientBuilder , RequestBuilder , Response } ,
@@ -263,7 +264,7 @@ pub(crate) struct TestEnvironment {
263
264
config : OnceCell < Arc < Config > > ,
264
265
db : tokio:: sync:: OnceCell < TestDatabase > ,
265
266
storage : OnceCell < Arc < Storage > > ,
266
- async_storage : OnceCell < Arc < AsyncStorage > > ,
267
+ async_storage : tokio :: sync :: OnceCell < Arc < AsyncStorage > > ,
267
268
cdn : OnceCell < Arc < CdnBackend > > ,
268
269
index : OnceCell < Arc < Index > > ,
269
270
registry_api : OnceCell < Arc < RegistryApi > > ,
@@ -298,7 +299,7 @@ impl TestEnvironment {
298
299
config : OnceCell :: new ( ) ,
299
300
db : tokio:: sync:: OnceCell :: new ( ) ,
300
301
storage : OnceCell :: new ( ) ,
301
- async_storage : OnceCell :: new ( ) ,
302
+ async_storage : tokio :: sync :: OnceCell :: new ( ) ,
302
303
cdn : OnceCell :: new ( ) ,
303
304
index : OnceCell :: new ( ) ,
304
305
registry_api : OnceCell :: new ( ) ,
@@ -391,25 +392,29 @@ impl TestEnvironment {
391
392
. clone ( )
392
393
}
393
394
394
- pub ( crate ) fn async_storage ( & self ) -> Arc < AsyncStorage > {
395
+ pub ( crate ) async fn async_storage ( & self ) -> Arc < AsyncStorage > {
395
396
self . async_storage
396
- . get_or_init ( || {
397
+ . get_or_init ( || async {
398
+ let db = self . async_db ( ) . await ;
397
399
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
404
402
. expect ( "failed to initialize the async storage" ) ,
405
403
)
406
404
} )
405
+ . await
407
406
. clone ( )
408
407
}
409
408
410
409
pub ( crate ) fn storage ( & self ) -> Arc < Storage > {
410
+ let runtime = self . runtime ( ) ;
411
411
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
+ } )
413
418
. clone ( )
414
419
}
415
420
@@ -515,6 +520,7 @@ impl TestEnvironment {
515
520
}
516
521
}
517
522
523
+ #[ async_trait]
518
524
impl Context for TestEnvironment {
519
525
fn config ( & self ) -> Result < Arc < Config > > {
520
526
Ok ( TestEnvironment :: config ( self ) )
@@ -528,8 +534,8 @@ impl Context for TestEnvironment {
528
534
Ok ( TestEnvironment :: storage ( self ) )
529
535
}
530
536
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 )
533
539
}
534
540
535
541
fn cdn ( & self ) -> Result < Arc < CdnBackend > > {
0 commit comments