Skip to content

Commit c73701d

Browse files
committed
asyncio: Refactor tests: add a base TestCase class
1 parent d6f02fc commit c73701d

13 files changed

+145
-219
lines changed

Lib/asyncio/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tempfile
1212
import threading
1313
import time
14+
import unittest
1415
from unittest import mock
1516

1617
from http.server import HTTPServer
@@ -379,3 +380,20 @@ def get_function_source(func):
379380
if source is None:
380381
raise ValueError("unable to get the source of %r" % (func,))
381382
return source
383+
384+
385+
class TestCase(unittest.TestCase):
386+
def set_event_loop(self, loop, *, cleanup=True):
387+
assert loop is not None
388+
# ensure that the event loop is passed explicitly in asyncio
389+
events.set_event_loop(None)
390+
if cleanup:
391+
self.addCleanup(loop.close)
392+
393+
def new_test_loop(self, gen=None):
394+
loop = TestLoop(gen)
395+
self.set_event_loop(loop)
396+
return loop
397+
398+
def tearDown(self):
399+
events.set_event_loop(None)

Lib/test/test_asyncio/test_base_events.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
PY34 = sys.version_info >= (3, 4)
2020

2121

22-
class BaseEventLoopTests(unittest.TestCase):
22+
class BaseEventLoopTests(test_utils.TestCase):
2323

2424
def setUp(self):
2525
self.loop = base_events.BaseEventLoop()
2626
self.loop._selector = mock.Mock()
27-
asyncio.set_event_loop(None)
27+
self.set_event_loop(self.loop)
2828

2929
def test_not_implemented(self):
3030
m = mock.Mock()
@@ -548,14 +548,11 @@ def connection_lost(self, exc):
548548
self.done.set_result(None)
549549

550550

551-
class BaseEventLoopWithSelectorTests(unittest.TestCase):
551+
class BaseEventLoopWithSelectorTests(test_utils.TestCase):
552552

553553
def setUp(self):
554554
self.loop = asyncio.new_event_loop()
555-
asyncio.set_event_loop(None)
556-
557-
def tearDown(self):
558-
self.loop.close()
555+
self.set_event_loop(self.loop)
559556

560557
@mock.patch('asyncio.base_events.socket')
561558
def test_create_connection_multiple_errors(self, m_socket):

Lib/test/test_asyncio/test_events.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class EventLoopTestsMixin:
224224
def setUp(self):
225225
super().setUp()
226226
self.loop = self.create_event_loop()
227-
asyncio.set_event_loop(None)
227+
self.set_event_loop(self.loop)
228228

229229
def tearDown(self):
230230
# just in case if we have transport close callbacks
@@ -1629,14 +1629,14 @@ def connect(cmd=None, **kwds):
16291629

16301630
if sys.platform == 'win32':
16311631

1632-
class SelectEventLoopTests(EventLoopTestsMixin, unittest.TestCase):
1632+
class SelectEventLoopTests(EventLoopTestsMixin, test_utils.TestCase):
16331633

16341634
def create_event_loop(self):
16351635
return asyncio.SelectorEventLoop()
16361636

16371637
class ProactorEventLoopTests(EventLoopTestsMixin,
16381638
SubprocessTestsMixin,
1639-
unittest.TestCase):
1639+
test_utils.TestCase):
16401640

16411641
def create_event_loop(self):
16421642
return asyncio.ProactorEventLoop()
@@ -1691,7 +1691,7 @@ def tearDown(self):
16911691
if hasattr(selectors, 'KqueueSelector'):
16921692
class KqueueEventLoopTests(UnixEventLoopTestsMixin,
16931693
SubprocessTestsMixin,
1694-
unittest.TestCase):
1694+
test_utils.TestCase):
16951695

16961696
def create_event_loop(self):
16971697
return asyncio.SelectorEventLoop(
@@ -1716,23 +1716,23 @@ def test_write_pty(self):
17161716
if hasattr(selectors, 'EpollSelector'):
17171717
class EPollEventLoopTests(UnixEventLoopTestsMixin,
17181718
SubprocessTestsMixin,
1719-
unittest.TestCase):
1719+
test_utils.TestCase):
17201720

17211721
def create_event_loop(self):
17221722
return asyncio.SelectorEventLoop(selectors.EpollSelector())
17231723

