Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tests/test_pipes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
import io
import os
import socket

from asyncio import test_utils
from uvloop import _testbase as tb


Expand Down Expand Up @@ -81,11 +81,11 @@ def connect():
self.loop.run_until_complete(connect())

os.write(wpipe, b'1')
test_utils.run_until(self.loop, lambda: proto.nbytes >= 1)
tb.run_until(self.loop, lambda: proto.nbytes >= 1)
self.assertEqual(1, proto.nbytes)

os.write(wpipe, b'2345')
test_utils.run_until(self.loop, lambda: proto.nbytes >= 5)
tb.run_until(self.loop, lambda: proto.nbytes >= 5)
self.assertEqual(['INITIAL', 'CONNECTED'], proto.state)
self.assertEqual(5, proto.nbytes)

Expand Down Expand Up @@ -114,11 +114,11 @@ def connect():
self.loop.run_until_complete(connect())

os.write(slave, b'1')
test_utils.run_until(self.loop, lambda: proto.nbytes)
tb.run_until(self.loop, lambda: proto.nbytes)
self.assertEqual(1, proto.nbytes)

os.write(slave, b'2345')
test_utils.run_until(self.loop, lambda: proto.nbytes >= 5)
tb.run_until(self.loop, lambda: proto.nbytes >= 5)
self.assertEqual(['INITIAL', 'CONNECTED'], proto.state)
self.assertEqual(5, proto.nbytes)

Expand Down Expand Up @@ -158,11 +158,11 @@ def reader(data):
data += chunk
return len(data)

test_utils.run_until(self.loop, lambda: reader(data) >= 1)
tb.run_until(self.loop, lambda: reader(data) >= 1)
self.assertEqual(b'1', data)

transport.write(b'2345')
test_utils.run_until(self.loop, lambda: reader(data) >= 5)
tb.run_until(self.loop, lambda: reader(data) >= 5)
self.assertEqual(b'12345', data)
self.assertEqual('CONNECTED', proto.state)

Expand All @@ -177,7 +177,7 @@ def reader(data):
self.assertEqual('CLOSED', proto.state)

def test_write_pipe_disconnect_on_close(self):
rsock, wsock = test_utils.socketpair()
rsock, wsock = socket.socketpair()
rsock.setblocking(False)

pipeobj = io.open(wsock.detach(), 'wb', 1024)
Expand Down Expand Up @@ -223,12 +223,12 @@ def reader(data):
data += chunk
return len(data)

