-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Python 3.5 “async for” compatibility
Instead of while True: msg = await dataqueue.read() ... do: async for msg in dataqueue: ...
- Loading branch information
1 parent
230bdf3
commit 29fa81b
Showing
3 changed files
with
154 additions
and
2 deletions.
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,5 @@ | ||
""" | ||
Python 3.5 test module, testing new native async stuff. | ||
This __init_.py file allows files in here to be called the same as other test files. | ||
""" |
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,72 @@ | ||
import pytest | ||
|
||
from aiohttp import streams | ||
|
||
|
||
DATA = b'line1\nline2\nline3\n' | ||
|
||
|
||
def chunkify(seq, n): | ||
for i in range(0, len(seq), n): | ||
yield seq[i:i+n] | ||
|
||
|
||
def create_stream(loop): | ||
stream = streams.StreamReader(loop=loop) | ||
stream.feed_data(DATA) | ||
stream.feed_eof() | ||
return stream | ||
|
||
|
||
@pytest.mark.run_loop | ||
async def test_stream_reader_lines(loop): | ||
line_iter = iter(DATA.splitlines(keepends=True)) | ||
async for line in create_stream(loop): | ||
assert line == next(line_iter) | ||
pytest.raises(StopIteration, next, line_iter) | ||
|
||
|
||
@pytest.mark.run_loop | ||
async def test_stream_reader_chunks_complete(loop): | ||
"""Tests if chunked iteration works if the chunking works out | ||
(i.e. the data is divisible by the chunk size) | ||
""" | ||
chunk_iter = chunkify(DATA, 9) | ||
async for line in create_stream(loop).iter_chunked(9): | ||
assert line == next(chunk_iter) | ||
pytest.raises(StopIteration, next, chunk_iter) | ||
|
||
|
||
@pytest.mark.run_loop | ||
async def test_stream_reader_chunks_incomplete(loop): | ||
"""Tests if chunked iteration works if the last chunk is incomplete""" | ||
chunk_iter = chunkify(DATA, 8) | ||
async for line in create_stream(loop).iter_chunked(8): | ||
assert line == next(chunk_iter) | ||
pytest.raises(StopIteration, next, chunk_iter) | ||
|
||
|
||
@pytest.mark.run_loop | ||
async def test_data_queue_empty(loop): | ||
"""Tests that async looping yields nothing if nothing is there""" | ||
buffer = streams.DataQueue(loop=loop) | ||
buffer.feed_eof() | ||
|
||
async for _ in buffer: | ||
assert False | ||
|
||
|
||
@pytest.mark.run_loop | ||
async def test_data_queue_items(loop): | ||
"""Tests that async looping yields objects identically""" | ||
buffer = streams.DataQueue(loop=loop) | ||
|
||
items = [object(), object()] | ||
buffer.feed_data(items[0], 1) | ||
buffer.feed_data(items[1], 1) | ||
buffer.feed_eof() | ||
|
||
item_iter = iter(items) | ||
async for item in buffer: | ||
assert item is next(item_iter) | ||
pytest.raises(StopIteration, next, item_iter) |