Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/libregrtest/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/libregrtest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 68 additions & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tempfile
import threading
import textwrap
import time
import types
import unittest
import unittest.mock
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
35 changes: 25 additions & 10 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading