Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/decibel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ def time_delta_ms(self, value: int) -> None:
self._time_delta_ms = value

async def close(self) -> None:
if self._owns_http_client:
await self._http_client.aclose()
try:
await self._aptos.close()
finally:
if self._owns_http_client:
await self._http_client.aclose()

async def __aenter__(self) -> BaseSDK:
return self
Expand Down
5 changes: 4 additions & 1 deletion src/decibel/read/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ async def close(self) -> None:
try:
await self.ws.close()
finally:
await self._http_client.aclose()
try:
await self._aptos.close()
finally:
await self._http_client.aclose()

async def __aenter__(self) -> DecibelReadDex:
return self
Expand Down
3 changes: 2 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def _module_response(status_code: int = 200, functions: Any = None) -> httpx.Res


def _make_sdk(config: Any, account: Any, opts: Any = None) -> BaseSDK:
with patch("decibel._base.AbiRegistry"), patch("decibel._base.RestClient"):
with patch("decibel._base.AbiRegistry"), patch("decibel._base.RestClient") as mock_rest:
mock_rest.return_value.close = AsyncMock()
sdk = BaseSDK(config=config, account=account, opts=opts)
return sdk

Expand Down
84 changes: 84 additions & 0 deletions tests/test_client_cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from typing import Any
from unittest.mock import AsyncMock, patch

import httpx
import pytest

from decibel._base import BaseSDK, BaseSDKOptions
from decibel.read import DecibelReadDex


@pytest.mark.parametrize("provided_client", [False, True])
@pytest.mark.parametrize("exit_mode", ["close", "context", "exception"])
async def test_base_closes_owned_clients(
test_config: Any, mock_account: Any, provided_client: bool, exit_mode: str
) -> None:
client = httpx.AsyncClient() if provided_client else None
sdk = BaseSDK(test_config, mock_account, BaseSDKOptions(http_client=client))
try:
if exit_mode == "close":
await sdk.close()
elif exit_mode == "context":
async with sdk:
pass
else:
with pytest.raises(ValueError, match="body failed"):
async with sdk:
raise ValueError("body failed")

assert sdk.aptos.client.is_closed
assert sdk._http_client.is_closed is not provided_client
finally:
await sdk.aptos.close()
await sdk._http_client.aclose()


@pytest.mark.parametrize("exit_mode", ["close", "context", "exception"])
async def test_reader_closes_owned_clients(test_config: Any, exit_mode: str) -> None:
sdk = DecibelReadDex(test_config)
try:
if exit_mode == "close":
await sdk.close()
elif exit_mode == "context":
async with sdk:
pass
else:
with pytest.raises(ValueError, match="body failed"):
async with sdk:
raise ValueError("body failed")

assert sdk._aptos.client.is_closed
assert sdk._http_client.is_closed
finally:
await sdk._aptos.close()
await sdk._http_client.aclose()


async def test_base_closes_http_client_when_aptos_close_fails(
test_config: Any, mock_account: Any
) -> None:
sdk = BaseSDK(test_config, mock_account)
try:
with patch.object(sdk.aptos, "close", AsyncMock(side_effect=ValueError("close failed"))):
with pytest.raises(ValueError, match="close failed"):
await sdk.close()
assert sdk._http_client.is_closed
finally:
await sdk.aptos.close()
await sdk._http_client.aclose()


@pytest.mark.parametrize("failure", ["websocket", "aptos"])
async def test_reader_finishes_cleanup_after_close_error(test_config: Any, failure: str) -> None:
sdk = DecibelReadDex(test_config)
target = sdk.ws if failure == "websocket" else sdk._aptos
try:
with patch.object(target, "close", AsyncMock(side_effect=ValueError("close failed"))):
with pytest.raises(ValueError, match="close failed"):
await sdk.close()
assert sdk._http_client.is_closed
if failure == "websocket":
assert sdk._aptos.client.is_closed
finally:
await sdk._aptos.close()
await sdk._http_client.aclose()