Skip to content

Commit 190f9af

Browse files
committed
Test new session behavior
1 parent aea685f commit 190f9af

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

nanokvm/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
217217
self._ws = None
218218

219219
# Close HTTP session
220-
if self._session is not None and self._auto_close_session:
221-
await self._session.close()
220+
if self._session is not None:
221+
if self._auto_close_session:
222+
await self._session.close()
223+
222224
self._session = None
223225

224226
@contextlib.asynccontextmanager

tests/test_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from aiohttp import ClientSession
12
from aioresponses import aioresponses
23
import pytest
34

@@ -79,3 +80,23 @@ async def test_client_context_manager() -> None:
7980

8081
# After exiting context, session should be closed
8182
assert client._session is None
83+
84+
85+
async def test_client_context_manager_external_session() -> None:
86+
"""Test that client properly deals with an external session."""
87+
async with ClientSession() as session:
88+
# Both clients connect with the same external session
89+
async with (
90+
NanoKVMClient("http://localhost:8888/api/", session=session) as client1,
91+
NanoKVMClient("http://localhost:8888/api/", session=session) as client2,
92+
):
93+
# Verify session is created
94+
assert client1._session is session
95+
assert client2._session is session
96+
97+
# Both exit but the session is still open
98+
assert not session.closed
99+
assert client1._session is None
100+
assert client2._session is None
101+
102+
assert session.closed

0 commit comments

Comments
 (0)