Skip to content

Commit 4b2dda3

Browse files
committed
[Test] Make timeout.py signal an error when the timeout is hit.
Print an error message and exit with a non-zero code when we hit the timeout. This makes it clear when a test fails due to a timeout. On Darwin, run `sample` on the target process first, so that the failure includes some information about what the test was doing when the timeout occurred.
1 parent 4987c3b commit 4b2dda3

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

test/Inputs/timeout.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
#!/uar/bin/env python3
22

3+
import platform
34
import subprocess
45
import sys
5-
import threading
66

7+
sampleCommand = None
8+
timeoutSampleTime = 0
79

8-
def watchdog(command, timeout=None):
10+
if platform.system() == 'Darwin':
11+
sampleCommand = '/usr/bin/sample'
12+
timeoutSampleTime = 10
13+
14+
15+
def watchdog(command, timeout):
916
process = subprocess.Popen(command)
10-
timer = threading.Timer(timeout, process.kill)
1117
try:
12-
timer.start()
13-
process.communicate()
14-
finally:
15-
timer.cancel()
18+
process.wait(timeout=timeout)
19+
except subprocess.TimeoutExpired:
20+
if sampleCommand:
21+
pidstr = str(process.pid)
22+
subprocess.run([sampleCommand, pidstr, str(timeoutSampleTime)])
23+
process.kill()
24+
sys.exit(
25+
'error: command timed out after {} seconds: {}'
26+
.format(timeout, ' '.join(sys.argv[2:])))
1627

1728

1829
if __name__ == '__main__':

0 commit comments

Comments
 (0)