Files

57 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Exercise startup retry limits and process ownership without ROS or Gazebo."""
from pathlib import Path
import subprocess
import sys
import tempfile
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / '_internal'))
from run_nav2 import stop_owned, supervise
def CHECK(condition, message):
if not condition:
raise RuntimeError(message)
def main():
with tempfile.TemporaryDirectory(prefix='nav2-supervisor-test-') as directory:
root = Path(directory)
navigation = root/'navigation.py'
navigation.write_text('''import signal,sys,time
signal.signal(signal.SIGINT, lambda *args: sys.exit(0))
signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
time.sleep(1)
sys.exit(int(sys.argv[1]))
''')
checker = root/'checker.py'
checker.write_text('''import json,sys
from pathlib import Path
counter=Path(sys.argv[1]);mode=sys.argv[2]
n=int(counter.read_text())+1 if counter.exists() else 1
counter.write_text(str(n))
passed=mode=='ready' or mode=='recover' and n==2
retryable=mode in ('recover','exhaust')
Path(sys.argv[sys.argv.index('--result')+1]).write_text(json.dumps({'passed':passed,'retryable':retryable}))
sys.exit(0 if passed else 1)
''')
unrelated = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(120)'],
start_new_session=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
for mode, expected_calls, nav_exit, expected_result in [
('recover', 2, 0, 0), ('exhaust', 3, 0, 1),
('unknown', 1, 0, 1), ('ready', 1, 1, 1)]:
counter = root/(mode+'.count')
result = supervise([sys.executable, str(navigation), str(nav_exit)],
[sys.executable, str(checker), str(counter), mode], 3, 2)
CHECK(result == expected_result and int(counter.read_text()) == expected_calls,
f'{mode}: wrong retry count/result')
CHECK(unrelated.poll() is None, 'unrelated process was stopped')
finally:
stop_owned(unrelated)
print('PASS: 5 supervisor checks (recovery, limit, unknown/active refusal, no retry after ready, process ownership)')
if __name__ == '__main__':
main()