diff --git a/aries_cloudagent/indy/sdk/profile.py b/aries_cloudagent/indy/sdk/profile.py index d8151a5050..525e52917b 100644 --- a/aries_cloudagent/indy/sdk/profile.py +++ b/aries_cloudagent/indy/sdk/profile.py @@ -128,9 +128,15 @@ def _make_finalizer(self, opened: IndyOpenWallet) -> finalize: See docs for weakref.finalize for more details on behavior of finalizers. """ + async def _closer(opened: IndyOpenWallet): + try: + await opened.close() + except Exception: + LOGGER.exception("Failed to close wallet from finalizer") + def _finalize(opened: IndyOpenWallet): LOGGER.debug("Profile finalizer called; closing wallet") - asyncio.get_event_loop().create_task(opened.close()) + asyncio.get_event_loop().create_task(_closer(opened)) return finalize(self, _finalize, opened) diff --git a/aries_cloudagent/indy/sdk/tests/test_issuer.py b/aries_cloudagent/indy/sdk/tests/test_issuer.py index 0378044314..a1b76509a0 100644 --- a/aries_cloudagent/indy/sdk/tests/test_issuer.py +++ b/aries_cloudagent/indy/sdk/tests/test_issuer.py @@ -51,7 +51,8 @@ async def setUp(self): "name": "test-wallet", } ).create_wallet() - self.profile = IndySdkProfile(self.wallet, self.context) + with async_mock.patch.object(IndySdkProfile, "_make_finalizer"): + self.profile = IndySdkProfile(self.wallet, self.context) self.issuer = test_module.IndySdkIssuer(self.profile) async def tearDown(self): diff --git a/aries_cloudagent/indy/sdk/tests/test_profile.py b/aries_cloudagent/indy/sdk/tests/test_profile.py index 6bd97474e6..8047d4cea6 100644 --- a/aries_cloudagent/indy/sdk/tests/test_profile.py +++ b/aries_cloudagent/indy/sdk/tests/test_profile.py @@ -1,35 +1,42 @@ +import asyncio import logging -import pytest from asynctest import mock as async_mock +import pytest from ....config.injection_context import InjectionContext from ....core.error import ProfileError from ....ledger.indy import IndySdkLedgerPool - from ..profile import IndySdkProfile -from ..wallet_setup import IndyWalletConfig, IndyOpenWallet +from ..wallet_setup import IndyOpenWallet, IndyWalletConfig @pytest.fixture async def open_wallet(): - yield IndyOpenWallet( + opened = IndyOpenWallet( config=IndyWalletConfig({"name": "test-profile"}), created=True, handle=1, master_secret_id="master-secret", ) + with async_mock.patch.object(opened, "close", async_mock.CoroutineMock()): + yield opened @pytest.fixture() async def profile(open_wallet): context = InjectionContext() context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name")) - yield IndySdkProfile(open_wallet, context) + profile = IndySdkProfile(open_wallet, context) + + yield profile + + # Trigger finalizer before event loop fixture is closed + profile._finalizer() @pytest.mark.asyncio -async def test_properties(profile): +async def test_properties(profile: IndySdkProfile): assert profile.name == "test-profile" assert profile.backend == "indy" assert profile.wallet and profile.wallet.handle == 1 diff --git a/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py b/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py index bf876a7a51..14c75c514b 100644 --- a/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py +++ b/aries_cloudagent/ledger/multiple_ledger/tests/test_manager_provider.py @@ -66,15 +66,16 @@ async def test_provide_invalid_manager(self): @pytest.mark.indy async def test_provide_indy_manager(self): context = InjectionContext() - profile = IndySdkProfile( - IndyOpenWallet( - config=IndyWalletConfig({"name": "test-profile"}), - created=True, - handle=1, - master_secret_id="master-secret", - ), - context, - ) + with async_mock.patch.object(IndySdkProfile, "_make_finalizer"): + profile = IndySdkProfile( + IndyOpenWallet( + config=IndyWalletConfig({"name": "test-profile"}), + created=True, + handle=1, + master_secret_id="master-secret", + ), + context, + ) context.injector.bind_instance( BaseLedger, IndySdkLedger(IndySdkLedgerPool("name"), profile) ) diff --git a/aries_cloudagent/ledger/tests/test_indy.py b/aries_cloudagent/ledger/tests/test_indy.py index c558e1c332..effa67cac2 100644 --- a/aries_cloudagent/ledger/tests/test_indy.py +++ b/aries_cloudagent/ledger/tests/test_indy.py @@ -76,10 +76,11 @@ async def setUp(self): self.test_verkey = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" context = InjectionContext() context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name")) - self.profile = IndySdkProfile( - async_mock.CoroutineMock(), - context, - ) + with async_mock.patch.object(IndySdkProfile, "_make_finalizer"): + self.profile = IndySdkProfile( + async_mock.CoroutineMock(), + context, + ) self.session = await self.profile.session() @async_mock.patch("indy.pool.create_pool_ledger_config") diff --git a/aries_cloudagent/ledger/tests/test_routes.py b/aries_cloudagent/ledger/tests/test_routes.py index 1310156e45..e3df880112 100644 --- a/aries_cloudagent/ledger/tests/test_routes.py +++ b/aries_cloudagent/ledger/tests/test_routes.py @@ -1,7 +1,6 @@ from asynctest import mock as async_mock, TestCase as AsyncTestCase from typing import Tuple -from ...admin.request_context import AdminRequestContext from ...core.in_memory import InMemoryProfile from ...ledger.base import BaseLedger from ...ledger.endpoint_type import EndpointType @@ -10,7 +9,6 @@ ) from ...ledger.multiple_ledger.base_manager import ( BaseMultipleLedgerManager, - MultipleLedgerManagerError, ) from ...multitenant.base import BaseMultitenantManager from ...multitenant.manager import MultitenantManager diff --git a/aries_cloudagent/storage/tests/test_indy_storage.py b/aries_cloudagent/storage/tests/test_indy_storage.py index 1bb9a17b84..ff1092cf61 100644 --- a/aries_cloudagent/storage/tests/test_indy_storage.py +++ b/aries_cloudagent/storage/tests/test_indy_storage.py @@ -12,7 +12,7 @@ from asynctest import mock as async_mock from ...config.injection_context import InjectionContext -from ...indy.sdk.profile import IndySdkProfileManager +from ...indy.sdk.profile import IndySdkProfileManager, IndySdkProfile from ...storage.base import BaseStorage from ...storage.error import StorageError, StorageSearchError from ...storage.indy import IndySdkStorage @@ -28,16 +28,17 @@ async def make_profile(): key = await IndySdkWallet.generate_wallet_key() context = InjectionContext() context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name")) - return await IndySdkProfileManager().provision( - context, - { - "auto_recreate": True, - "auto_remove": True, - "name": "test-wallet", - "key": key, - "key_derivation_method": "RAW", # much slower tests with argon-hashed keys - }, - ) + with async_mock.patch.object(IndySdkProfile, "_make_finalizer"): + return await IndySdkProfileManager().provision( + context, + { + "auto_recreate": True, + "auto_remove": True, + "name": "test-wallet", + "key": key, + "key_derivation_method": "RAW", # much slower tests with argon-hashed keys + }, + ) @pytest.fixture() @@ -75,7 +76,9 @@ async def test_record(self): indy.wallet, "close_wallet", async_mock.CoroutineMock() ) as mock_close, async_mock.patch.object( indy.wallet, "delete_wallet", async_mock.CoroutineMock() - ) as mock_delete: + ) as mock_delete, async_mock.patch.object( + IndySdkProfile, "_make_finalizer" + ): config = { "auto_recreate": True, "auto_remove": True, @@ -244,7 +247,9 @@ async def test_storage_search_x(self): indy.wallet, "close_wallet", async_mock.CoroutineMock() ) as mock_close, async_mock.patch.object( indy.wallet, "delete_wallet", async_mock.CoroutineMock() - ) as mock_delete: + ) as mock_delete, async_mock.patch.object( + IndySdkProfile, "_make_finalizer" + ): context = InjectionContext() context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name")) fake_profile = await IndySdkProfileManager().provision( @@ -322,7 +327,9 @@ async def test_storage_del_close(self): indy.wallet, "close_wallet", async_mock.CoroutineMock() ) as mock_close, async_mock.patch.object( indy.wallet, "delete_wallet", async_mock.CoroutineMock() - ) as mock_delete: + ) as mock_delete, async_mock.patch.object( + IndySdkProfile, "_make_finalizer" + ): context = InjectionContext() context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name")) fake_profile = await IndySdkProfileManager().provision( diff --git a/aries_cloudagent/storage/vc_holder/tests/test_indy_vc_holder.py b/aries_cloudagent/storage/vc_holder/tests/test_indy_vc_holder.py index 401b0c8750..fa092511b4 100644 --- a/aries_cloudagent/storage/vc_holder/tests/test_indy_vc_holder.py +++ b/aries_cloudagent/storage/vc_holder/tests/test_indy_vc_holder.py @@ -1,8 +1,9 @@ import pytest +from asynctest import mock as async_mock from ....config.injection_context import InjectionContext -from ....indy.sdk.profile import IndySdkProfileManager +from ....indy.sdk.profile import IndySdkProfileManager, IndySdkProfile from ....ledger.indy import IndySdkLedgerPool from ....wallet.indy import IndySdkWallet @@ -25,16 +26,18 @@ async def make_profile(): key = await IndySdkWallet.generate_wallet_key() context = InjectionContext() context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name")) - return await IndySdkProfileManager().provision( - context, - { - "auto_recreate": True, - "auto_remove": True, - "name": "test-wallet", - "key": key, - "key_derivation_method": "RAW", # much slower tests with argon-hashed keys - }, - ) + + with async_mock.patch.object(IndySdkProfile, "_make_finalizer"): + return await IndySdkProfileManager().provision( + context, + { + "auto_recreate": True, + "auto_remove": True, + "name": "test-wallet", + "key": key, + "key_derivation_method": "RAW", # much slower tests with argon-hashed keys + }, + ) @pytest.fixture() diff --git a/aries_cloudagent/wallet/tests/test_indy_wallet.py b/aries_cloudagent/wallet/tests/test_indy_wallet.py index aa3f7ee88d..e53551161e 100644 --- a/aries_cloudagent/wallet/tests/test_indy_wallet.py +++ b/aries_cloudagent/wallet/tests/test_indy_wallet.py @@ -1,5 +1,6 @@ import json import os +from typing import cast import indy.anoncreds import indy.crypto @@ -13,7 +14,7 @@ from ...config.injection_context import InjectionContext from ...core.error import ProfileError, ProfileDuplicateError, ProfileNotFoundError from ...indy.sdk import wallet_setup as test_setup_module -from ...indy.sdk.profile import IndySdkProfileManager +from ...indy.sdk.profile import IndySdkProfile, IndySdkProfileManager from ...indy.sdk.wallet_setup import IndyWalletConfig from ...ledger.endpoint_type import EndpointType from ...wallet.key_type import KeyType @@ -40,16 +41,20 @@ async def wallet(): key = await IndySdkWallet.generate_wallet_key() context = InjectionContext() context.injector.bind_instance(IndySdkLedgerPool, IndySdkLedgerPool("name")) - profile = await IndySdkProfileManager().provision( - context, - { - "auto_recreate": True, - "auto_remove": True, - "name": "test-wallet", - "key": key, - "key_derivation_method": "RAW", # much slower tests with argon-hashed keys - }, - ) + with async_mock.patch.object(IndySdkProfile, "_make_finalizer"): + profile = cast( + IndySdkProfile, + await IndySdkProfileManager().provision( + context, + { + "auto_recreate": True, + "auto_remove": True, + "name": "test-wallet", + "key": key, + "key_derivation_method": "RAW", # much slower tests with argon-hashed keys + }, + ), + ) async with profile.session() as session: yield session.inject(BaseWallet) await profile.close()