Skip to content

Commit

Permalink
test_audit: Test "time.sleep" hook raising an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
encukou committed Aug 23, 2023
1 parent aee8eb3 commit ab34b8e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,15 @@ def test_not_in_gc():
assert hook not in o


def test_time():
def test_time(mode):
import time

def hook(event, args):
if event.startswith("time."):
print(event, *args)
if mode == 'print':
print(event, *args)
elif mode == 'fail':
raise AssertionError('hook failed')
sys.addaudithook(hook)

time.sleep(0)
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def test_not_in_gc(self):
self.fail(stderr)

def test_time(self):
returncode, events, stderr = self.run_python("test_time")
returncode, events, stderr = self.run_python("test_time", "print")
if returncode:
self.fail(stderr)

Expand All @@ -270,6 +270,11 @@ def test_time(self):

self.assertEqual(actual, expected)

def test_time_fail(self):
returncode, events, stderr = self.run_python("test_time", "fail",
expect_stderr=True)
self.assertNotEqual(returncode, 0)
self.assertIn('hook failed', stderr.splitlines()[-1])

def test_sys_monitoring_register_callback(self):
returncode, events, stderr = self.run_python("test_sys_monitoring_register_callback")
Expand Down

0 comments on commit ab34b8e

Please sign in to comment.