17241724
if hasattr(selectors, 'PollSelector'):
17251725
class PollEventLoopTests(UnixEventLoopTestsMixin,
17261726
SubprocessTestsMixin,
1727-
unittest.TestCase):
1727+
test_utils.TestCase):
17281728

17291729
def create_event_loop(self):
17301730
return asyncio.SelectorEventLoop(selectors.PollSelector())
17311731

17321732
# Should always exist.
17331733
class SelectEventLoopTests(UnixEventLoopTestsMixin,
17341734
SubprocessTestsMixin,
1735-
unittest.TestCase):
1735+
test_utils.TestCase):
17361736

17371737
def create_event_loop(self):
17381738
return asyncio.SelectorEventLoop(selectors.SelectSelector())

Lib/test/test_asyncio/test_futures.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ def _fakefunc(f):
1313
return f
1414

1515

16-
class FutureTests(unittest.TestCase):
16+
class FutureTests(test_utils.TestCase):
1717

1818
def setUp(self):
19-
self.loop = test_utils.TestLoop()
20-
asyncio.set_event_loop(None)
21-
22-
def tearDown(self):
23-
self.loop.close()
19+
self.loop = self.new_test_loop()
2420

2521
def test_initial_state(self):
2622
f = asyncio.Future(loop=self.loop)
@@ -30,12 +26,9 @@ def test_initial_state(self):
3026
self.assertTrue(f.cancelled())
3127

3228
def test_init_constructor_default_loop(self):
33-
try:
34-
asyncio.set_event_loop(self.loop)
35-
f = asyncio.Future()
36-
self.assertIs(f._loop, self.loop)
37-
finally:
38-
asyncio.set_event_loop(None)
29+
asyncio.set_event_loop(self.loop)
30+
f = asyncio.Future()
31+
self.assertIs(f._loop, self.loop)
3932

4033
def test_constructor_positional(self):
4134
# Make sure Future doesn't accept a positional argument
@@ -264,14 +257,10 @@ def test_wrap_future_cancel2(self):
264257
self.assertTrue(f2.cancelled())
265258

266259

267-
class FutureDoneCallbackTests(unittest.TestCase):
260+
class FutureDoneCallbackTests(test_utils.TestCase):
268261

269262
def setUp(self):
270-
self.loop = test_utils.TestLoop()
271-
asyncio.set_event_loop(None)
272-
273-
def tearDown(self):
274-
self.loop.close()
263+
self.loop = self.new_test_loop()
275264

276265
def run_briefly(self):
277266
test_utils.run_briefly(self.loop)

Lib/test/test_asyncio/test_locks.py

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717
RGX_REPR = re.compile(STR_RGX_REPR)
1818

1919

20-
class LockTests(unittest.TestCase):
20+
class LockTests(test_utils.TestCase):
2121

2222
def setUp(self):
23-
self.loop = test_utils.TestLoop()
24-
asyncio.set_event_loop(None)
25-
26-
def tearDown(self):
27-
self.loop.close()
23+
self.loop = self.new_test_loop()
2824

2925
def test_ctor_loop(self):
3026
loop = mock.Mock()
@@ -35,12 +31,9 @@ def test_ctor_loop(self):
3531
self.assertIs(lock._loop, self.loop)
3632

3733
def test_ctor_noloop(self):
38-
try:
39-
asyncio.set_event_loop(self.loop)
40-
lock = asyncio.Lock()
41-
self.assertIs(lock._loop, self.loop)
42-
finally:
43-
asyncio.set_event_loop(None)
34+
asyncio.set_event_loop(self.loop)
35+
lock = asyncio.Lock()
36+
self.assertIs(lock._loop, self.loop)
4437

4538
def test_repr(self):
4639
lock = asyncio.Lock(loop=self.loop)
@@ -240,14 +233,10 @@ def test_context_manager_no_yield(self):
240233
self.assertFalse(lock.locked())
241234

242235

243-
class EventTests(unittest.TestCase):
236+
class EventTests(test_utils.TestCase):
244237

245238
def setUp(self):
246-
self.loop = test_utils.TestLoop()
247-
asyncio.set_event_loop(None)
248-
249-
def tearDown(self):
250-
self.loop.close()
239+
self.loop = self.new_test_loop()
251240

252241
def test_ctor_loop(self):
253242
loop = mock.Mock()
@@ -258,12 +247,9 @@ def test_ctor_loop(self):
258247
self.assertIs(ev._loop, self.loop)
259248

