Fix bug #361: failure on async resource depends on other async resource

This commit is contained in:
Roman Mogylatov 2021-01-19 17:47:25 -05:00
parent 2cfc61aa37
commit 21cc6ffc1b
4 changed files with 2263 additions and 1417 deletions

View File

@ -7,6 +7,13 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_ follows `Semantic versioning`_
Development version
-------------------
- Fix a bug in ``Resource`` that cause failure when async resource depends on
another async resource.
See issue `#361 <https://github.com/ets-labs/python-dependency-injector/issues/361>`_.
Thanks `@kolypto <https://github.com/kolypto>`_ for the bug report.
4.10.1 4.10.1
------ ------
- Fix a Python 3.9 specific bug in ``wiring`` module: introspection doesn't work for - Fix a Python 3.9 specific bug in ``wiring`` module: introspection doesn't work for

File diff suppressed because it is too large Load Diff

View File

@ -2967,7 +2967,7 @@ cdef class Resource(Provider):
self.__kwargs_len, self.__kwargs_len,
) )
self.__initialized = True self.__initialized = True
return self._create_init_future(initializer.__anext__(), initializer.asend) return self._create_async_gen_init_future(initializer)
elif callable(self.__initializer): elif callable(self.__initializer):
self.__resource = __call( self.__resource = __call(
self.__initializer, self.__initializer,
@ -2995,6 +2995,18 @@ cdef class Resource(Provider):
return future return future
def _create_async_gen_init_future(self, initializer):
if inspect.isasyncgen(initializer):
return self._create_init_future(initializer.__anext__(), initializer.asend)
future = asyncio.Future()
create_initializer = asyncio.ensure_future(initializer)
create_initializer.add_done_callback(functools.partial(self._async_create_gen_callback, future))
self.__resource = future
return future
def _async_init_callback(self, initializer, shutdowner=None): def _async_init_callback(self, initializer, shutdowner=None):
try: try:
resource = initializer.result() resource = initializer.result()
@ -3005,6 +3017,14 @@ cdef class Resource(Provider):
self.__resource = resource self.__resource = resource
self.__shutdowner = shutdowner self.__shutdowner = shutdowner
def _async_create_gen_callback(self, future, initializer_future):
initializer = initializer_future.result()
init_future = self._create_init_future(initializer.__anext__(), initializer.asend)
init_future.add_done_callback(functools.partial(self._async_trigger_result, future))
def _async_trigger_result(self, future, future_result):
future.set_result(future_result.result())
def _create_shutdown_future(self, shutdown_future): def _create_shutdown_future(self, shutdown_future):
future = asyncio.Future() future = asyncio.Future()
shutdown_future = asyncio.ensure_future(shutdown_future) shutdown_future = asyncio.ensure_future(shutdown_future)

View File

@ -461,6 +461,43 @@ class AsyncResourceTest(AsyncTestCase):
self.assertFalse(provider.initialized) self.assertFalse(provider.initialized)
self.assertTrue(provider.is_async_mode_enabled()) self.assertTrue(provider.is_async_mode_enabled())
def test_init_with_dependency_to_other_resource(self):
# See: https://github.com/ets-labs/python-dependency-injector/issues/361
async def init_db_connection(db_url: str):
await asyncio.sleep(0.001)
yield {'connection': 'ok', 'url': db_url}
async def init_user_session(db):
await asyncio.sleep(0.001)
yield {'session': 'ok', 'db': db}
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
db_connection = providers.Resource(
init_db_connection,
db_url=config.db_url,
)
user_session = providers.Resource(
init_user_session,
db=db_connection
)
async def main():
container = Container(config={'db_url': 'postgres://...'})
try:
return await container.user_session()
finally:
await container.shutdown_resources()
result = self._run(main())
self.assertEqual(
result,
{'session': 'ok', 'db': {'connection': 'ok', 'url': 'postgres://...'}},
)
def test_init_and_shutdown_methods(self): def test_init_and_shutdown_methods(self):
async def _init(): async def _init():
await asyncio.sleep(0.001) await asyncio.sleep(0.001)