diff --git a/requirements-test.txt b/requirements-test.txt --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,5 +1,4 @@ pytest < 7.0.0 # v7.0.0 removed _pytest.tmpdir.TempdirFactory, which is used by some of the pytest plugins we use -pytest-asyncio pytest-mock requests_mock[fixture] >= 1.9 requests_toolbelt diff --git a/swh/objstorage/backends/azure.py b/swh/objstorage/backends/azure.py --- a/swh/objstorage/backends/azure.py +++ b/swh/objstorage/backends/azure.py @@ -27,6 +27,7 @@ compute_hash, decompressors, ) +from swh.objstorage.utils import call_async def get_container_url( @@ -76,16 +77,6 @@ return f"https://{account_name}.blob.core.windows.net/{container_name}?{signature}" -def call_async(f, *args): - """Calls an async coroutine from a synchronous function.""" - loop = asyncio.new_event_loop() - try: - return loop.run_until_complete(f(*args)) - finally: - loop.run_until_complete(loop.shutdown_asyncgens()) - loop.close() - - class AzureCloudObjStorage(ObjStorage): """ObjStorage backend for Azure blob storage accounts. diff --git a/swh/objstorage/tests/test_objstorage_winery.py b/swh/objstorage/tests/test_objstorage_winery.py --- a/swh/objstorage/tests/test_objstorage_winery.py +++ b/swh/objstorage/tests/test_objstorage_winery.py @@ -20,6 +20,7 @@ Throttler, ) from swh.objstorage.factory import get_objstorage +from swh.objstorage.utils import call_async from .winery_benchmark import Bench, work from .winery_testing_helpers import PoolHelper, SharedBaseHelper @@ -207,8 +208,7 @@ assert work("ro", winery.args) == "ro" -@pytest.mark.asyncio -async def test_winery_bench_real(pytestconfig, postgresql, ceph_pool): +def test_winery_bench_real(pytestconfig, postgresql, ceph_pool): dsn = ( f"postgres://{postgresql.info.user}" f":@{postgresql.info.host}:{postgresql.info.port}" @@ -227,12 +227,11 @@ "throttle_read": pytestconfig.getoption("--winery-bench-throttle-read"), "throttle_write": pytestconfig.getoption("--winery-bench-throttle-write"), } - count = await Bench(kwargs).run() + count = call_async(Bench(kwargs).run) assert count > 0 -@pytest.mark.asyncio -async def test_winery_bench_fake(pytestconfig, mocker): +def test_winery_bench_fake(pytestconfig, mocker): kwargs = { "rw_workers": pytestconfig.getoption("--winery-bench-rw-workers"), "ro_workers": pytestconfig.getoption("--winery-bench-ro-workers"), @@ -244,7 +243,7 @@ return kind mocker.patch("swh.objstorage.tests.winery_benchmark.Worker.run", side_effect=run) - assert await Bench(kwargs).run() == kwargs["rw_workers"] + kwargs["ro_workers"] + assert call_async(Bench(kwargs).run) == kwargs["rw_workers"] + kwargs["ro_workers"] def test_winery_leaky_bucket_tick(mocker): diff --git a/swh/objstorage/utils.py b/swh/objstorage/utils.py new file mode 100644 --- /dev/null +++ b/swh/objstorage/utils.py @@ -0,0 +1,16 @@ +# Copyright (C) 2021 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import asyncio + + +def call_async(f, *args): + """Calls an async coroutine from a synchronous function.""" + loop = asyncio.new_event_loop() + try: + return loop.run_until_complete(f(*args)) + finally: + loop.run_until_complete(loop.shutdown_asyncgens()) + loop.close()