-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Timeout context manager #611
Merged
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
04bc1a0
timeout context manager
jettify ebdffc4
tests for timeout context manager
jettify 0e8ead0
drop __del__ method
jettify 73a8a71
update tests and handle None for timeout
jettify b4aded5
drop timeout=None and cancel api
jettify 950d778
update tests
jettify 619e812
drop raise_error argument
jettify bb24fe9
drop reference to task in __aexit__
jettify 2082a12
use with instead async with
jettify 8da5d4f
refactor and move tests from py35 directory
jettify bebe064
raise TimeoutError only if body was canceled and CancelledError was r…
jettify 71995c1
add docs and doc string
jettify File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import asyncio | ||
import time | ||
|
||
import pytest | ||
from aiohttp.helpers import Timeout | ||
|
||
|
||
def test_timeout(loop): | ||
canceled_raised = False | ||
|
||
async def long_running_task(): | ||
try: | ||
await asyncio.sleep(10, loop=loop) | ||
except asyncio.CancelledError: | ||
nonlocal canceled_raised | ||
canceled_raised = True | ||
raise | ||
|
||
async def run(): | ||
with pytest.raises(asyncio.TimeoutError): | ||
async with Timeout(0.01, loop=loop) as t: | ||
await long_running_task() | ||
assert t._loop is loop | ||
assert canceled_raised, 'CancelledError was not raised' | ||
|
||
loop.run_until_complete(run()) | ||
|
||
|
||
def test_timeout_finish_in_time(loop): | ||
async def long_running_task(): | ||
await asyncio.sleep(0.01, loop=loop) | ||
return 'done' | ||
|
||
async def run(): | ||
async with Timeout(0.1, loop=loop): | ||
resp = await long_running_task() | ||
assert resp == 'done' | ||
|
||
loop.run_until_complete(run()) | ||
|
||
|
||
def test_timeout_gloabal_loop(loop): | ||
asyncio.set_event_loop(loop) | ||
|
||
async def run(): | ||
async with Timeout(0.1) as t: | ||
await asyncio.sleep(0.01) | ||
assert t._loop is loop | ||
|
||
loop.run_until_complete(run()) | ||
|
||
|
||
def test_timeout_not_relevant_exception(loop): | ||
async def run(): | ||
with pytest.raises(KeyError): | ||
async with Timeout(0.1, loop=loop): | ||
raise KeyError | ||
|
||
loop.run_until_complete(run()) | ||
|
||
|
||
def test_timeout_blocking_loop(loop): | ||
async def long_running_task(): | ||
time.sleep(0.1) | ||
return 'done' | ||
|
||
async def run(): | ||
async with Timeout(0.01, loop=loop): | ||
result = await long_running_task() | ||
assert result == 'done' | ||
|
||
loop.run_until_complete(run()) | ||
|
||
|
||
def test_for_race_conditions(loop): | ||
async def run(): | ||
fut = asyncio.Future(loop=loop) | ||
loop.call_later(0.1, fut.set_result('done')) | ||
async with Timeout(0.2, loop=loop): | ||
resp = await fut | ||
assert resp == 'done' | ||
|
||
loop.run_until_complete(run()) | ||
|
||
|
||
def test_timeout_time(loop): | ||
async def go(): | ||
foo_running = None | ||
|
||
start = loop.time() | ||
with pytest.raises(asyncio.TimeoutError): | ||
async with Timeout(0.1, loop=loop): | ||
foo_running = True | ||
try: | ||
await asyncio.sleep(0.2, loop=loop) | ||
finally: | ||
foo_running = False | ||
|
||
assert abs(0.1 - (loop.time() - start)) < 0.01 | ||
assert not foo_running | ||
|
||
loop.run_until_complete(go()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add
self._task = None