Skip to content

Commit 41a5289

Browse files
committed
fix(tests): check settime at runtime and serialize clock writes across test threads
Refs #162280
1 parent f571357 commit 41a5289

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎Lib/test/libregrtest/cmdline.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@
123123
walltime - Long running but not CPU-bound tests.
124124
125125
settime - Allow tests to change the system clock. Must be enabled
126-
explicitly, even when using `-u all`.
126+
explicitly, even when using `-u all`. Run these tests
127+
in isolation as clock changes affect other processes.
127128
128129
subprocess Run all tests for the subprocess module.
129130

‎Lib/test/test_regrtest.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import tempfile
2323
import threading
2424
import textwrap
25+
import time
2526
import types
2627
import unittest
2728
import unittest.mock
@@ -907,6 +908,24 @@ def run_python(self, args, isolated=True, **kw):
907908

908909

909910
class CheckActualTests(BaseTestCase):
911+
@unittest.skipUnless(hasattr(time, 'clock_settime'), 'requires clock_settime')
912+
@support.thread_unsafe('changes the enabled test resources')
913+
def test_system_clock_writes(self):
914+
from test.libregrtest.single import _parallelize_tests
915+
from test.test_time import TimeTestCase
916+
917+
names = ('test_clock_settime', 'test_monotonic_settime')
918+
for resources, expected in (({}, 0), ({'settime': None}, 3), ({}, 0)):
919+
with (self.subTest(resources=resources),
920+
unittest.mock.patch.object(support, 'use_resources', resources),
921+
unittest.mock.patch.object(time, 'clock_settime') as setter):
922+
suite = unittest.TestSuite(TimeTestCase(name) for name in names)
923+
_parallelize_tests(suite, 2)
924+
result = unittest.TestResult()
925+
suite.run(result)
926+
self.assertTrue(result.wasSuccessful(), result.errors + result.failures)
927+
self.assertEqual(setter.call_count, expected)
928+
910929
def test_finds_expected_number_of_tests(self):
911930
"""
912931
Check that regrtest appears to find the expected set of tests.

‎Lib/test/test_time.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,14 @@ def test_clock_getres(self):
143143

144144
@unittest.skipUnless(hasattr(time, 'clock_settime'),
145145
'need time.clock_settime()')
146-
@support.requires_resource('settime')
146+
@support.thread_unsafe('sets the system clock')
147147
def test_clock_settime(self):
148+
support.requires('settime')
148149
t = time.clock_gettime(time.CLOCK_REALTIME)
149150
try:
150151
time.clock_settime(time.CLOCK_REALTIME, t)
151-
except PermissionError:
152-
pass
152+
except PermissionError as err:
153+
self.skipTest(err)
153154

154155
@unittest.skipUnless(hasattr(time, 'clock_settime') and
155156
hasattr(time, 'CLOCK_MONOTONIC'),
@@ -707,8 +708,9 @@ def test_thread_time(self):
707708

708709
@unittest.skipUnless(hasattr(time, 'clock_settime'),
709710
'need time.clock_settime')
710-
@support.requires_resource('settime')
711+
@support.thread_unsafe('sets the system clock')
711712
def test_monotonic_settime(self):
713+
support.requires('settime')
712714
t1 = time.monotonic()
713715
realtime = time.clock_gettime(time.CLOCK_REALTIME)
714716
# jump backward with an offset of 1 hour

0 commit comments

Comments
 (0)