diff --git a/Doc/library/test.rst b/Doc/library/test.rst index e6786a441c66f8..a2e5bdc64dfe61 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -490,7 +490,7 @@ The :mod:`!test.support` module defines the following functions: Return ``True`` if *resource* is enabled and available. The list of available resources is only set when :mod:`test.regrtest` is executing the - tests. + tests. Otherwise, all resources except ``settime`` are enabled. .. function:: get_resource_value(resource) diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index 720b073e460127..85a6e9169369c5 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -122,6 +122,11 @@ walltime - Long running but not CPU-bound tests. + settime - Allow tests to change the system clock. Must be enabled + explicitly, even when using `-u all`. Run these tests + alone, without -j: clock changes affect other processes + and a killed worker cannot restore the clock. + subprocess Run all tests for the subprocess module. urlfetch - It is okay to download files required on testing. diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 28e6d904fa28aa..c62ac9034000af 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -51,7 +51,9 @@ # - tzdata: while needed to validate fully test_datetime, it makes # test_datetime too slow (15-20 min on some buildbots) and so is disabled by # default (see bpo-30822). -RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile', 'tzdata', 'xpickle', 'wantobjects') +# - settime: changing the system clock affects other processes. +RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile', 'settime', 'tzdata', + 'xpickle', 'wantobjects') # Types for types hints diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 2cfe4b7f20e101..4e285dc5ee827b 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -324,16 +324,15 @@ def is_resource_enabled(resource): """Test whether a resource is enabled. Known resources are set by regrtest.py. If not running under regrtest.py, - all resources are assumed enabled unless use_resources has been set. + all resources except settime are assumed enabled unless use_resources + has been set. """ - return use_resources is None or resource in use_resources + if use_resources is None: + return resource != 'settime' + return resource in use_resources def get_resource_value(resource): - """Test whether a resource is enabled. - - Known resources are set by regrtest.py. If not running under regrtest.py, - all resources are assumed enabled unless use_resources has been set. - """ + """Return the configured resource value, or None if no value was set.""" if use_resources is None: return None return use_resources.get(resource) diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index cd2cd8a21e8ea7..d5205ed3dce418 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -22,6 +22,7 @@ import tempfile import threading import textwrap +import time import types import unittest import unittest.mock @@ -340,6 +341,11 @@ def test_use(self): self.checkError([opt, 'none=0'], 'invalid resource: none=0') self.checkError([opt, 'all,-gui=0'], 'invalid resource: -gui=0') + def test_settime_requires_opt_in(self): + self.assertNotIn('settime', self.parse_args([]).use_resources) + self.assertNotIn('settime', self.parse_args(['-u', 'all']).use_resources) + self.assertIn('settime', self.parse_args(['-u', 'settime']).use_resources) + def test_memlimit(self): for opt in '-M', '--memlimit': with self.subTest(opt=opt): @@ -902,6 +908,68 @@ def run_python(self, args, isolated=True, **kw): class CheckActualTests(BaseTestCase): + @unittest.skipUnless(hasattr(time, 'clock_settime'), 'requires clock_settime') + @support.thread_unsafe('changes the enabled test resources') + def test_system_clock_writes(self): + from test.libregrtest.single import _parallelize_tests + from test.test_time import TimeTestCase + + names = ('test_clock_settime', 'test_monotonic_settime') + # None: the tests are run directly by unittest, outside regrtest. + cases = ((None, 0), ({}, 0), ({'settime': None}, 3), ({}, 0)) + for resources, expected in cases: + with (self.subTest(resources=resources), + unittest.mock.patch.object(support, 'use_resources', resources), + unittest.mock.patch.object(time, 'clock_settime') as setter): + suite = unittest.TestSuite(TimeTestCase(name) for name in names) + _parallelize_tests(suite, 2) + result = unittest.TestResult() + suite.run(result) + self.assertTrue(result.wasSuccessful(), result.errors + result.failures) + self.assertEqual(setter.call_count, expected) + + @unittest.skipUnless(hasattr(time, 'clock_settime'), 'requires clock_settime') + @support.thread_unsafe('changes the enabled test resources') + def test_system_clock_restored(self): + from test.test_time import TimeTestCase + + # Each step of clock_settime(): (exception raised, clock changed). + cases = ( + ('success', [(None, True), (None, True)], None), + # A signal can arrive after the clock changed, before the setter + # returns. + ('interrupted', [(KeyboardInterrupt, True), (None, True)], + KeyboardInterrupt), + ('denied', [(PermissionError, False)], unittest.SkipTest), + ('failed', [(OSError, False)], OSError), + ) + for name, steps, expected in cases: + clock = 7200 + writes = 0 + + def clock_settime(clock_id, value): + nonlocal clock, writes + self.assertLess(writes, len(steps), 'unexpected clock write') + exc, changed = steps[writes] + writes += 1 + if changed: + clock = value + if exc is not None: + raise exc + + with (self.subTest(name), + unittest.mock.patch.object(support, 'use_resources', {'settime': None}), + unittest.mock.patch.object(time, 'clock_gettime', lambda clock_id: clock), + unittest.mock.patch.object(time, 'clock_settime', clock_settime)): + test = TimeTestCase('test_monotonic_settime') + if expected is None: + test.test_monotonic_settime() + else: + with self.assertRaises(expected): + test.test_monotonic_settime() + self.assertEqual(writes, len(steps)) + self.assertEqual(clock, 7200) + def test_finds_expected_number_of_tests(self): """ Check that regrtest appears to find the expected set of tests. diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 861076357e0019..de500c31cd3f49 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -143,16 +143,21 @@ def test_clock_getres(self): @unittest.skipUnless(hasattr(time, 'clock_settime'), 'need time.clock_settime()') + @support.thread_unsafe('sets the system clock') def test_clock_settime(self): + support.requires('settime') t = time.clock_gettime(time.CLOCK_REALTIME) try: time.clock_settime(time.CLOCK_REALTIME, t) - except PermissionError: - pass + except PermissionError as err: + self.skipTest(err) - if hasattr(time, 'CLOCK_MONOTONIC'): - self.assertRaises(OSError, - time.clock_settime, time.CLOCK_MONOTONIC, 0) + @unittest.skipUnless(hasattr(time, 'clock_settime') and + hasattr(time, 'CLOCK_MONOTONIC'), + 'need clock_settime() and CLOCK_MONOTONIC') + def test_clock_settime_monotonic(self): + with self.assertRaises(OSError): + time.clock_settime(time.CLOCK_MONOTONIC, 0) def test_large_timestamp_year(self): # 2147483648-06-29 00:00:00 UTC, away from local year boundaries. @@ -703,16 +708,26 @@ def test_thread_time(self): @unittest.skipUnless(hasattr(time, 'clock_settime'), 'need time.clock_settime') + @support.thread_unsafe('sets the system clock') def test_monotonic_settime(self): + support.requires('settime') t1 = time.monotonic() realtime = time.clock_gettime(time.CLOCK_REALTIME) # jump backward with an offset of 1 hour + restore_clock = True try: - time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) - except PermissionError as err: - self.skipTest(err) - t2 = time.monotonic() - time.clock_settime(time.CLOCK_REALTIME, realtime) + try: + time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) + except OSError as err: + # A failed system call did not change the clock. + restore_clock = False + if isinstance(err, PermissionError): + self.skipTest(err) + raise + t2 = time.monotonic() + finally: + if restore_clock: + time.clock_settime(time.CLOCK_REALTIME, realtime) # monotonic must not be affected by system clock updates self.assertGreaterEqual(t2, t1) diff --git a/Misc/NEWS.d/next/Tests/2026-11-20-08-50-55.gh-issue-162280.zWJny3.rst b/Misc/NEWS.d/next/Tests/2026-11-20-08-50-55.gh-issue-162280.zWJny3.rst new file mode 100644 index 00000000000000..f1a92d264eb303 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-20-08-50-55.gh-issue-162280.zWJny3.rst @@ -0,0 +1,3 @@ +Tests in :mod:`!test.test_time` that change the system clock now require the +``settime`` resource, which is not enabled by ``-u all``. Moving the clock +back could make tests running in parallel fail. Patch by Gabriel Lobo.