test_utils.run_until(self.loop, lambda: reader(data) >= 1,
tb.run_until(self.loop, lambda: reader(data) >= 1,
timeout=10)
self.assertEqual(b'1', data)

transport.write(b'2345')
test_utils.run_until(self.loop, lambda: reader(data) >= 5,
tb.run_until(self.loop, lambda: reader(data) >= 5,
timeout=10)
self.assertEqual(b'12345', data)
self.assertEqual('CONNECTED', proto.state)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time
import unittest

from asyncio import test_utils
from uvloop import _testbase as tb


Expand Down Expand Up @@ -504,7 +503,7 @@ def cancel_make_transport():

# ignore the log:
# "Exception during subprocess creation, kill the subprocess"
with test_utils.disable_logger():
with tb.disable_logger():
self.loop.run_until_complete(cancel_make_transport())

def test_cancel_post_init(self):
Expand All @@ -522,9 +521,9 @@ def cancel_make_transport():

# ignore the log:
# "Exception during subprocess creation, kill the subprocess"
with test_utils.disable_logger():
with tb.disable_logger():
self.loop.run_until_complete(cancel_make_transport())
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)


class Test_UV_Process(_TestProcess, tb.UVTestCase):
Expand Down
85 changes: 16 additions & 69 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import asyncio

from asyncio import test_utils
from uvloop import _testbase as tb


Expand Down Expand Up @@ -30,59 +29,6 @@ def format_coroutine(qualname, state, src, source_traceback, generator=False):

class _TestTasks:

def test_task_repr(self):
self.loop.set_debug(False)

@asyncio.coroutine
def notmuch():
yield from []
return 'abc'

# test coroutine function
self.assertEqual(notmuch.__name__, 'notmuch')
self.assertEqual(notmuch.__qualname__,
'_TestTasks.test_task_repr.<locals>.notmuch')
self.assertEqual(notmuch.__module__, __name__)

filename, lineno = test_utils.get_function_source(notmuch)
src = "%s:%s" % (filename, lineno)

# test coroutine object
gen = notmuch()
coro_qualname = '_TestTasks.test_task_repr.<locals>.notmuch'
self.assertEqual(gen.__name__, 'notmuch')
self.assertEqual(gen.__qualname__, coro_qualname)

# test pending Task
t = asyncio.Task(gen, loop=self.loop)
t.add_done_callback(Dummy())

coro = format_coroutine(coro_qualname, 'running', src,
t._source_traceback, generator=True)
self.assertEqual(repr(t),
'<Task pending %s cb=[<Dummy>()]>' % coro)

# test canceling Task
t.cancel() # Does not take immediate effect!
self.assertEqual(repr(t),
'<Task cancelling %s cb=[<Dummy>()]>' % coro)

# test canceled Task
self.assertRaises(asyncio.CancelledError,
self.loop.run_until_complete, t)
coro = format_coroutine(coro_qualname, 'done', src,
t._source_traceback)
self.assertEqual(repr(t),
'<Task cancelled %s>' % coro)

# test finished Task
t = asyncio.Task(notmuch(), loop=self.loop)
self.loop.run_until_complete(t)
coro = format_coroutine(coro_qualname, 'done', src,
t._source_traceback)
self.assertEqual(repr(t),
"<Task finished %s result='abc'>" % coro)

def test_task_basics(self):
@asyncio.coroutine
def outer():
Expand All @@ -109,7 +55,7 @@ def task():
return 12

t = self.create_task(task())
test_utils.run_briefly(self.loop) # start coro
tb.run_briefly(self.loop) # start coro
t.cancel()
self.assertRaises(
asyncio.CancelledError, self.loop.run_until_complete, t)
Expand All @@ -126,7 +72,7 @@ def task():
return 12

t = self.create_task(task())
test_utils.run_briefly(self.loop) # start task
tb.run_briefly(self.loop) # start task
f.cancel()
with self.assertRaises(asyncio.CancelledError):
self.loop.run_until_complete(t)
Expand All @@ -143,7 +89,7 @@ def task():

t = self.create_task(task())
self.assertEqual(asyncio.Task.all_tasks(loop=self.loop), {t})
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)

f.cancel()
t.cancel()
Expand All @@ -168,10 +114,10 @@ def task():
return 42

t = self.create_task(task())
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut1) # White-box test.
fut1.set_result(None)
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut2) # White-box test.
t.cancel()
self.assertTrue(fut2.cancelled())
Expand All @@ -195,14 +141,14 @@ def task():
return res

t = self.create_task(task())
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut1) # White-box test.
fut1.set_result(None)
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut2) # White-box test.
t.cancel()
self.assertTrue(fut2.cancelled())
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertIs(t._fut_waiter, fut3) # White-box test.
fut3.set_result(42)
res = self.loop.run_until_complete(t)
Expand Down Expand Up @@ -232,7 +178,8 @@ def notmutch():
raise BaseException()

task = self.create_task(notmutch())
self.assertRaises(BaseException, task._step)
with self.assertRaises(BaseException):
tb.run_briefly(self.loop)

self.assertTrue(task.done())
self.assertIsInstance(task.exception(), BaseException)
Expand All @@ -245,7 +192,7 @@ def __init__(self, *args, **kwds):
self.cb_added = False
super().__init__(*args, **kwds)

def add_done_callback(self, fn):
def add_done_callback(self, fn, context=None):
self.cb_added = True
super().add_done_callback(fn)

Expand All @@ -258,12 +205,12 @@ def wait_for_future():
result = yield from fut

t = self.create_task(wait_for_future())
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertTrue(fut.cb_added)

