Skip to content

Commit f571357

Browse files
committed
fix(tests): require explicit opt-in to change the system clock
Refs #162280
1 parent b09a7e9 commit f571357

5 files changed

Lines changed: 29 additions & 13 deletions

File tree

‎Lib/test/libregrtest/cmdline.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122
123123
walltime - Long running but not CPU-bound tests.
124124
125+
settime - Allow tests to change the system clock. Must be enabled
126+
explicitly, even when using `-u all`.
127+
125128
subprocess Run all tests for the subprocess module.
126129
127130
urlfetch - It is okay to download files required on testing.

‎Lib/test/libregrtest/utils.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151
# - tzdata: while needed to validate fully test_datetime, it makes
5252
# test_datetime too slow (15-20 min on some buildbots) and so is disabled by
5353
# default (see bpo-30822).
54-
RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile', 'tzdata', 'xpickle', 'wantobjects')
54+
# - settime: changing the system clock affects other processes.
55+
RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile', 'settime', 'tzdata',
56+
'xpickle', 'wantobjects')
5557

5658

5759
# Types for types hints

‎Lib/test/support/__init__.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,15 @@ def is_resource_enabled(resource):
324324
"""Test whether a resource is enabled.
325325
326326
Known resources are set by regrtest.py. If not running under regrtest.py,
327-
all resources are assumed enabled unless use_resources has been set.
327+
all resources except settime are assumed enabled unless use_resources
328+
has been set.
328329
"""
329-
return use_resources is None or resource in use_resources
330+
if use_resources is None:
331+
return resource != 'settime'
332+
return resource in use_resources
330333

331334
def get_resource_value(resource):
332-
"""Test whether a resource is enabled.
333-
334-
Known resources are set by regrtest.py. If not running under regrtest.py,
335-
all resources are assumed enabled unless use_resources has been set.
336-
"""
335+
"""Return the configured resource value, or None if no value was set."""
337336
if use_resources is None:
338337
return None
339338
return use_resources.get(resource)

‎Lib/test/test_regrtest.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ def test_use(self):
340340
self.checkError([opt, 'none=0'], 'invalid resource: none=0')
341341
self.checkError([opt, 'all,-gui=0'], 'invalid resource: -gui=0')
342342

343+
def test_settime_requires_opt_in(self):
344+
self.assertNotIn('settime', self.parse_args([]).use_resources)
345+
self.assertNotIn('settime', self.parse_args(['-u', 'all']).use_resources)
346+
self.assertIn('settime', self.parse_args(['-u', 'settime']).use_resources)
347+
343348
def test_memlimit(self):
344349
for opt in '-M', '--memlimit':
345350
with self.subTest(opt=opt):

‎Lib/test/test_time.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,20 @@ def test_clock_getres(self):
143143

144144
@unittest.skipUnless(hasattr(time, 'clock_settime'),
145145
'need time.clock_settime()')
146+
@support.requires_resource('settime')
146147
def test_clock_settime(self):
147148
t = time.clock_gettime(time.CLOCK_REALTIME)
148149
try:
149150
time.clock_settime(time.CLOCK_REALTIME, t)
150151
except PermissionError:
151152
pass
152153

153-
if hasattr(time, 'CLOCK_MONOTONIC'):
154-
self.assertRaises(OSError,
155-
time.clock_settime, time.CLOCK_MONOTONIC, 0)
154+
@unittest.skipUnless(hasattr(time, 'clock_settime') and
155+
hasattr(time, 'CLOCK_MONOTONIC'),
156+
'need clock_settime() and CLOCK_MONOTONIC')
157+
def test_clock_settime_monotonic(self):
158+
with self.assertRaises(OSError):
159+
time.clock_settime(time.CLOCK_MONOTONIC, 0)
156160

157161
def test_large_timestamp_year(self):
158162
# 2147483648-06-29 00:00:00 UTC, away from local year boundaries.
@@ -703,6 +707,7 @@ def test_thread_time(self):
703707

704708
@unittest.skipUnless(hasattr(time, 'clock_settime'),
705709
'need time.clock_settime')
710+
@support.requires_resource('settime')
706711
def test_monotonic_settime(self):
707712
t1 = time.monotonic()
708713
realtime = time.clock_gettime(time.CLOCK_REALTIME)
@@ -711,8 +716,10 @@ def test_monotonic_settime(self):
711716
time.clock_settime(time.CLOCK_REALTIME, realtime - 3600)
712717
except PermissionError as err:
713718
self.skipTest(err)
714-
t2 = time.monotonic()
715-
time.clock_settime(time.CLOCK_REALTIME, realtime)
719+
try:
720+
t2 = time.monotonic()
721+
finally:
722+
time.clock_settime(time.CLOCK_REALTIME, realtime)
716723
# monotonic must not be affected by system clock updates
717724
self.assertGreaterEqual(t2, t1)
718725

0 commit comments

Comments
 (0)