Skip to content

Commit

Permalink
Merge pull request #1069 from nolar/close-client-sessions
Browse files Browse the repository at this point in the history
Close the client sessions before the test server to avoid keep-alive timeout waiting (in CI)
  • Loading branch information
nolar authored Oct 9, 2023
2 parents b35ab78 + d6dffdf commit c6ef526
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
16 changes: 16 additions & 0 deletions tests/apis/test_api_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ async def serve_slowly():

assert 0.1 < timer.seconds < 0.2

# Let the server request finish and release all resources (tasks).
# TODO: Remove when fixed: https://github.com/aio-libs/aiohttp/issues/7551
await asyncio.sleep(1.0)


@pytest.mark.parametrize('fn, method', [
(get, 'get'),
Expand All @@ -182,6 +186,10 @@ async def serve_slowly():

assert 0.1 < timer.seconds < 0.2

# Let the server request finish and release all resources (tasks).
# TODO: Remove when fixed: https://github.com/aio-libs/aiohttp/issues/7551
await asyncio.sleep(1.0)


@pytest.mark.parametrize('method', ['get']) # the only supported method at the moment
async def test_direct_timeout_in_streams(
Expand All @@ -204,6 +212,10 @@ async def serve_slowly():

assert 0.1 < timer.seconds < 0.2

# Let the server request finish and release all resources (tasks).
# TODO: Remove when fixed: https://github.com/aio-libs/aiohttp/issues/7551
await asyncio.sleep(1.0)


@pytest.mark.parametrize('method', ['get']) # the only supported method at the moment
async def test_settings_timeout_in_streams(
Expand All @@ -226,6 +238,10 @@ async def serve_slowly():

assert 0.1 < timer.seconds < 0.2

# Let the server request finish and release all resources (tasks).
# TODO: Remove when fixed: https://github.com/aio-libs/aiohttp/issues/7551
await asyncio.sleep(1.0)


@pytest.mark.parametrize('delay, expected', [
pytest.param(0.0, [], id='instant-none'),
Expand Down
37 changes: 29 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,34 @@ def clean_kubernetes_client():
kubernetes.client.configuration.Configuration.set_default(None)


# Aresponses/aiohttp must be closed strictly after the vault. See the docstring.
@pytest.fixture()
def fake_vault(mocker, hostname):
async def _fake_vault(mocker, hostname, aresponses):
"""
A hack around pytest's internal flaw in order to close the vault in the end.
We cannot keep both the ContextVar and vault closing in the same fixture.
Pytest runs every async setup and every async teardown in a separate task
(a separate ``run_until_complete()``). The `vault_var` remains invisible
to tests (with API calls) and even to the fixture's finalizing part.
Sync (global) context vars do work and propagate fine — hence 2 fixtures.
Without the proper vault finalization, the cached TCP sessions/connections
remain open, so the aresponses/aiohttp test server takes time before exiting
(15 seconds of keep-alive timeout by default).
"""
key = VaultKey('fixture')
info = ConnectionInfo(server=f'https://{hostname}')
vault = Vault({key: info})
mocker.patch.object(vault._ready, 'wait_for')
try:
yield vault
finally:
await vault.close()


@pytest.fixture()
def fake_vault(_fake_vault):
"""
Provide a freshly created and populated authentication vault for every test.
Expand All @@ -424,15 +450,10 @@ def fake_vault(mocker, hostname):
"""
from kopf._cogs.clients import auth

key = VaultKey('fixture')
info = ConnectionInfo(server=f'https://{hostname}')
vault = Vault({key: info})
token = auth.vault_var.set(vault)
mocker.patch.object(vault._ready, 'wait_for')
token = auth.vault_var.set(_fake_vault)
try:
yield vault
yield _fake_vault
finally:
# await vault.close() # TODO: but it runs in a different loop, w/ wrong contextvar.
auth.vault_var.reset(token)

#
Expand Down

0 comments on commit c6ef526

Please sign in to comment.