summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_time.py
diff options
context:
space:
mode:
authorAntoine Pitrou <pitrou@free.fr>2017-11-15 21:52:21 (GMT)
committerGitHub <noreply@github.com>2017-11-15 21:52:21 (GMT)
commit4bd41c9b52ea0c730e9e294caaf003e54c088c6e (patch)
tree9bb2c82cd67af88b9a3990ef081dd4b2c2f668c8 /Lib/test/test_time.py
parent762b9571c9c8c6b036f1bf90140a1d030b3f9a01 (diff)
downloadcpython-4bd41c9b52ea0c730e9e294caaf003e54c088c6e.zip
cpython-4bd41c9b52ea0c730e9e294caaf003e54c088c6e.tar.gz
cpython-4bd41c9b52ea0c730e9e294caaf003e54c088c6e.tar.bz2
bpo-32025: Add time.thread_time() (#4410)
* bpo-32025: Add time.thread_time() * Add missing #endif * Add NEWS blurb * Add docs and whatsnew * Address review comments * Review comments
Diffstat (limited to 'Lib/test/test_time.py')
-rw-r--r--Lib/test/test_time.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index b44646d..eda3885 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -47,6 +47,12 @@ ROUNDING_MODES = (
)
+def busy_wait(duration):
+ deadline = time.monotonic() + duration
+ while time.monotonic() < deadline:
+ pass
+
+
class TimeTestCase(unittest.TestCase):
def setUp(self):
@@ -81,6 +87,10 @@ class TimeTestCase(unittest.TestCase):
check_ns(time.process_time(),
time.process_time_ns())
+ if hasattr(time, 'thread_time'):
+ check_ns(time.thread_time(),
+ time.thread_time_ns())
+
if hasattr(time, 'clock_gettime'):
check_ns(time.clock_gettime(time.CLOCK_REALTIME),
time.clock_gettime_ns(time.CLOCK_REALTIME))
@@ -486,10 +496,57 @@ class TimeTestCase(unittest.TestCase):
# on Windows
self.assertLess(stop - start, 0.020)
+ # process_time() should include CPU time spent in any thread
+ start = time.process_time()
+ busy_wait(0.100)
+ stop = time.process_time()
+ self.assertGreaterEqual(stop - start, 0.020) # machine busy?
+
+ t = threading.Thread(target=busy_wait, args=(0.100,))
+ start = time.process_time()
+ t.start()
+ t.join()
+ stop = time.process_time()
+ self.assertGreaterEqual(stop - start, 0.020) # machine busy?
+
info = time.get_clock_info('process_time')
self.assertTrue(info.monotonic)
self.assertFalse(info.adjustable)
+ def test_thread_time(self):
+ if not hasattr(time, 'thread_time'):
+ if sys.platform.startswith(('linux', 'win')):
+ self.fail("time.thread_time() should be available on %r"
+ % (sys.platform,))
+ else:
+ self.skipTest("need time.thread_time")
+
+ # thread_time() should not include time spend during a sleep
+ start = time.thread_time()
+ time.sleep(0.100)
+ stop = time.thread_time()
+ # use 20 ms because thread_time() has usually a resolution of 15 ms
+ # on Windows
+ self.assertLess(stop - start, 0.020)
+
+ # thread_time() should include CPU time spent in current thread...
+ start = time.thread_time()
+ busy_wait(0.100)
+ stop = time.thread_time()
+ self.assertGreaterEqual(stop - start, 0.020) # machine busy?
+
+ # ...but not in other threads
+ t = threading.Thread(target=busy_wait, args=(0.100,))
+ start = time.thread_time()
+ t.start()
+ t.join()
+ stop = time.thread_time()
+ self.assertLess(stop - start, 0.020)
+
+ info = time.get_clock_info('thread_time')
+ self.assertTrue(info.monotonic)
+ self.assertFalse(info.adjustable)
+
@unittest.skipUnless(hasattr(time, 'clock_settime'),
'need time.clock_settime')
def test_monotonic_settime(self):