| 1 |
import sys |
| 2 |
|
| 3 |
import encodings.idna |
| 4 |
import asyncio |
| 5 |
|
| 6 |
def test_async_gil_issue(): |
| 7 |
async def f(): |
| 8 |
reader, writer = await asyncio.open_connection('example.com', 80) |
| 9 |
writer.close() |
| 10 |
|
| 11 |
loop = asyncio.get_event_loop() |
| 12 |
loop.run_until_complete(f()) |
| 13 |
|
| 14 |
def test_async_for(): |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import asyncio |
| 19 |
|
| 20 |
class AsyncIter: |
| 21 |
def __init__(self): |
| 22 |
self._data = list(range(5)) |
| 23 |
self._index = 0 |
| 24 |
|
| 25 |
def __aiter__(self): |
| 26 |
return self |
| 27 |
|
| 28 |
async def __anext__(self): |
| 29 |
while self._index < 5: |
| 30 |
await asyncio.sleep(1) |
| 31 |
self._index += 1 |
| 32 |
return self._data[self._index-1] |
| 33 |
raise StopAsyncIteration |
| 34 |
|
| 35 |
class Corotest(object): |
| 36 |
def __init__(self): |
| 37 |
self.res = "-" |
| 38 |
|
| 39 |
async def do_loop(self): |
| 40 |
async for x in AsyncIter(): |
| 41 |
self.res += str(x) |
| 42 |
self.res += "-" |
| 43 |
|
| 44 |
cor = Corotest() |
| 45 |
loop = asyncio.get_event_loop() |
| 46 |
futures = [asyncio.ensure_future(cor.do_loop()), asyncio.ensure_future(cor.do_loop())] |
| 47 |
taskres = loop.run_until_complete(asyncio.wait(futures)) |
| 48 |
assert cor.res.count('0') == 2 |
| 49 |
assert cor.res.count('1') == 2 |
| 50 |
assert cor.res.count('2') == 2 |
| 51 |
assert cor.res.count('3') == 2 |
| 52 |
assert cor.res.count('4') == 2 |
| 53 |
assert cor.res.find("0") < cor.res.find("1") |
| 54 |
assert cor.res.find("1") < cor.res.find("2") |
| 55 |
assert cor.res.find("2") < cor.res.find("3") |
| 56 |
assert cor.res.find("3") < cor.res.find("4") |
| 57 |
assert isinstance(taskres, tuple) |
| 58 |
assert len(taskres) == 2 |
| 59 |
assert "result=None" in repr(taskres[0].pop()) |
| 60 |
assert "result=None" in repr(taskres[0].pop()) |
| 61 |
|
| 62 |
def test_asynchronous_context_managers(): |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
import encodings.idna |
| 67 |
import asyncio |
| 68 |
|
| 69 |
class Corotest(object): |
| 70 |
def __init__(self): |
| 71 |
self.res = "-" |
| 72 |
|
| 73 |
async def coro(self, name, lock): |
| 74 |
self.res += ' coro {}: waiting for lock -'.format(name) |
| 75 |
async with lock: |
| 76 |
self.res += ' coro {}: holding the lock -'.format(name) |
| 77 |
await asyncio.sleep(1) |
| 78 |
self.res += ' coro {}: releasing the lock -'.format(name) |
| 79 |
|
| 80 |
cor = Corotest() |
| 81 |
loop = asyncio.get_event_loop() |
| 82 |
lock = asyncio.Lock() |
| 83 |
coros = asyncio.gather(cor.coro(1, lock), cor.coro(2, lock)) |
| 84 |
try: |
| 85 |
loop.run_until_complete(coros) |
| 86 |
finally: |
| 87 |
loop.close() |
| 88 |
|
| 89 |
assert "coro 1: waiting for lock" in cor.res |
| 90 |
assert "coro 1: holding the lock" in cor.res |
| 91 |
assert "coro 1: releasing the lock" in cor.res |
| 92 |
assert "coro 2: waiting for lock" in cor.res |
| 93 |
assert "coro 2: holding the lock" in cor.res |
| 94 |
assert "coro 2: releasing the lock" in cor.res |
| 95 |
assert cor.res.find("coro 1: releasing the lock") < cor.res.find("coro 2: holding the lock") or \ |
| 96 |
cor.res.find("coro 2: releasing the lock") < cor.res.find("coro 1: holding the lock") |