bpo-38107: Replace direct future and task contructor calls with factories in asyncio tests (GH-15928)

(cherry picked from commit 9aee90018a)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
This commit is contained in:
Miss Islington (bot) 2019-09-11 07:21:53 -07:00 committed by GitHub
parent fef5bdc645
commit b18b19809d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 156 additions and 159 deletions

View File

@ -193,7 +193,7 @@ class BaseEventLoopTests(test_utils.TestCase):
self.loop.close() self.loop.close()
# operation blocked when the loop is closed # operation blocked when the loop is closed
f = asyncio.Future(loop=self.loop) f = self.loop.create_future()
self.assertRaises(RuntimeError, self.loop.run_forever) self.assertRaises(RuntimeError, self.loop.run_forever)
self.assertRaises(RuntimeError, self.loop.run_until_complete, f) self.assertRaises(RuntimeError, self.loop.run_until_complete, f)
@ -324,7 +324,7 @@ class BaseEventLoopTests(test_utils.TestCase):
def test_thread(loop, debug, create_loop=False): def test_thread(loop, debug, create_loop=False):
event = threading.Event() event = threading.Event()
fut = asyncio.Future(loop=loop) fut = loop.create_future()
loop.call_soon(event.set) loop.call_soon(event.set)
args = (loop, event, debug, create_loop, fut) args = (loop, event, debug, create_loop, fut)
thread = threading.Thread(target=check_in_thread, args=args) thread = threading.Thread(target=check_in_thread, args=args)
@ -472,7 +472,7 @@ class BaseEventLoopTests(test_utils.TestCase):
self.loop.run_until_complete, 'blah') self.loop.run_until_complete, 'blah')
def test_run_until_complete_loop(self): def test_run_until_complete_loop(self):
task = asyncio.Future(loop=self.loop) task = self.loop.create_future()
other_loop = self.new_test_loop() other_loop = self.new_test_loop()
self.addCleanup(other_loop.close) self.addCleanup(other_loop.close)
self.assertRaises(ValueError, self.assertRaises(ValueError,
@ -555,7 +555,7 @@ class BaseEventLoopTests(test_utils.TestCase):
# Test call_soon (events.Handle) # Test call_soon (events.Handle)
with mock.patch('asyncio.base_events.logger') as log: with mock.patch('asyncio.base_events.logger') as log:
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
self.loop.call_soon(zero_error, fut) self.loop.call_soon(zero_error, fut)
fut.add_done_callback(lambda fut: self.loop.stop()) fut.add_done_callback(lambda fut: self.loop.stop())
self.loop.run_forever() self.loop.run_forever()
@ -565,7 +565,7 @@ class BaseEventLoopTests(test_utils.TestCase):
# Test call_later (events.TimerHandle) # Test call_later (events.TimerHandle)
with mock.patch('asyncio.base_events.logger') as log: with mock.patch('asyncio.base_events.logger') as log:
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
self.loop.call_later(0.01, zero_error, fut) self.loop.call_later(0.01, zero_error, fut)
fut.add_done_callback(lambda fut: self.loop.stop()) fut.add_done_callback(lambda fut: self.loop.stop())
self.loop.run_forever() self.loop.run_forever()
@ -996,7 +996,7 @@ class MyProto(asyncio.Protocol):
self.state = 'INITIAL' self.state = 'INITIAL'
self.nbytes = 0 self.nbytes = 0
if create_future: if create_future:
self.done = asyncio.Future() self.done = asyncio.get_running_loop().create_future()
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
@ -1026,7 +1026,7 @@ class MyDatagramProto(asyncio.DatagramProtocol):
self.state = 'INITIAL' self.state = 'INITIAL'
self.nbytes = 0 self.nbytes = 0
if create_future: if create_future:
self.done = asyncio.Future(loop=loop) self.done = loop.create_future()
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
@ -1071,7 +1071,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
(2, 1, 6, '', ('107.6.106.82', 80))] (2, 1, 6, '', ('107.6.106.82', 80))]
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
idx = -1 idx = -1
errors = ['err1', 'err2'] errors = ['err1', 'err2']
@ -1098,7 +1098,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
m_socket.socket.return_value = sock m_socket.socket.return_value = sock
def getaddrinfo(*args, **kw): def getaddrinfo(*args, **kw):
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
addr = (socket.AF_INET, socket.SOCK_STREAM, 0, '', addr = (socket.AF_INET, socket.SOCK_STREAM, 0, '',
('127.0.0.1', 80)) ('127.0.0.1', 80))
fut.set_result([addr]) fut.set_result([addr])
@ -1190,7 +1190,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
return [] return []
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
self.loop.getaddrinfo = getaddrinfo_task self.loop.getaddrinfo = getaddrinfo_task
coro = self.loop.create_connection(MyProto, 'example.com', 80) coro = self.loop.create_connection(MyProto, 'example.com', 80)
@ -1202,7 +1202,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
return [(2, 1, 6, '', ('107.6.106.82', 80))] return [(2, 1, 6, '', ('107.6.106.82', 80))]
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
self.loop.getaddrinfo = getaddrinfo_task self.loop.getaddrinfo = getaddrinfo_task
self.loop.sock_connect = mock.Mock() self.loop.sock_connect = mock.Mock()
@ -1218,7 +1218,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
(2, 1, 6, '', ('0.0.0.2', 80))] (2, 1, 6, '', ('0.0.0.2', 80))]
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
self.loop.getaddrinfo = getaddrinfo_task self.loop.getaddrinfo = getaddrinfo_task
self.loop.sock_connect = mock.Mock() self.loop.sock_connect = mock.Mock()
@ -1245,7 +1245,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
(2, 1, 6, '', ('0.0.0.2', 80))] (2, 1, 6, '', ('0.0.0.2', 80))]
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
self.loop.getaddrinfo = getaddrinfo_task self.loop.getaddrinfo = getaddrinfo_task
self.loop.sock_connect = mock.Mock() self.loop.sock_connect = mock.Mock()
@ -1377,7 +1377,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
return [] return []
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
self.loop.getaddrinfo = getaddrinfo_task self.loop.getaddrinfo = getaddrinfo_task
coro = self.loop.create_connection( coro = self.loop.create_connection(
@ -1405,7 +1405,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
self.loop.getaddrinfo = mock.Mock() self.loop.getaddrinfo = mock.Mock()
def mock_getaddrinfo(*args, **kwds): def mock_getaddrinfo(*args, **kwds):
f = asyncio.Future(loop=self.loop) f = self.loop.create_future()
f.set_result([(socket.AF_INET, socket.SOCK_STREAM, f.set_result([(socket.AF_INET, socket.SOCK_STREAM,
socket.SOL_TCP, '', ('1.2.3.4', 80))]) socket.SOL_TCP, '', ('1.2.3.4', 80))])
return f return f
@ -1513,7 +1513,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
return [] return []
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
self.loop.getaddrinfo = getaddrinfo_task self.loop.getaddrinfo = getaddrinfo_task
fut = self.loop.create_server(MyProto, '', 0) fut = self.loop.create_server(MyProto, '', 0)

View File

@ -81,8 +81,8 @@ class MyBaseProto(asyncio.Protocol):
self.state = 'INITIAL' self.state = 'INITIAL'
self.nbytes = 0 self.nbytes = 0
if loop is not None: if loop is not None:
self.connected = asyncio.Future(loop=loop) self.connected = loop.create_future()
self.done = asyncio.Future(loop=loop) self.done = loop.create_future()
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
@ -119,7 +119,7 @@ class MyDatagramProto(asyncio.DatagramProtocol):
self.state = 'INITIAL' self.state = 'INITIAL'
self.nbytes = 0 self.nbytes = 0
if loop is not None: if loop is not None:
self.done = asyncio.Future(loop=loop) self.done = loop.create_future()
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
@ -148,7 +148,7 @@ class MyReadPipeProto(asyncio.Protocol):
self.nbytes = 0 self.nbytes = 0
self.transport = None self.transport = None
if loop is not None: if loop is not None:
self.done = asyncio.Future(loop=loop) self.done = loop.create_future()
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
@ -179,7 +179,7 @@ class MyWritePipeProto(asyncio.BaseProtocol):
self.state = 'INITIAL' self.state = 'INITIAL'
self.transport = None self.transport = None
if loop is not None: if loop is not None:
self.done = asyncio.Future(loop=loop) self.done = loop.create_future()
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
@ -198,9 +198,9 @@ class MySubprocessProtocol(asyncio.SubprocessProtocol):
def __init__(self, loop): def __init__(self, loop):
self.state = 'INITIAL' self.state = 'INITIAL'
self.transport = None self.transport = None
self.connected = asyncio.Future(loop=loop) self.connected = loop.create_future()
self.completed = asyncio.Future(loop=loop) self.completed = loop.create_future()
self.disconnects = {fd: asyncio.Future(loop=loop) for fd in range(3)} self.disconnects = {fd: loop.create_future() for fd in range(3)}
self.data = {1: b'', 2: b''} self.data = {1: b'', 2: b''}
self.returncode = None self.returncode = None
self.got_data = {1: asyncio.Event(loop=loop), self.got_data = {1: asyncio.Event(loop=loop),
@ -738,7 +738,7 @@ class EventLoopTestsMixin:
return [(family, socket.SOCK_STREAM, 6, '', (host, port, 0, 0))] return [(family, socket.SOCK_STREAM, 6, '', (host, port, 0, 0))]
def getaddrinfo_task(*args, **kwds): def getaddrinfo_task(*args, **kwds):
return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) return self.loop.create_task(getaddrinfo(*args, **kwds))
unique_hosts = set(hosts) unique_hosts = set(hosts)
@ -1094,7 +1094,7 @@ class EventLoopTestsMixin:
client, pr = self.loop.run_until_complete(f_c) client, pr = self.loop.run_until_complete(f_c)
# extra info is available # extra info is available
self.check_ssl_extra_info(client,peername=(host, port), self.check_ssl_extra_info(client, peername=(host, port),
peercert=test_utils.PEERCERT) peercert=test_utils.PEERCERT)
# close connection # close connection
@ -1104,7 +1104,7 @@ class EventLoopTestsMixin:
self.loop.run_until_complete(proto.done) self.loop.run_until_complete(proto.done)
def test_create_server_sock(self): def test_create_server_sock(self):
proto = asyncio.Future(loop=self.loop) proto = self.loop.create_future()
class TestMyProto(MyProto): class TestMyProto(MyProto):
def connection_made(self, transport): def connection_made(self, transport):
@ -1143,7 +1143,7 @@ class EventLoopTestsMixin:
@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 not supported or enabled') @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 not supported or enabled')
def test_create_server_dual_stack(self): def test_create_server_dual_stack(self):
f_proto = asyncio.Future(loop=self.loop) f_proto = self.loop.create_future()
class TestMyProto(MyProto): class TestMyProto(MyProto):
def connection_made(self, transport): def connection_made(self, transport):
@ -1172,7 +1172,7 @@ class EventLoopTestsMixin:
proto.transport.close() proto.transport.close()
client.close() client.close()
f_proto = asyncio.Future(loop=self.loop) f_proto = self.loop.create_future()
client = socket.socket(socket.AF_INET6) client = socket.socket(socket.AF_INET6)
client.connect(('::1', port)) client.connect(('::1', port))
client.send(b'xxx') client.send(b'xxx')
@ -1587,7 +1587,7 @@ class EventLoopTestsMixin:
return res return res
start = time.monotonic() start = time.monotonic()
t = asyncio.Task(main(), loop=self.loop) t = self.loop.create_task(main())
self.loop.run_forever() self.loop.run_forever()
elapsed = time.monotonic() - start elapsed = time.monotonic() - start
@ -1673,7 +1673,7 @@ class EventLoopTestsMixin:
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
self.loop.run_forever() self.loop.run_forever()
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
self.loop.run_until_complete(fut) self.loop.run_until_complete(fut)
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
self.loop.call_soon(func) self.loop.call_soon(func)

View File

@ -124,8 +124,8 @@ class LockTests(test_utils.TestCase):
result.append(3) result.append(3)
return True return True
t1 = asyncio.Task(c1(result), loop=self.loop) t1 = self.loop.create_task(c1(result))
t2 = asyncio.Task(c2(result), loop=self.loop) t2 = self.loop.create_task(c2(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([], result) self.assertEqual([], result)
@ -137,7 +137,7 @@ class LockTests(test_utils.TestCase):
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([1], result) self.assertEqual([1], result)
t3 = asyncio.Task(c3(result), loop=self.loop) t3 = self.loop.create_task(c3(result))
lock.release() lock.release()
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
@ -159,7 +159,7 @@ class LockTests(test_utils.TestCase):
lock = asyncio.Lock(loop=self.loop) lock = asyncio.Lock(loop=self.loop)
self.assertTrue(self.loop.run_until_complete(lock.acquire())) self.assertTrue(self.loop.run_until_complete(lock.acquire()))
task = asyncio.Task(lock.acquire(), loop=self.loop) task = self.loop.create_task(lock.acquire())
self.loop.call_soon(task.cancel) self.loop.call_soon(task.cancel)
self.assertRaises( self.assertRaises(
asyncio.CancelledError, asyncio.CancelledError,
@ -192,14 +192,14 @@ class LockTests(test_utils.TestCase):
finally: finally:
lock.release() lock.release()
fa = asyncio.Future(loop=self.loop) fa = self.loop.create_future()
ta = asyncio.Task(lockit('A', fa), loop=self.loop) ta = self.loop.create_task(lockit('A', fa))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertTrue(lock.locked()) self.assertTrue(lock.locked())
tb = asyncio.Task(lockit('B', None), loop=self.loop) tb = self.loop.create_task(lockit('B', None))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual(len(lock._waiters), 1) self.assertEqual(len(lock._waiters), 1)
tc = asyncio.Task(lockit('C', None), loop=self.loop) tc = self.loop.create_task(lockit('C', None))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual(len(lock._waiters), 2) self.assertEqual(len(lock._waiters), 2)
@ -267,17 +267,17 @@ class LockTests(test_utils.TestCase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
lock = asyncio.Lock(loop=self.loop) lock = asyncio.Lock(loop=self.loop)
ta = asyncio.Task(lock.acquire(), loop=self.loop) ta = self.loop.create_task(lock.acquire())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertTrue(lock.locked()) self.assertTrue(lock.locked())
tb = asyncio.Task(lock.acquire(), loop=self.loop) tb = self.loop.create_task(lock.acquire())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual(len(lock._waiters), 1) self.assertEqual(len(lock._waiters), 1)
# Create a second waiter, wake up the first, and cancel it. # Create a second waiter, wake up the first, and cancel it.
# Without the fix, the second was not woken up. # Without the fix, the second was not woken up.
tc = asyncio.Task(lock.acquire(), loop=self.loop) tc = self.loop.create_task(lock.acquire())
lock.release() lock.release()
tb.cancel() tb.cancel()
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
@ -405,13 +405,13 @@ class EventTests(test_utils.TestCase):
if await ev.wait(): if await ev.wait():
result.append(3) result.append(3)
t1 = asyncio.Task(c1(result), loop=self.loop) t1 = self.loop.create_task(c1(result))
t2 = asyncio.Task(c2(result), loop=self.loop) t2 = self.loop.create_task(c2(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([], result) self.assertEqual([], result)
t3 = asyncio.Task(c3(result), loop=self.loop) t3 = self.loop.create_task(c3(result))
ev.set() ev.set()
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
@ -436,7 +436,7 @@ class EventTests(test_utils.TestCase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
ev = asyncio.Event(loop=self.loop) ev = asyncio.Event(loop=self.loop)
wait = asyncio.Task(ev.wait(), loop=self.loop) wait = self.loop.create_task(ev.wait())
self.loop.call_soon(wait.cancel) self.loop.call_soon(wait.cancel)
self.assertRaises( self.assertRaises(
asyncio.CancelledError, asyncio.CancelledError,
@ -464,7 +464,7 @@ class EventTests(test_utils.TestCase):
result.append(1) result.append(1)
return True return True
t = asyncio.Task(c1(result), loop=self.loop) t = self.loop.create_task(c1(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([], result) self.assertEqual([], result)
@ -527,9 +527,9 @@ class ConditionTests(test_utils.TestCase):
result.append(3) result.append(3)
return True return True
t1 = asyncio.Task(c1(result), loop=self.loop) t1 = self.loop.create_task(c1(result))
t2 = asyncio.Task(c2(result), loop=self.loop) t2 = self.loop.create_task(c2(result))
t3 = asyncio.Task(c3(result), loop=self.loop) t3 = self.loop.create_task(c3(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([], result) self.assertEqual([], result)
@ -573,7 +573,7 @@ class ConditionTests(test_utils.TestCase):
cond = asyncio.Condition(loop=self.loop) cond = asyncio.Condition(loop=self.loop)
self.loop.run_until_complete(cond.acquire()) self.loop.run_until_complete(cond.acquire())
wait = asyncio.Task(cond.wait(), loop=self.loop) wait = self.loop.create_task(cond.wait())
self.loop.call_soon(wait.cancel) self.loop.call_soon(wait.cancel)
self.assertRaises( self.assertRaises(
asyncio.CancelledError, asyncio.CancelledError,
@ -588,7 +588,7 @@ class ConditionTests(test_utils.TestCase):
self.loop.run_until_complete(cond.acquire()) self.loop.run_until_complete(cond.acquire())
self.assertTrue(cond.locked()) self.assertTrue(cond.locked())
wait_task = asyncio.Task(cond.wait(), loop=self.loop) wait_task = self.loop.create_task(cond.wait())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertFalse(cond.locked()) self.assertFalse(cond.locked())
@ -657,7 +657,7 @@ class ConditionTests(test_utils.TestCase):
cond.release() cond.release()
return True return True
t = asyncio.Task(c1(result), loop=self.loop) t = self.loop.create_task(c1(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([], result) self.assertEqual([], result)
@ -717,9 +717,9 @@ class ConditionTests(test_utils.TestCase):
cond.release() cond.release()
return True return True
t1 = asyncio.Task(c1(result), loop=self.loop) t1 = self.loop.create_task(c1(result))
t2 = asyncio.Task(c2(result), loop=self.loop) t2 = self.loop.create_task(c2(result))
t3 = asyncio.Task(c3(result), loop=self.loop) t3 = self.loop.create_task(c3(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([], result) self.assertEqual([], result)
@ -764,8 +764,8 @@ class ConditionTests(test_utils.TestCase):
cond.release() cond.release()
return True return True
t1 = asyncio.Task(c1(result), loop=self.loop) t1 = self.loop.create_task(c1(result))
t2 = asyncio.Task(c2(result), loop=self.loop) t2 = self.loop.create_task(c2(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([], result) self.assertEqual([], result)
@ -965,9 +965,9 @@ class SemaphoreTests(test_utils.TestCase):
result.append(4) result.append(4)
return True return True
t1 = asyncio.Task(c1(result), loop=self.loop) t1 = self.loop.create_task(c1(result))
t2 = asyncio.Task(c2(result), loop=self.loop) t2 = self.loop.create_task(c2(result))
t3 = asyncio.Task(c3(result), loop=self.loop) t3 = self.loop.create_task(c3(result))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual([1], result) self.assertEqual([1], result)
@ -975,7 +975,7 @@ class SemaphoreTests(test_utils.TestCase):
self.assertEqual(2, len(sem._waiters)) self.assertEqual(2, len(sem._waiters))
self.assertEqual(0, sem._value) self.assertEqual(0, sem._value)
t4 = asyncio.Task(c4(result), loop=self.loop) t4 = self.loop.create_task(c4(result))
sem.release() sem.release()
sem.release() sem.release()
@ -1003,7 +1003,7 @@ class SemaphoreTests(test_utils.TestCase):
sem = asyncio.Semaphore(loop=self.loop) sem = asyncio.Semaphore(loop=self.loop)
self.loop.run_until_complete(sem.acquire()) self.loop.run_until_complete(sem.acquire())
acquire = asyncio.Task(sem.acquire(), loop=self.loop) acquire = self.loop.create_task(sem.acquire())
self.loop.call_soon(acquire.cancel) self.loop.call_soon(acquire.cancel)
self.assertRaises( self.assertRaises(
asyncio.CancelledError, asyncio.CancelledError,
@ -1015,10 +1015,10 @@ class SemaphoreTests(test_utils.TestCase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
sem = asyncio.Semaphore(value=0, loop=self.loop) sem = asyncio.Semaphore(value=0, loop=self.loop)
t1 = asyncio.Task(sem.acquire(), loop=self.loop) t1 = self.loop.create_task(sem.acquire())
t2 = asyncio.Task(sem.acquire(), loop=self.loop) t2 = self.loop.create_task(sem.acquire())
t3 = asyncio.Task(sem.acquire(), loop=self.loop) t3 = self.loop.create_task(sem.acquire())
t4 = asyncio.Task(sem.acquire(), loop=self.loop) t4 = self.loop.create_task(sem.acquire())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
@ -1038,8 +1038,8 @@ class SemaphoreTests(test_utils.TestCase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
sem = asyncio.Semaphore(value=0, loop=self.loop) sem = asyncio.Semaphore(value=0, loop=self.loop)
t1 = asyncio.Task(sem.acquire(), loop=self.loop) t1 = self.loop.create_task(sem.acquire())
t2 = asyncio.Task(sem.acquire(), loop=self.loop) t2 = self.loop.create_task(sem.acquire())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)

View File

@ -206,7 +206,7 @@ class CoroutineTests(BaseTest):
async def runner(): async def runner():
coro = afunc() coro = afunc()
t = asyncio.Task(coro, loop=self.loop) t = self.loop.create_task(coro)
try: try:
await asyncio.sleep(0) await asyncio.sleep(0)
await coro await coro

View File

@ -47,7 +47,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
return transport return transport
def test_ctor(self): def test_ctor(self):
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
tr = self.socket_transport(waiter=fut) tr = self.socket_transport(waiter=fut)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result()) self.assertIsNone(fut.result())
@ -62,7 +62,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertFalse(self.protocol.eof_received.called) self.assertFalse(self.protocol.eof_received.called)
def test_loop_reading_data(self): def test_loop_reading_data(self):
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(b'data') res.set_result(b'data')
tr = self.socket_transport() tr = self.socket_transport()
@ -72,7 +72,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.protocol.data_received.assert_called_with(b'data') self.protocol.data_received.assert_called_with(b'data')
def test_loop_reading_no_data(self): def test_loop_reading_no_data(self):
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(b'') res.set_result(b'')
tr = self.socket_transport() tr = self.socket_transport()
@ -182,7 +182,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
m_log.warning.assert_called_with('socket.send() raised exception.') m_log.warning.assert_called_with('socket.send() raised exception.')
def test_loop_writing_stop(self): def test_loop_writing_stop(self):
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
fut.set_result(b'data') fut.set_result(b'data')
tr = self.socket_transport() tr = self.socket_transport()
@ -191,7 +191,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
self.assertIsNone(tr._write_fut) self.assertIsNone(tr._write_fut)
def test_loop_writing_closing(self): def test_loop_writing_closing(self):
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
fut.set_result(1) fut.set_result(1)
tr = self.socket_transport() tr = self.socket_transport()
@ -260,7 +260,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_loop_writing_force_close(self): def test_loop_writing_force_close(self):
exc_handler = mock.Mock() exc_handler = mock.Mock()
self.loop.set_exception_handler(exc_handler) self.loop.set_exception_handler(exc_handler)
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
fut.set_result(1) fut.set_result(1)
self.proactor.send.return_value = fut self.proactor.send.return_value = fut
@ -303,7 +303,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_write_eof_buffer(self): def test_write_eof_buffer(self):
tr = self.socket_transport() tr = self.socket_transport()
f = asyncio.Future(loop=self.loop) f = self.loop.create_future()
tr._loop._proactor.send.return_value = f tr._loop._proactor.send.return_value = f
tr.write(b'data') tr.write(b'data')
tr.write_eof() tr.write_eof()
@ -327,7 +327,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
def test_write_eof_buffer_write_pipe(self): def test_write_eof_buffer_write_pipe(self):
tr = _ProactorWritePipeTransport(self.loop, self.sock, self.protocol) tr = _ProactorWritePipeTransport(self.loop, self.sock, self.protocol)
f = asyncio.Future(loop=self.loop) f = self.loop.create_future()
tr._loop._proactor.send.return_value = f tr._loop._proactor.send.return_value = f
tr.write(b'data') tr.write(b'data')
tr.write_eof() tr.write_eof()
@ -352,7 +352,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
tr = self.socket_transport() tr = self.socket_transport()
futures = [] futures = []
for msg in [b'data1', b'data2', b'data3', b'data4', b'data5', b'']: for msg in [b'data1', b'data2', b'data3', b'data4', b'data5', b'']:
f = asyncio.Future(loop=self.loop) f = self.loop.create_future()
f.set_result(msg) f.set_result(msg)
futures.append(f) futures.append(f)
@ -406,7 +406,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
tr = self.pause_writing_transport(high=4) tr = self.pause_writing_transport(high=4)
# write a large chunk, must pause writing # write a large chunk, must pause writing
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
self.loop._proactor.send.return_value = fut self.loop._proactor.send.return_value = fut
tr.write(b'large data') tr.write(b'large data')
self.loop._run_once() self.loop._run_once()
@ -422,7 +422,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
tr = self.pause_writing_transport(high=4) tr = self.pause_writing_transport(high=4)
# first short write, the buffer is not full (3 <= 4) # first short write, the buffer is not full (3 <= 4)
fut1 = asyncio.Future(loop=self.loop) fut1 = self.loop.create_future()
self.loop._proactor.send.return_value = fut1 self.loop._proactor.send.return_value = fut1
tr.write(b'123') tr.write(b'123')
self.loop._run_once() self.loop._run_once()
@ -439,7 +439,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
tr = self.pause_writing_transport(high=4) tr = self.pause_writing_transport(high=4)
# first short write, the buffer is not full (1 <= 4) # first short write, the buffer is not full (1 <= 4)
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
self.loop._proactor.send.return_value = fut self.loop._proactor.send.return_value = fut
tr.write(b'1') tr.write(b'1')
self.loop._run_once() self.loop._run_once()
@ -463,7 +463,7 @@ class ProactorSocketTransportTests(test_utils.TestCase):
# write a large chunk which completes immediately, # write a large chunk which completes immediately,
# it should not pause writing # it should not pause writing
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
fut.set_result(None) fut.set_result(None)
self.loop._proactor.send.return_value = fut self.loop._proactor.send.return_value = fut
tr.write(b'very large data') tr.write(b'very large data')
@ -496,7 +496,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
return transport return transport
def test_ctor(self): def test_ctor(self):
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
tr = self.socket_transport(waiter=fut) tr = self.socket_transport(waiter=fut)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertIsNone(fut.result()) self.assertIsNone(fut.result())
@ -541,7 +541,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
self.protocol = test_utils.make_test_protocol(asyncio.Protocol) self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
tr = self.socket_transport() tr = self.socket_transport()
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(b'data') res.set_result(b'data')
tr = self.socket_transport() tr = self.socket_transport()
@ -558,7 +558,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
tr.set_protocol(buf_proto) tr.set_protocol(buf_proto)
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(4) res.set_result(4)
tr._read_fut = res tr._read_fut = res
@ -591,7 +591,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
self.loop.call_exception_handler = mock.Mock() self.loop.call_exception_handler = mock.Mock()
self.protocol.buffer_updated.side_effect = LookupError() self.protocol.buffer_updated.side_effect = LookupError()
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(10) res.set_result(10)
transport._read_fut = res transport._read_fut = res
transport._loop_reading(res) transport._loop_reading(res)
@ -601,7 +601,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
self.assertTrue(self.protocol.buffer_updated.called) self.assertTrue(self.protocol.buffer_updated.called)
def test_loop_eof_received_error(self): def test_loop_eof_received_error(self):
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(0) res.set_result(0)
self.protocol.eof_received.side_effect = LookupError() self.protocol.eof_received.side_effect = LookupError()
@ -617,7 +617,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
self.assertTrue(tr._fatal_error.called) self.assertTrue(tr._fatal_error.called)
def test_loop_reading_data(self): def test_loop_reading_data(self):
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(4) res.set_result(4)
tr = self.socket_transport() tr = self.socket_transport()
@ -627,7 +627,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
self.protocol.buffer_updated.assert_called_with(4) self.protocol.buffer_updated.assert_called_with(4)
def test_loop_reading_no_data(self): def test_loop_reading_no_data(self):
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result(0) res.set_result(0)
tr = self.socket_transport() tr = self.socket_transport()
@ -691,7 +691,7 @@ class ProactorSocketTransportBufferedProtoTests(test_utils.TestCase):
tr = self.socket_transport() tr = self.socket_transport()
futures = [] futures = []
for msg in [10, 20, 30, 40, 0]: for msg in [10, 20, 30, 40, 0]:
f = asyncio.Future(loop=self.loop) f = self.loop.create_future()
f.set_result(msg) f.set_result(msg)
futures.append(f) futures.append(f)
@ -1044,7 +1044,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
loop = call_soon.call_args[0][0] loop = call_soon.call_args[0][0]
# cancelled # cancelled
fut = asyncio.Future(loop=self.loop) fut = self.loop.create_future()
fut.cancel() fut.cancel()
loop(fut) loop(fut)
self.assertTrue(self.sock.close.called) self.assertTrue(self.sock.close.called)
@ -1094,7 +1094,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
close_transport(tr) close_transport(tr)
def test_datagram_loop_reading_data(self): def test_datagram_loop_reading_data(self):
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result((b'data', ('127.0.0.1', 12068))) res.set_result((b'data', ('127.0.0.1', 12068)))
tr = self.datagram_transport() tr = self.datagram_transport()
@ -1105,7 +1105,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
close_transport(tr) close_transport(tr)
def test_datagram_loop_reading_no_data(self): def test_datagram_loop_reading_no_data(self):
res = asyncio.Future(loop=self.loop) res = self.loop.create_future()
res.set_result((b'', ('127.0.0.1', 12068))) res.set_result((b'', ('127.0.0.1', 12068)))
tr = self.datagram_transport() tr = self.datagram_transport()

View File

@ -44,7 +44,7 @@ class QueueBasicTests(_QueueTestBase):
async def add_getter(): async def add_getter():
q = asyncio.Queue(loop=loop) q = asyncio.Queue(loop=loop)
# Start a task that waits to get. # Start a task that waits to get.
asyncio.Task(q.get(), loop=loop) loop.create_task(q.get())
# Let it start waiting. # Let it start waiting.
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
self.assertTrue('_getters[1]' in fn(q)) self.assertTrue('_getters[1]' in fn(q))
@ -58,7 +58,7 @@ class QueueBasicTests(_QueueTestBase):
q = asyncio.Queue(maxsize=1, loop=loop) q = asyncio.Queue(maxsize=1, loop=loop)
q.put_nowait(1) q.put_nowait(1)
# Start a task that waits to put. # Start a task that waits to put.
asyncio.Task(q.put(2), loop=loop) loop.create_task(q.put(2))
# Let it start waiting. # Let it start waiting.
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
self.assertTrue('_putters[1]' in fn(q)) self.assertTrue('_putters[1]' in fn(q))
@ -143,7 +143,7 @@ class QueueBasicTests(_QueueTestBase):
return True return True
async def test(): async def test():
t = asyncio.Task(putter(), loop=loop) t = loop.create_task(putter())
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
# The putter is blocked after putting two items. # The putter is blocked after putting two items.
@ -181,7 +181,7 @@ class QueueGetTests(_QueueTestBase):
q = asyncio.Queue(1, loop=self.loop) q = asyncio.Queue(1, loop=self.loop)
q.put_nowait(1) q.put_nowait(1)
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
q._putters.append(waiter) q._putters.append(waiter)
res = self.loop.run_until_complete(q.get()) res = self.loop.run_until_complete(q.get())
@ -212,7 +212,7 @@ class QueueGetTests(_QueueTestBase):
async def queue_put(): async def queue_put():
loop.call_later(0.01, q.put_nowait, 1) loop.call_later(0.01, q.put_nowait, 1)
queue_get_task = asyncio.Task(queue_get(), loop=loop) queue_get_task = loop.create_task(queue_get())
await started.wait() await started.wait()
self.assertFalse(finished) self.assertFalse(finished)
res = await queue_get_task res = await queue_get_task
@ -252,7 +252,7 @@ class QueueGetTests(_QueueTestBase):
return await asyncio.wait_for(q.get(), 0.051) return await asyncio.wait_for(q.get(), 0.051)
async def test(): async def test():
get_task = asyncio.Task(queue_get(), loop=loop) get_task = loop.create_task(queue_get())
await asyncio.sleep(0.01) # let the task start await asyncio.sleep(0.01) # let the task start
q.put_nowait(1) q.put_nowait(1)
return await get_task return await get_task
@ -264,8 +264,8 @@ class QueueGetTests(_QueueTestBase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
q = asyncio.Queue(loop=self.loop) q = asyncio.Queue(loop=self.loop)
t1 = asyncio.Task(q.get(), loop=self.loop) t1 = self.loop.create_task(q.get())
t2 = asyncio.Task(q.get(), loop=self.loop) t2 = self.loop.create_task(q.get())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
t1.cancel() t1.cancel()
@ -278,8 +278,8 @@ class QueueGetTests(_QueueTestBase):
def test_get_with_waiting_putters(self): def test_get_with_waiting_putters(self):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
q = asyncio.Queue(loop=self.loop, maxsize=1) q = asyncio.Queue(loop=self.loop, maxsize=1)
asyncio.Task(q.put('a'), loop=self.loop) self.loop.create_task(q.put('a'))
asyncio.Task(q.put('b'), loop=self.loop) self.loop.create_task(q.put('b'))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertEqual(self.loop.run_until_complete(q.get()), 'a') self.assertEqual(self.loop.run_until_complete(q.get()), 'a')
self.assertEqual(self.loop.run_until_complete(q.get()), 'b') self.assertEqual(self.loop.run_until_complete(q.get()), 'b')
@ -361,7 +361,7 @@ class QueuePutTests(_QueueTestBase):
async def queue_get(): async def queue_get():
loop.call_later(0.01, q.get_nowait) loop.call_later(0.01, q.get_nowait)
queue_put_task = asyncio.Task(queue_put(), loop=loop) queue_put_task = loop.create_task(queue_put())
await started.wait() await started.wait()
self.assertFalse(finished) self.assertFalse(finished)
await queue_put_task await queue_put_task
@ -502,7 +502,7 @@ class QueuePutTests(_QueueTestBase):
async def test(): async def test():
return await q.get() 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.assertEqual(1, self.loop.run_until_complete(test()))
self.assertTrue(t.done()) self.assertTrue(t.done())
self.assertTrue(t.result()) self.assertTrue(t.result())
@ -511,9 +511,9 @@ class QueuePutTests(_QueueTestBase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
q = asyncio.Queue(loop=self.loop, maxsize=1) q = asyncio.Queue(loop=self.loop, maxsize=1)
put_a = asyncio.Task(q.put('a'), loop=self.loop) put_a = self.loop.create_task(q.put('a'))
put_b = asyncio.Task(q.put('b'), loop=self.loop) put_b = self.loop.create_task(q.put('b'))
put_c = asyncio.Task(q.put('X'), loop=self.loop) put_c = self.loop.create_task(q.put('X'))
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.assertTrue(put_a.done()) self.assertTrue(put_a.done())
@ -531,7 +531,7 @@ class QueuePutTests(_QueueTestBase):
def test_put_with_waiting_getters(self): def test_put_with_waiting_getters(self):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
q = asyncio.Queue(loop=self.loop) q = asyncio.Queue(loop=self.loop)
t = asyncio.Task(q.get(), loop=self.loop) t = self.loop.create_task(q.get())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
self.loop.run_until_complete(q.put('a')) self.loop.run_until_complete(q.put('a'))
self.assertEqual(self.loop.run_until_complete(t), 'a') self.assertEqual(self.loop.run_until_complete(t), 'a')
@ -665,7 +665,7 @@ class _QueueJoinTestMixin:
q.task_done() q.task_done()
async def test(): async def test():
tasks = [asyncio.Task(worker(), loop=self.loop) tasks = [self.loop.create_task(worker())
for index in range(2)] for index in range(2)]
await q.join() await q.join()

View File

@ -78,7 +78,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
self.loop._add_writer = mock.Mock() self.loop._add_writer = mock.Mock()
self.loop._remove_reader = mock.Mock() self.loop._remove_reader = mock.Mock()
self.loop._remove_writer = mock.Mock() self.loop._remove_writer = mock.Mock()
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
with test_utils.disable_logger(): with test_utils.disable_logger():
transport = self.loop._make_ssl_transport( transport = self.loop._make_ssl_transport(
m, asyncio.Protocol(), m, waiter) m, asyncio.Protocol(), m, waiter)
@ -154,7 +154,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase):
self.loop.close() self.loop.close()
# operation blocked when the loop is closed # operation blocked when the loop is closed
f = asyncio.Future(loop=self.loop) f = self.loop.create_future()
self.assertRaises(RuntimeError, self.loop.run_forever) self.assertRaises(RuntimeError, self.loop.run_forever)
self.assertRaises(RuntimeError, self.loop.run_until_complete, f) self.assertRaises(RuntimeError, self.loop.run_until_complete, f)
fd = 0 fd = 0
@ -516,7 +516,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
return transport return transport
def test_ctor(self): def test_ctor(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
tr = self.socket_transport(waiter=waiter) tr = self.socket_transport(waiter=waiter)
self.loop.run_until_complete(waiter) self.loop.run_until_complete(waiter)
@ -525,7 +525,7 @@ class SelectorSocketTransportTests(test_utils.TestCase):
self.protocol.connection_made.assert_called_with(tr) self.protocol.connection_made.assert_called_with(tr)
def test_ctor_with_waiter(self): def test_ctor_with_waiter(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
self.socket_transport(waiter=waiter) self.socket_transport(waiter=waiter)
self.loop.run_until_complete(waiter) self.loop.run_until_complete(waiter)
@ -911,7 +911,7 @@ class SelectorSocketTransportBufferedProtocolTests(test_utils.TestCase):
return transport return transport
def test_ctor(self): def test_ctor(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
tr = self.socket_transport(waiter=waiter) tr = self.socket_transport(waiter=waiter)
self.loop.run_until_complete(waiter) self.loop.run_until_complete(waiter)

View File

@ -16,8 +16,8 @@ class MyProto(asyncio.Protocol):
self.state = 'INITIAL' self.state = 'INITIAL'
self.nbytes = 0 self.nbytes = 0
if loop is not None: if loop is not None:
self.connected = asyncio.Future(loop=loop) self.connected = loop.create_future()
self.done = asyncio.Future(loop=loop) self.done = loop.create_future()
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport

View File

@ -72,7 +72,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
ssl_handshake_timeout=-10) ssl_handshake_timeout=-10)
def test_eof_received_waiter(self): def test_eof_received_waiter(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
ssl_proto = self.ssl_protocol(waiter=waiter) ssl_proto = self.ssl_protocol(waiter=waiter)
self.connection_made(ssl_proto) self.connection_made(ssl_proto)
ssl_proto.eof_received() ssl_proto.eof_received()
@ -83,7 +83,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
# From issue #363. # From issue #363.
# _fatal_error() generates a NameError if sslproto.py # _fatal_error() generates a NameError if sslproto.py
# does not import base_events. # does not import base_events.
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
ssl_proto = self.ssl_protocol(waiter=waiter) ssl_proto = self.ssl_protocol(waiter=waiter)
# Temporarily turn off error logging so as not to spoil test output. # Temporarily turn off error logging so as not to spoil test output.
log_level = log.logger.getEffectiveLevel() log_level = log.logger.getEffectiveLevel()
@ -97,7 +97,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
def test_connection_lost(self): def test_connection_lost(self):
# From issue #472. # From issue #472.
# yield from waiter hang if lost_connection was called. # yield from waiter hang if lost_connection was called.
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
ssl_proto = self.ssl_protocol(waiter=waiter) ssl_proto = self.ssl_protocol(waiter=waiter)
self.connection_made(ssl_proto) self.connection_made(ssl_proto)
ssl_proto.connection_lost(ConnectionAbortedError) ssl_proto.connection_lost(ConnectionAbortedError)
@ -106,7 +106,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
def test_close_during_handshake(self): def test_close_during_handshake(self):
# bpo-29743 Closing transport during handshake process leaks socket # bpo-29743 Closing transport during handshake process leaks socket
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
ssl_proto = self.ssl_protocol(waiter=waiter) ssl_proto = self.ssl_protocol(waiter=waiter)
transport = self.connection_made(ssl_proto) transport = self.connection_made(ssl_proto)
@ -116,7 +116,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
self.assertTrue(transport.abort.called) self.assertTrue(transport.abort.called)
def test_get_extra_info_on_closed_connection(self): def test_get_extra_info_on_closed_connection(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
ssl_proto = self.ssl_protocol(waiter=waiter) ssl_proto = self.ssl_protocol(waiter=waiter)
self.assertIsNone(ssl_proto._get_extra_info('socket')) self.assertIsNone(ssl_proto._get_extra_info('socket'))
default = object() default = object()
@ -127,7 +127,7 @@ class SslProtoHandshakeTests(test_utils.TestCase):
self.assertIsNone(ssl_proto._get_extra_info('socket')) self.assertIsNone(ssl_proto._get_extra_info('socket'))
def test_set_new_app_protocol(self): def test_set_new_app_protocol(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
ssl_proto = self.ssl_protocol(waiter=waiter) ssl_proto = self.ssl_protocol(waiter=waiter)
new_app_proto = asyncio.Protocol() new_app_proto = asyncio.Protocol()
ssl_proto._app_transport.set_protocol(new_app_proto) ssl_proto._app_transport.set_protocol(new_app_proto)

View File

@ -192,7 +192,7 @@ class StreamTests(test_utils.TestCase):
stream = asyncio.Stream(mode=asyncio.StreamMode.READ, stream = asyncio.Stream(mode=asyncio.StreamMode.READ,
loop=self.loop, loop=self.loop,
_asyncio_internal=True) _asyncio_internal=True)
read_task = asyncio.Task(stream.read(30), loop=self.loop) read_task = self.loop.create_task(stream.read(30))
def cb(): def cb():
stream._feed_data(self.DATA) stream._feed_data(self.DATA)
@ -220,7 +220,7 @@ class StreamTests(test_utils.TestCase):
stream = asyncio.Stream(mode=asyncio.StreamMode.READ, stream = asyncio.Stream(mode=asyncio.StreamMode.READ,
loop=self.loop, loop=self.loop,
_asyncio_internal=True) _asyncio_internal=True)
read_task = asyncio.Task(stream.read(1024), loop=self.loop) read_task = self.loop.create_task(stream.read(1024))
def cb(): def cb():
stream._feed_eof() stream._feed_eof()
@ -235,7 +235,7 @@ class StreamTests(test_utils.TestCase):
stream = asyncio.Stream(mode=asyncio.StreamMode.READ, stream = asyncio.Stream(mode=asyncio.StreamMode.READ,
loop=self.loop, loop=self.loop,
_asyncio_internal=True) _asyncio_internal=True)
read_task = asyncio.Task(stream.read(-1), loop=self.loop) read_task = self.loop.create_task(stream.read(-1))
def cb(): def cb():
stream._feed_data(b'chunk1\n') stream._feed_data(b'chunk1\n')
@ -288,7 +288,7 @@ class StreamTests(test_utils.TestCase):
loop=self.loop, loop=self.loop,
_asyncio_internal=True) _asyncio_internal=True)
stream._feed_data(b'chunk1 ') stream._feed_data(b'chunk1 ')
read_task = asyncio.Task(stream.readline(), loop=self.loop) read_task = self.loop.create_task(stream.readline())
def cb(): def cb():
stream._feed_data(b'chunk2 ') stream._feed_data(b'chunk2 ')
@ -579,7 +579,7 @@ class StreamTests(test_utils.TestCase):
_asyncio_internal=True) _asyncio_internal=True)
n = 2 * len(self.DATA) n = 2 * len(self.DATA)
read_task = asyncio.Task(stream.readexactly(n), loop=self.loop) read_task = self.loop.create_task(stream.readexactly(n))
def cb(): def cb():
stream._feed_data(self.DATA) stream._feed_data(self.DATA)
@ -606,7 +606,7 @@ class StreamTests(test_utils.TestCase):
loop=self.loop, loop=self.loop,
_asyncio_internal=True) _asyncio_internal=True)
n = 2 * len(self.DATA) n = 2 * len(self.DATA)
read_task = asyncio.Task(stream.readexactly(n), loop=self.loop) read_task = self.loop.create_task(stream.readexactly(n))
def cb(): def cb():
stream._feed_data(self.DATA) stream._feed_data(self.DATA)
@ -652,8 +652,8 @@ class StreamTests(test_utils.TestCase):
async def set_err(): async def set_err():
stream._set_exception(ValueError()) stream._set_exception(ValueError())
t1 = asyncio.Task(stream.readline(), loop=self.loop) t1 = self.loop.create_task(stream.readline())
t2 = asyncio.Task(set_err(), loop=self.loop) t2 = self.loop.create_task(set_err())
self.loop.run_until_complete(asyncio.wait([t1, t2])) self.loop.run_until_complete(asyncio.wait([t1, t2]))
@ -664,7 +664,7 @@ class StreamTests(test_utils.TestCase):
loop=self.loop, loop=self.loop,
_asyncio_internal=True) _asyncio_internal=True)
t = asyncio.Task(stream.readline(), loop=self.loop) t = self.loop.create_task(stream.readline())
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
t.cancel() t.cancel()
test_utils.run_briefly(self.loop) test_utils.run_briefly(self.loop)
@ -735,8 +735,7 @@ class StreamTests(test_utils.TestCase):
server = MyServer(self.loop) server = MyServer(self.loop)
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
addr = server.start() addr = server.start()
msg = self.loop.run_until_complete(asyncio.Task(client(addr), msg = self.loop.run_until_complete(self.loop.create_task(client(addr)))
loop=self.loop))
server.stop() server.stop()
self.assertEqual(msg, b"hello world!\n") self.assertEqual(msg, b"hello world!\n")
@ -744,8 +743,7 @@ class StreamTests(test_utils.TestCase):
server = MyServer(self.loop) server = MyServer(self.loop)
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
addr = server.start_callback() addr = server.start_callback()
msg = self.loop.run_until_complete(asyncio.Task(client(addr), msg = self.loop.run_until_complete(self.loop.create_task(client(addr)))
loop=self.loop))
server.stop() server.stop()
self.assertEqual(msg, b"hello world!\n") self.assertEqual(msg, b"hello world!\n")
@ -810,8 +808,8 @@ class StreamTests(test_utils.TestCase):
server = MyServer(self.loop, path) server = MyServer(self.loop, path)
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
server.start() server.start()
msg = self.loop.run_until_complete(asyncio.Task(client(path), msg = self.loop.run_until_complete(
loop=self.loop)) self.loop.create_task(client(path)))
server.stop() server.stop()
self.assertEqual(msg, b"hello world!\n") self.assertEqual(msg, b"hello world!\n")
@ -820,8 +818,8 @@ class StreamTests(test_utils.TestCase):
server = MyServer(self.loop, path) server = MyServer(self.loop, path)
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
server.start_callback() server.start_callback()
msg = self.loop.run_until_complete(asyncio.Task(client(path), msg = self.loop.run_until_complete(
loop=self.loop)) self.loop.create_task(client(path)))
server.stop() server.stop()
self.assertEqual(msg, b"hello world!\n") self.assertEqual(msg, b"hello world!\n")
@ -1016,7 +1014,7 @@ os.close(fd)
stream = asyncio.Stream(mode=asyncio.StreamMode.READ, stream = asyncio.Stream(mode=asyncio.StreamMode.READ,
loop=self.loop, loop=self.loop,
_asyncio_internal=True) _asyncio_internal=True)
stream._waiter = asyncio.Future(loop=self.loop) stream._waiter = self.loop.create_future()
self.assertRegex( self.assertRegex(
repr(stream), repr(stream),
r"<Stream .+ waiter=<Future pending[\S ]*>>") r"<Stream .+ waiter=<Future pending[\S ]*>>")

View File

@ -43,7 +43,6 @@ class SubprocessTransportTests(test_utils.TestCase):
self.loop = self.new_test_loop() self.loop = self.new_test_loop()
self.set_event_loop(self.loop) self.set_event_loop(self.loop)
def create_transport(self, waiter=None): def create_transport(self, waiter=None):
protocol = mock.Mock() protocol = mock.Mock()
protocol.connection_made._is_coroutine = False protocol.connection_made._is_coroutine = False
@ -54,7 +53,7 @@ class SubprocessTransportTests(test_utils.TestCase):
return (transport, protocol) return (transport, protocol)
def test_proc_exited(self): def test_proc_exited(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
transport, protocol = self.create_transport(waiter) transport, protocol = self.create_transport(waiter)
transport._process_exited(6) transport._process_exited(6)
self.loop.run_until_complete(waiter) self.loop.run_until_complete(waiter)
@ -80,7 +79,7 @@ class SubprocessTransportTests(test_utils.TestCase):
transport.close() transport.close()
def test_subprocess_repr(self): def test_subprocess_repr(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
transport, protocol = self.create_transport(waiter) transport, protocol = self.create_transport(waiter)
transport._process_exited(6) transport._process_exited(6)
self.loop.run_until_complete(waiter) self.loop.run_until_complete(waiter)

View File

@ -2660,7 +2660,7 @@ class CTask_Future_Tests(test_utils.TestCase):
try: try:
fut = Fut(loop=self.loop) fut = Fut(loop=self.loop)
self.loop.call_later(0.1, fut.set_result, 1) self.loop.call_later(0.1, fut.set_result, 1)
task = asyncio.Task(coro(), loop=self.loop) task = self.loop.create_task(coro())
res = self.loop.run_until_complete(task) res = self.loop.run_until_complete(task)
finally: finally:
self.loop.close() self.loop.close()
@ -2878,7 +2878,7 @@ class GatherTestsBase:
test_utils.run_briefly(loop) test_utils.run_briefly(loop)
def _check_success(self, **kwargs): def _check_success(self, **kwargs):
a, b, c = [asyncio.Future(loop=self.one_loop) for i in range(3)] a, b, c = [self.one_loop.create_future() for i in range(3)]
fut = asyncio.gather(*self.wrap_futures(a, b, c), **kwargs) fut = asyncio.gather(*self.wrap_futures(a, b, c), **kwargs)
cb = test_utils.MockCallback() cb = test_utils.MockCallback()
fut.add_done_callback(cb) fut.add_done_callback(cb)
@ -2900,7 +2900,7 @@ class GatherTestsBase:
self._check_success(return_exceptions=True) self._check_success(return_exceptions=True)
def test_one_exception(self): def test_one_exception(self):
a, b, c, d, e = [asyncio.Future(loop=self.one_loop) for i in range(5)] a, b, c, d, e = [self.one_loop.create_future() for i in range(5)]
fut = asyncio.gather(*self.wrap_futures(a, b, c, d, e)) fut = asyncio.gather(*self.wrap_futures(a, b, c, d, e))
cb = test_utils.MockCallback() cb = test_utils.MockCallback()
fut.add_done_callback(cb) fut.add_done_callback(cb)
@ -2918,7 +2918,7 @@ class GatherTestsBase:
e.exception() e.exception()
def test_return_exceptions(self): def test_return_exceptions(self):
a, b, c, d = [asyncio.Future(loop=self.one_loop) for i in range(4)] a, b, c, d = [self.one_loop.create_future() for i in range(4)]
fut = asyncio.gather(*self.wrap_futures(a, b, c, d), fut = asyncio.gather(*self.wrap_futures(a, b, c, d),
return_exceptions=True) return_exceptions=True)
cb = test_utils.MockCallback() cb = test_utils.MockCallback()
@ -2991,15 +2991,15 @@ class FutureGatherTests(GatherTestsBase, test_utils.TestCase):
self._check_empty_sequence(iter("")) self._check_empty_sequence(iter(""))
def test_constructor_heterogenous_futures(self): def test_constructor_heterogenous_futures(self):
fut1 = asyncio.Future(loop=self.one_loop) fut1 = self.one_loop.create_future()
fut2 = asyncio.Future(loop=self.other_loop) fut2 = self.other_loop.create_future()
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
asyncio.gather(fut1, fut2) asyncio.gather(fut1, fut2)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
asyncio.gather(fut1, loop=self.other_loop) asyncio.gather(fut1, loop=self.other_loop)
def test_constructor_homogenous_futures(self): def test_constructor_homogenous_futures(self):
children = [asyncio.Future(loop=self.other_loop) for i in range(3)] children = [self.other_loop.create_future() for i in range(3)]
fut = asyncio.gather(*children) fut = asyncio.gather(*children)
self.assertIs(fut._loop, self.other_loop) self.assertIs(fut._loop, self.other_loop)
self._run_loop(self.other_loop) self._run_loop(self.other_loop)
@ -3010,7 +3010,7 @@ class FutureGatherTests(GatherTestsBase, test_utils.TestCase):
self.assertFalse(fut.done()) self.assertFalse(fut.done())
def test_one_cancellation(self): def test_one_cancellation(self):
a, b, c, d, e = [asyncio.Future(loop=self.one_loop) for i in range(5)] a, b, c, d, e = [self.one_loop.create_future() for i in range(5)]
fut = asyncio.gather(a, b, c, d, e) fut = asyncio.gather(a, b, c, d, e)
cb = test_utils.MockCallback() cb = test_utils.MockCallback()
fut.add_done_callback(cb) fut.add_done_callback(cb)
@ -3028,7 +3028,7 @@ class FutureGatherTests(GatherTestsBase, test_utils.TestCase):
e.exception() e.exception()
def test_result_exception_one_cancellation(self): def test_result_exception_one_cancellation(self):
a, b, c, d, e, f = [asyncio.Future(loop=self.one_loop) a, b, c, d, e, f = [self.one_loop.create_future()
for i in range(6)] for i in range(6)]
fut = asyncio.gather(a, b, c, d, e, f, return_exceptions=True) fut = asyncio.gather(a, b, c, d, e, f, return_exceptions=True)
cb = test_utils.MockCallback() cb = test_utils.MockCallback()
@ -3094,7 +3094,7 @@ class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
def test_cancellation_broadcast(self): def test_cancellation_broadcast(self):
# Cancelling outer() cancels all children. # Cancelling outer() cancels all children.
proof = 0 proof = 0
waiter = asyncio.Future(loop=self.one_loop) waiter = self.one_loop.create_future()
async def inner(): async def inner():
nonlocal proof nonlocal proof
@ -3130,8 +3130,8 @@ class CoroutineGatherTests(GatherTestsBase, test_utils.TestCase):
await f await f
raise RuntimeError('should not be ignored') raise RuntimeError('should not be ignored')
a = asyncio.Future(loop=self.one_loop) a = self.one_loop.create_future()
b = asyncio.Future(loop=self.one_loop) b = self.one_loop.create_future()
async def outer(): async def outer():
await asyncio.gather(inner(a), inner(b), loop=self.one_loop) await asyncio.gather(inner(a), inner(b), loop=self.one_loop)

View File

@ -669,7 +669,7 @@ class UnixReadPipeTransportTests(test_utils.TestCase):
return transport return transport
def test_ctor(self): def test_ctor(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
tr = self.read_pipe_transport(waiter=waiter) tr = self.read_pipe_transport(waiter=waiter)
self.loop.run_until_complete(waiter) self.loop.run_until_complete(waiter)
@ -819,7 +819,7 @@ class UnixWritePipeTransportTests(test_utils.TestCase):
return transport return transport
def test_ctor(self): def test_ctor(self):
waiter = asyncio.Future(loop=self.loop) waiter = self.loop.create_future()
tr = self.write_pipe_transport(waiter=waiter) tr = self.write_pipe_transport(waiter=waiter)
self.loop.run_until_complete(waiter) self.loop.run_until_complete(waiter)