From db022e4bf93e34dc25d750e639c98a9969ea0150 Mon Sep 17 00:00:00 2001 From: Pan Pastos Date: Wed, 14 Aug 2024 09:54:24 -0700 Subject: [PATCH] Add cleanup to be called when future is garbage collected --- bravado/http_future.py | 9 +++++++++ tests/http_future/HttpFuture/cleanup_test.py | 10 ++++++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/http_future/HttpFuture/cleanup_test.py diff --git a/bravado/http_future.py b/bravado/http_future.py index 0dceca8..2a7fbdd 100644 --- a/bravado/http_future.py +++ b/bravado/http_future.py @@ -90,6 +90,11 @@ def _raise_connection_error(self, exception): # type: (BaseException) -> typing.NoReturn self._raise_error(BravadoConnectionError, 'ConnectionError', exception) + def cleanup(self): + # type: () -> None + """Perform any cleanup necessary to avoid resource leaks.""" + pass + def result(self, timeout=None): # type: (typing.Optional[float]) -> T """ @@ -285,6 +290,10 @@ def cancel(self): # type: () -> None return self.future.cancel() + def __del__(self): + # type: () -> None + self.future.cleanup() + @reraise_errors def _get_incoming_response(self, timeout=None): # type: (typing.Optional[float]) -> IncomingResponse diff --git a/tests/http_future/HttpFuture/cleanup_test.py b/tests/http_future/HttpFuture/cleanup_test.py new file mode 100644 index 0000000..0fe7dde --- /dev/null +++ b/tests/http_future/HttpFuture/cleanup_test.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +import mock + +from bravado.http_future import HttpFuture + + +def test_cleanup_on_gc(mock_future_adapter): + http_future = HttpFuture(future=mock_future_adapter, response_adapter=mock.Mock()) # type: HttpFuture[None] + del http_future + assert mock_future_adapter.cleanup.call_count == 1