@@ -1008,35 +1008,41 @@ def _(celery_app: Celery) -> None: ...
10081008
10091009
10101010@pytest .fixture
1011- def app_server_with_worker_mode (
1011+ def app_server_factory_with_worker_mode (
10121012 app_environment : EnvVarsDict ,
10131013 monkeypatch : pytest .MonkeyPatch ,
1014- ) -> FastAPIAppServer :
1014+ ) -> Callable [[], FastAPIAppServer ] :
10151015 monkeypatch .setenv ("STORAGE_WORKER_MODE" , "true" )
10161016
1017- app_settings = ApplicationSettings . create_from_envs ()
1018- assert app_settings . STORAGE_WORKER_MODE is True
1017+ # Cache to ensure we create the app server only once per test
1018+ _app_server_cache : list [ FastAPIAppServer ] = []
10191019
1020- tracing_config = TracingConfig .create (
1021- tracing_settings = None , # disable tracing in tests
1022- service_name = "storage-worker" ,
1023- )
1024- return FastAPIAppServer (app = create_app (app_settings , tracing_config ))
1020+ def _app_server_factory () -> FastAPIAppServer :
1021+ if not _app_server_cache :
1022+ app_settings = ApplicationSettings .create_from_envs ()
1023+ assert app_settings .STORAGE_WORKER_MODE is True
1024+
1025+ tracing_config = TracingConfig .create (
1026+ tracing_settings = None , # disable tracing in tests
1027+ service_name = "storage-worker" ,
1028+ )
1029+ _app_server_cache .append (
1030+ FastAPIAppServer (app = create_app (app_settings , tracing_config ))
1031+ )
1032+ return _app_server_cache [0 ]
1033+
1034+ return _app_server_factory
10251035
10261036
10271037@pytest .fixture
10281038async def with_storage_celery_worker (
10291039 celery_app : Celery ,
1030- app_server_with_worker_mode : FastAPIAppServer ,
1040+ app_server_factory_with_worker_mode : Callable [[], FastAPIAppServer ] ,
10311041 register_test_tasks : Callable [[Celery ], None ],
10321042) -> AsyncIterator [TestWorkController ]:
10331043
1034- def _app_server_factory () -> FastAPIAppServer :
1035- return app_server_with_worker_mode
1036-
1037- # NOTE: explicitly connect the signals in tests
10381044 worker_init .connect (
1039- _worker_init_wrapper (celery_app , _app_server_factory ),
1045+ _worker_init_wrapper (celery_app , app_server_factory_with_worker_mode ),
10401046 weak = False ,
10411047 )
10421048 worker_shutdown .connect (_worker_shutdown_wrapper (celery_app ), weak = False )
@@ -1054,7 +1060,6 @@ def _app_server_factory() -> FastAPIAppServer:
10541060 ) as worker :
10551061 yield worker
10561062
1057- # Explicitly trigger shutdown signal for proper cleanup
10581063 _worker_shutdown_wrapper (celery_app )()
10591064
10601065
0 commit comments