Skip to content

Commit

Permalink
Convert async tests to async/await syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jul 28, 2018
1 parent 15d133e commit 0da84c9
Showing 1 changed file with 76 additions and 101 deletions.
177 changes: 76 additions & 101 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,27 @@ def _test_repr_or_str(self, fn, expect_id):
id_is_present = hex(id(q)) in fn(q)
self.assertEqual(expect_id, id_is_present)

@asyncio.coroutine
def add_getter():
async def add_getter():
_q = janus.Queue(loop=self.loop)
q = _q.async_q
# Start a task that waits to get.
asyncio.Task(q.get(), loop=self.loop)
self.loop.create_task(q.get())
# Let it start waiting.
yield from asyncio.sleep(0.1, loop=self.loop)
await asyncio.sleep(0.1)
self.assertTrue('_getters[1]' in fn(q))
# resume q.get coroutine to finish generator
q.put_nowait(0)

self.loop.run_until_complete(add_getter())

@asyncio.coroutine
def add_putter():
async def add_putter():
_q = janus.Queue(maxsize=1, loop=self.loop)
q = _q.async_q
q.put_nowait(1)
# Start a task that waits to put.
asyncio.Task(q.put(2), loop=self.loop)
self.loop.create_task(q.put(2))
# Let it start waiting.
yield from asyncio.sleep(0.1, loop=self.loop)
await asyncio.sleep(0.1)
self.assertTrue('_putters[1]' in fn(q))
# resume q.put coroutine to finish generator
q.get_nowait()
Expand Down Expand Up @@ -119,28 +117,26 @@ def test_maxsize(self):
self.assertEqual(2, q.maxsize)
have_been_put = []

fut = asyncio.Future(loop=self.loop)
fut = self.loop.create_future()

@asyncio.coroutine
def putter():
async def putter():
for i in range(3):
yield from q.put(i)
await q.put(i)
have_been_put.append(i)
if i == q.maxsize - 1:
fut.set_result(None)
return True

@asyncio.coroutine
def test():
t = asyncio.Task(putter(), loop=self.loop)
yield from fut
async def test():
t = self.loop.create_task(putter())
await fut

# The putter is blocked after putting two items.
self.assertEqual([0, 1], have_been_put)
self.assertEqual(0, q.get_nowait())

# Let the putter resume and put last item.
yield from t
await t
self.assertEqual([0, 1, 2], have_been_put)
self.assertEqual(1, q.get_nowait())
self.assertEqual(2, q.get_nowait())
Expand All @@ -158,9 +154,8 @@ def test_blocking_get(self):
q = _q.async_q
q.put_nowait(1)

@asyncio.coroutine
def queue_get():
return (yield from q.get())
async def queue_get():
return (await q.get())

res = self.loop.run_until_complete(queue_get())
self.assertEqual(1, res)
Expand All @@ -174,12 +169,11 @@ def test_get_with_putters(self):
q = _q.async_q
q.put_nowait(1)

fut = asyncio.Future(loop=self.loop)
fut = self.loop.create_future()

@asyncio.coroutine
def put():
async def put():
t = janus.ensure_future(q.put(2), loop=self.loop)
yield from asyncio.sleep(0.01, loop=self.loop)
await asyncio.sleep(0.01)
fut.set_result(None)
return t

Expand All @@ -201,21 +195,19 @@ def test_blocking_get_wait(self):
started = asyncio.Event(loop=self.loop)
finished = False

@asyncio.coroutine
def queue_get():
async def queue_get():
nonlocal finished
started.set()
res = yield from q.get()
res = await q.get()
finished = True
return res

@asyncio.coroutine
def queue_put():
async def queue_put():
self.loop.call_later(0.01, q.put_nowait, 1)
queue_get_task = asyncio.Task(queue_get(), loop=self.loop)
yield from started.wait()
queue_get_task = self.loop.create_task(queue_get())
await started.wait()
self.assertFalse(finished)
res = yield from queue_get_task
res = await queue_get_task
self.assertTrue(finished)
return res

Expand Down Expand Up @@ -247,18 +239,14 @@ def test_get_cancelled(self):
_q = janus.Queue(loop=self.loop)
q = _q.async_q

@asyncio.coroutine
def queue_get():
return (yield from asyncio.wait_for(q.get(), 0.051,
loop=self.loop))
async def queue_get():
return await asyncio.wait_for(q.get(), 0.051)

@asyncio.coroutine
def test():
get_task = asyncio.Task(queue_get(), loop=self.loop)
yield from asyncio.sleep(0.01,
loop=self.loop) # let the task start
async def test():
get_task = self.loop.create_task(queue_get())
await asyncio.sleep(0.01) # let the task start
q.put_nowait(1)
return (yield from get_task)
return await get_task

self.assertEqual(1, self.loop.run_until_complete(test()))

Expand All @@ -270,18 +258,17 @@ def test_get_cancelled_race(self):
_q = janus.Queue(loop=self.loop)
q = _q.async_q

f1 = asyncio.Future(loop=self.loop)
f1 = self.loop.create_future()

@asyncio.coroutine
def g1():
async def g1():
f1.set_result(None)
yield from q.get()
await q.get()

t1 = asyncio.Task(g1(), loop=self.loop)
t2 = asyncio.Task(q.get(), loop=self.loop)
t1 = self.loop.create_task(g1())
t2 = self.loop.create_task(q.get())

self.loop.run_until_complete(f1)
self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
self.loop.run_until_complete(asyncio.sleep(0.01))
t1.cancel()