260249
def test_ctor_noloop(self):
261-
try:
262-
asyncio.set_event_loop(self.loop)
263-
ev = asyncio.Event()
264-
self.assertIs(ev._loop, self.loop)
265-
finally:
266-
asyncio.set_event_loop(None)
250+
asyncio.set_event_loop(self.loop)
251+
ev = asyncio.Event()
252+
self.assertIs(ev._loop, self.loop)
267253

268254
def test_repr(self):
269255
ev = asyncio.Event(loop=self.loop)
@@ -376,14 +362,10 @@ def c1(result):
376362
self.assertTrue(t.result())
377363

378364

379-
class ConditionTests(unittest.TestCase):
365+
class ConditionTests(test_utils.TestCase):
380366

381367
def setUp(self):
382-
self.loop = test_utils.TestLoop()
383-
asyncio.set_event_loop(None)
384-
385-
def tearDown(self):
386-
self.loop.close()
368+
self.loop = self.new_test_loop()
387369

388370
def test_ctor_loop(self):
389371
loop = mock.Mock()
@@ -394,12 +376,9 @@ def test_ctor_loop(self):
394376
self.assertIs(cond._loop, self.loop)
395377

396378
def test_ctor_noloop(self):
397-
try:
398-
asyncio.set_event_loop(self.loop)
399-
cond = asyncio.Condition()
400-
self.assertIs(cond._loop, self.loop)
401-
finally:
402-
asyncio.set_event_loop(None)
379+
asyncio.set_event_loop(self.loop)
380+
cond = asyncio.Condition()
381+
self.assertIs(cond._loop, self.loop)
403382

404383
def test_wait(self):
405384
cond = asyncio.Condition(loop=self.loop)
@@ -678,14 +657,10 @@ def test_context_manager_no_yield(self):
678657
self.assertFalse(cond.locked())
679658

680659

681-
class SemaphoreTests(unittest.TestCase):
660+
class SemaphoreTests(test_utils.TestCase):
682661

683662
def setUp(self):
684-
self.loop = test_utils.TestLoop()
685-
asyncio.set_event_loop(None)
686-
687-
def tearDown(self):
688-
self.loop.close()
663+
self.loop = self.new_test_loop()
689664

690665
def test_ctor_loop(self):
691666
loop = mock.Mock()
@@ -696,12 +671,9 @@ def test_ctor_loop(self):
696671
self.assertIs(sem._loop, self.loop)
697672

698673
def test_ctor_noloop(self):
699-
try:
700-
asyncio.set_event_loop(self.loop)
701-
sem = asyncio.Semaphore()
702-
self.assertIs(sem._loop, self.loop)
703-
finally:
704-
asyncio.set_event_loop(None)
674+
asyncio.set_event_loop(self.loop)
675+
sem = asyncio.Semaphore()
676+
self.assertIs(sem._loop, self.loop)
705677

706678
def test_initial_value_zero(self):
707679
sem = asyncio.Semaphore(0, loop=self.loop)

Lib/test/test_asyncio/test_proactor_events.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
from asyncio import test_utils
1313

1414

15-
class ProactorSocketTransportTests(unittest.TestCase):
15+
class ProactorSocketTransportTests(test_utils.TestCase):
1616

1717
def setUp(self):
18-
self.loop = test_utils.TestLoop()
18+
self.loop = self.new_test_loop()
1919
self.proactor = mock.Mock()
2020
self.loop._proactor = self.proactor
2121
self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
@@ -343,7 +343,7 @@ def test_pause_resume_reading(self):
343343
tr.close()
344344

345345

346-
class BaseProactorEventLoopTests(unittest.TestCase):
346+
class BaseProactorEventLoopTests(test_utils.TestCase):
347347

348348
def setUp(self):
349349
self.sock = mock.Mock(socket.socket)
@@ -356,6 +356,7 @@ def _socketpair(s):
356356
return (self.ssock, self.csock)
357357

358358
self.loop = EventLoop(self.proactor)
359+
self.set_event_loop(self.loop, cleanup=False)
359360

360361
@mock.patch.object(BaseProactorEventLoop, 'call_soon')
361362
@mock.patch.object(BaseProactorEventLoop, '_socketpair')

0 commit comments

Comments
 (0)