res = object()
fut.set_result(res)
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertIs(res, result)
self.assertTrue(t.done())
self.assertIsNone(t.result())
Expand Down Expand Up @@ -356,7 +303,7 @@ def outer():
proof += 10

f = asyncio.ensure_future(outer(), loop=self.loop)
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
f.cancel()
self.loop.run_until_complete(f)
self.assertEqual(proof, 101)
Expand All @@ -381,12 +328,12 @@ def outer():
proof += 100

f = asyncio.ensure_future(outer(), loop=self.loop)
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
f.cancel()
self.assertRaises(
asyncio.CancelledError, self.loop.run_until_complete, f)
waiter.set_result(None)
test_utils.run_briefly(self.loop)
tb.run_briefly(self.loop)
self.assertEqual(proof, 1)


Expand Down
5 changes: 2 additions & 3 deletions tests/test_udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest
import sys

from asyncio import test_utils
from uvloop import _testbase as tb


Expand Down Expand Up @@ -77,9 +76,9 @@ def datagram_received(self, data, addr):
self.assertIs(client.transport, transport)

transport.sendto(b'xxx')
test_utils.run_until(self.loop, lambda: server.nbytes)
tb.run_until(self.loop, lambda: server.nbytes)
self.assertEqual(3, server.nbytes)
test_utils.run_until(self.loop, lambda: client.nbytes)
tb.run_until(self.loop, lambda: client.nbytes)

# received
self.assertEqual(8, client.nbytes)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile
import time
import unittest
import sys

from uvloop import _testbase as tb

Expand Down Expand Up @@ -362,6 +363,7 @@ async def client():
self.assertIn(excs[0].__class__,
(BrokenPipeError, ConnectionResetError))

@unittest.skipUnless(sys.version_info < (3, 7), 'Python version must be < 3.7')
def test_transport_unclosed_warning(self):
async def test(sock):
return await self.loop.create_unix_connection(
Expand Down
43 changes: 43 additions & 0 deletions uvloop/_testbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,46 @@ def _handle_client(self, sock):
@property
def addr(self):
return self._sock.getsockname()


###############################################################################
# A few helpers from asyncio/tests/testutils.py
###############################################################################


def run_briefly(loop):
async def once():
pass
gen = once()
t = loop.create_task(gen)
# Don't log a warning if the task is not done after run_until_complete().
# It occurs if the loop is stopped or if a task raises a BaseException.
t._log_destroy_pending = False
try:
loop.run_until_complete(t)
finally:
gen.close()


def run_until(loop, pred, timeout=30):
deadline = time.time() + timeout
while not pred():
if timeout is not None:
timeout = deadline - time.time()
if timeout <= 0:
raise asyncio.futures.TimeoutError()
loop.run_until_complete(asyncio.tasks.sleep(0.001, loop=loop))


@contextlib.contextmanager
def disable_logger():
"""Context manager to disable asyncio logger.

For example, it can be used to ignore warnings in debug mode.
"""
old_level = asyncio.log.logger.level
try:
asyncio.log.logger.setLevel(logging.CRITICAL + 1)
yield
finally:
asyncio.log.logger.setLevel(old_level)
2 changes: 1 addition & 1 deletion uvloop/includes/stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ cdef aio_iscoroutinefunction = asyncio.iscoroutinefunction
cdef aio_BaseProtocol = asyncio.BaseProtocol
cdef aio_Protocol = asyncio.Protocol
cdef aio_SSLProtocol = asyncio.sslproto.SSLProtocol
cdef aio_debug_wrapper = asyncio.coroutines.debug_wrapper
cdef aio_isfuture = getattr(asyncio, 'isfuture', None)
cdef aio_get_running_loop = getattr(asyncio, '_get_running_loop', None)
cdef aio_set_running_loop = getattr(asyncio, '_set_running_loop', None)
cdef aio_debug_wrapper = getattr(asyncio.coroutines, 'debug_wrapper', None)

cdef col_deque = collections.deque
cdef col_Iterable = collections.Iterable
Expand Down
Loading