with self.assertRaises(asyncio.CancelledError):
Expand All @@ -300,8 +287,8 @@ def test_get_with_waiting_putters(self):
_q = janus.Queue(loop=self.loop, maxsize=1)
q = _q.async_q

asyncio.Task(q.put('a'), loop=self.loop)
asyncio.Task(q.put('b'), loop=self.loop)
self.loop.create_task(q.put('a'))
self.loop.create_task(q.put('b'))

self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))

Expand All @@ -318,10 +305,9 @@ def test_blocking_put(self):
_q = janus.Queue(loop=self.loop)
q = _q.async_q

@asyncio.coroutine
def queue_put():
async def queue_put():
# No maxsize, won't block.
yield from q.put(1)
await q.put(1)

self.loop.run_until_complete(queue_put())

Expand All @@ -335,21 +321,19 @@ def test_blocking_put_wait(self):
started = asyncio.Event(loop=self.loop)
finished = False

@asyncio.coroutine
def queue_put():
async def queue_put():
nonlocal finished
started.set()
yield from q.put(1)
yield from q.put(2)
await q.put(1)
await q.put(2)
finished = True

@asyncio.coroutine
def queue_get():
async def queue_get():
self.loop.call_later(0.01, q.get_nowait)
queue_put_task = asyncio.Task(queue_put(), loop=self.loop)
yield from started.wait()
queue_put_task = self.loop.create_task(queue_put())
await started.wait()
self.assertFalse(finished)
yield from queue_put_task
await queue_put_task
self.assertTrue(finished)

self.loop.run_until_complete(queue_get())
Expand Down Expand Up @@ -392,10 +376,9 @@ def test_float_maxsize(self):
_q = janus.Queue(maxsize=1.3, loop=self.loop)
q = _q.async_q

@asyncio.coroutine
def queue_put():
yield from q.put(1)
yield from q.put(2)
async def queue_put():
await q.put(1)
await q.put(2)
self.assertTrue(q.full())

self.loop.run_until_complete(queue_put())
Expand All @@ -408,16 +391,14 @@ def test_put_cancelled(self):
_q = janus.Queue(loop=self.loop)
q = _q.async_q

@asyncio.coroutine
def queue_put():
yield from q.put(1)
async def queue_put():
await q.put(1)
return True

@asyncio.coroutine
def test():
return (yield from q.get())
async def test():
return (await q.get())

t = asyncio.Task(queue_put(), loop=self.loop)
t = self.loop.create_task(queue_put())
self.assertEqual(1, self.loop.run_until_complete(test()))
self.assertTrue(t.done())
self.assertTrue(t.result())
Expand All @@ -430,9 +411,9 @@ def test_put_cancelled_race(self):
_q = janus.Queue(loop=self.loop, maxsize=1)
q = _q.async_q

put_a = asyncio.Task(q.put('a'), loop=self.loop)
put_b = asyncio.Task(q.put('b'), loop=self.loop)
put_c = asyncio.Task(q.put('X'), loop=self.loop)
put_a = self.loop.create_task(q.put('a'))
put_b = self.loop.create_task(q.put('b'))
put_c = self.loop.create_task(q.put('X'))

self.loop.run_until_complete(put_a)
self.assertFalse(put_b.done())
Expand All @@ -442,11 +423,10 @@ def test_put_cancelled_race(self):
with self.assertRaises(asyncio.CancelledError):
self.loop.run_until_complete(put_c)

@asyncio.coroutine
def go():
a = yield from q.get()
async def go():
a = await q.get()
self.assertEqual(a, 'a')
b = yield from q.get()
b = await q.get()
self.assertEqual(b, 'b')
self.assertTrue(put_b.done())

Expand All @@ -457,21 +437,19 @@ def go():
self.loop.run_until_complete(_q.wait_closed())

def test_put_with_waiting_getters(self):
fut = asyncio.Future(loop=self.loop)
fut = self.loop.create_future()

@asyncio.coroutine
def go():
async def go():
fut.set_result(None)
ret = yield from q.get()
ret = await q.get()
return ret

@asyncio.coroutine
def put():
yield from q.put('a')
async def put():
await q.put('a')

_q = janus.Queue(loop=self.loop)
q = _q.async_q
t = asyncio.Task(go(), loop=self.loop)
t = self.loop.create_task(go())
self.loop.run_until_complete(fut)
self.loop.run_until_complete(put())
self.assertEqual(self.loop.run_until_complete(t), 'a')
Expand Down Expand Up @@ -536,21 +514,19 @@ def test_task_done(self):
# Join the queue and assert all items have been processed.
running = True

@asyncio.coroutine
def worker():
async def worker():
nonlocal accumulator

while running:
item = yield from q.get()
item = await q.get()
accumulator += item
q.task_done()

@asyncio.coroutine
def test():
tasks = [asyncio.Task(worker(),
loop=self.loop) for index in range(2)]
async def test():
tasks = [self.loop.create_task(worker())
for index in range(2)]

yield from q.join()
await q.join()
return tasks

tasks = self.loop.run_until_complete(test())
Expand All @@ -573,10 +549,9 @@ def test_join_empty_queue(self):
# Test that a queue join()s successfully, and before anything else
# (done twice for insurance).

@asyncio.coroutine
def join():
yield from q.join()
yield from q.join()
async def join():
await q.join()
await q.join()

self.loop.run_until_complete(join())

Expand Down

0 comments on commit 0da84c9

Please sign in to comment.