Skip to content

Commit

Permalink
add support.set_event_loop_policy for setting the event loop policy i…
Browse files Browse the repository at this point in the history
…n tests
  • Loading branch information
graingert committed Oct 11, 2023
1 parent 43cbdb2 commit 339445e
Show file tree
Hide file tree
Showing 44 changed files with 90 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Lib/test/libregrtest/save_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_asyncio_events__event_loop_policy(self):
return support.maybe_get_event_loop_policy()
def restore_asyncio_events__event_loop_policy(self, policy):
asyncio = self.get_module('asyncio')
asyncio.set_event_loop_policy(policy)
support.set_event_loop_policy(policy)

def get_sys_argv(self):
return id(sys.argv), sys.argv, sys.argv[:]
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,13 @@ def maybe_get_event_loop_policy():
import asyncio.events
return asyncio.events._event_loop_policy

def set_event_loop_policy(policy):
"""Set the global event loop policy ignoring deprecation warnings"""
import asyncio
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
asyncio.set_event_loop_policy(policy)

# Helpers for testing hashing.
NHASHBITS = sys.hash_info.width # number of bits in hash() result
assert NHASHBITS in (32, 64)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def setUp(self):
def tearDown(self):
self.loop.close()
self.loop = None
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)

def check_async_iterator_anext(self, ait_class):
with self.subTest(anext="pure-Python"):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


def mock_socket_module():
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_buffered_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import unittest

from test.test_asyncio import functional as func_tests
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class ReceiveStuffProto(asyncio.BufferedProtocol):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_context.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
import decimal
import unittest
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


@unittest.skipUnless(decimal.HAVE_CONTEXTVAR, "decimal is built with a thread-local context")
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_eager_task_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
from asyncio import tasks
from test.test_asyncio import utils as test_utils
from test.support.script_helper import assert_python_ok
from test import support

MOCK_ANY = mock.ANY


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class EagerTaskFactoryLoopTests:
Expand Down
17 changes: 10 additions & 7 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


def broken_unix_getsockname():
Expand Down Expand Up @@ -2684,8 +2684,9 @@ def test_get_event_loop_policy(self):
self.assertIs(policy, asyncio.get_event_loop_policy())

def test_set_event_loop_policy(self):
self.assertRaises(
TypeError, asyncio.set_event_loop_policy, object())
with self.assertWarns(DeprecationWarning):
self.assertRaises(
TypeError, asyncio.set_event_loop_policy, object())

old_policy = asyncio.get_event_loop_policy()

Expand Down Expand Up @@ -2790,7 +2791,8 @@ def get_event_loop(self):

old_policy = asyncio.get_event_loop_policy()
try:
asyncio.set_event_loop_policy(Policy())
with self.assertWarns(DeprecationWarning):
asyncio.set_event_loop_policy(Policy())
loop = asyncio.new_event_loop()

with self.assertRaises(TestError):
Expand Down Expand Up @@ -2818,7 +2820,7 @@ async def func():
asyncio.get_event_loop()

finally:
asyncio.set_event_loop_policy(old_policy)
support.set_event_loop_policy(old_policy)
if loop is not None:
loop.close()

Expand All @@ -2830,7 +2832,8 @@ async def func():
def test_get_event_loop_returns_running_loop2(self):
old_policy = asyncio.get_event_loop_policy()
try:
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
with self.assertWarns(DeprecationWarning):
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

Expand Down Expand Up @@ -2861,7 +2864,7 @@ async def func():
asyncio.get_event_loop()

finally:
asyncio.set_event_loop_policy(old_policy)
support.set_event_loop_policy(old_policy)
if loop is not None:
loop.close()

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


def _fakefunc(f):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_futures2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import traceback
import unittest
from asyncio import tasks
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class FutureTests:
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import asyncio
import collections
from test import support

STR_RGX_REPR = (
r'^<(?P<class>.*?) object at (?P<address>.*?)'
Expand All @@ -20,7 +21,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class LockTests(unittest.IsolatedAsyncioTestCase):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_pep492.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

import asyncio
from test.test_asyncio import utils as test_utils
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


# Test that asyncio.iscoroutine() uses collections.abc.Coroutine
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
from test.support import os_helper
from test.support import socket_helper
from test.test_asyncio import utils as test_utils
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


def close_transport(transport):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from unittest import mock

import asyncio
from test import support


def tearDownModule():
# not needed for the test file but added for uniformness with all other
# asyncio test files for the sake of unified cleanup
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class ProtocolsAbsTests(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import asyncio
import unittest
from types import GenericAlias
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class QueueBasicTests(unittest.IsolatedAsyncioTestCase):
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_asyncio/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import threading
import unittest
from test.test_asyncio import utils as test_utils
from test import support
from unittest import mock
from unittest.mock import patch


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


def interrupt_self():
Expand Down Expand Up @@ -60,15 +61,15 @@ def setUp(self):
super().setUp()

policy = TestPolicy(self.new_loop)
asyncio.set_event_loop_policy(policy)
support.set_event_loop_policy(policy)

def tearDown(self):
policy = asyncio.get_event_loop_policy()
if policy.loop is not None:
self.assertTrue(policy.loop.is_closed())
self.assertTrue(policy.loop.shutdown_ag_run)

asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)
super().tearDown()


Expand Down Expand Up @@ -258,7 +259,7 @@ def new_event_loop():
loop.set_task_factory(Task)
return loop

asyncio.set_event_loop_policy(TestPolicy(new_event_loop))
support.set_event_loop_policy(TestPolicy(new_event_loop))
with self.assertRaises(asyncio.CancelledError):
asyncio.run(main())

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
_SelectorSocketTransport,
_SelectorTransport)
from test.test_asyncio import utils as test_utils
from test import support

MOCK_ANY = mock.ANY


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class TestBaseSelectorEventLoop(BaseSelectorEventLoop):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_sendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class MySendfileProto(asyncio.Protocol):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from test.support import socket_helper
from test.test_asyncio import utils as test_utils
from test.test_asyncio import functional as func_tests
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class BaseStartServer(func_tests.FunctionalTestCaseMixin):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_sock_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class MyProto(asyncio.Protocol):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class MyBaseProto(asyncio.Protocol):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_sslproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


@unittest.skipIf(ssl is None, 'No ssl module')
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import asyncio
from test.test_asyncio import utils as test_utils
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class StreamTests(test_utils.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class TestSubprocessTransport(base_subprocess.BaseSubprocessTransport):
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import contextlib
from asyncio import taskgroups
import unittest
from test import support


# To prevent a warning "test altered the execution environment"
def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class MyExc(Exception):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


async def coroutine_function():
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_asyncio/test_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from contextvars import ContextVar
from unittest import mock
from test import support


def tearDownModule():
asyncio.set_event_loop_policy(None)
support.set_event_loop_policy(None)


class ToThreadTests(unittest.IsolatedAsyncioTestCase):
Expand Down
Loading

0 comments on commit 339445e

Please sign in to comment.