Skip to content

Commit

Permalink
Add cleanup to be called when future is garbage collected
Browse files Browse the repository at this point in the history
  • Loading branch information
xdrop committed Aug 14, 2024
1 parent 8be7100 commit 5af2166
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bravado/http_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -285,6 +290,11 @@ def cancel(self):
# type: () -> None
return self.future.cancel()

def __del__(self):
# type: () -> None
# Call cleanup to avoid resource leaks
self.future.cleanup()

@reraise_errors
def _get_incoming_response(self, timeout=None):
# type: (typing.Optional[float]) -> IncomingResponse
Expand Down
10 changes: 10 additions & 0 deletions tests/http_future/HttpFuture/cleanup_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
from unittest 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())
del http_future
assert mock_future_adapter.cleanup.call_count == 1

0 comments on commit 5af2166

Please sign in to comment.