-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close a connection if an unexpected exception occurs while sending a request #2828
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Close a connection if an unexpected exception occurs while sending a request |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -371,6 +371,7 @@ async def create_connection(req, traces=None): | |
# return self.transport, self.protocol | ||
return mock.Mock() | ||
session._connector._create_connection = create_connection | ||
session._connector._release = mock.Mock() | ||
|
||
with pytest.raises(aiohttp.ClientOSError) as ctx: | ||
await session.request('get', 'http://example.com') | ||
|
@@ -379,6 +380,42 @@ async def create_connection(req, traces=None): | |
assert e.strerror == err.strerror | ||
|
||
|
||
async def test_close_conn_on_error(create_session): | ||
class UnexpectedException(Exception): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your try/except block catches BaseExceptions, but here you test just for Exception. It's easy to change the code and break the behavior, but not the tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
pass | ||
|
||
err = UnexpectedException("permission error") | ||
req = mock.Mock() | ||
req_factory = mock.Mock(return_value=req) | ||
req.send = mock.Mock(side_effect=err) | ||
session = create_session(request_class=req_factory) | ||
|
||
connections = [] | ||
original_connect = session._connector.connect | ||
|
||
async def connect(req, traces=None): | ||
conn = await original_connect(req, traces=traces) | ||
connections.append(conn) | ||
return conn | ||
|
||
async def create_connection(req, traces=None): | ||
# return self.transport, self.protocol | ||
conn = mock.Mock() | ||
return conn | ||
|
||
session._connector.connect = connect | ||
session._connector._create_connection = create_connection | ||
session._connector._release = mock.Mock() | ||
|
||
with pytest.raises(UnexpectedException): | ||
await session.request('get', 'http://example.com') | ||
|
||
# normally called during garbage collection. triggers an exception | ||
# if the connection wasn't already closed | ||
for c in connections: | ||
c.__del__() | ||
|
||
|
||
async def test_cookie_jar_usage(loop, aiohttp_client): | ||
req_url = None | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we're don't the same thing twice. How about to regroup try/except a bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.