test-run-dev: Use a pipe rather than polling a log file.

This avoids unnecessary polling delays, quadratic string operations,
and hardcoded paths in /tmp.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2019-01-14 16:17:04 -08:00 committed by Tim Abbott
parent 54d92446db
commit 19429c3ad7
1 changed files with 29 additions and 24 deletions

View File

@ -1,10 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import itertools
import os import os
import signal import signal
import subprocess import subprocess
import sys import sys
import time import types
from typing import Tuple from typing import Tuple
from lib import sanity_check from lib import sanity_check
@ -12,36 +13,40 @@ sanity_check.check_venv(__file__)
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
def start_server(logfile_name: str) -> Tuple[bool, str]: def start_server(run_dev: subprocess.Popen) -> Tuple[bool, str]:
failure = True failure = False
key = "Quit the server with CTRL-C." i = 0
datalog = []
with open(logfile_name, 'rb', buffering=0) as logfile:
for i in range(200):
time.sleep(0.5)
print("{}. Polling run-dev...".format(i))
new_data = logfile.read().decode()
if new_data:
datalog.append(new_data)
if key in ''.join(datalog): def on_timer(signum: int, frame: types.FrameType) -> None:
failure = False nonlocal failure, i
break print("{}. Polling run-dev...".format(i))
i += 1
if i == 200:
failure = True
run_dev.send_signal(signal.SIGINT)
signal.setitimer(signal.ITIMER_REAL, 0, 0)
return failure, ''.join(datalog) key = "Quit the server with CTRL-C.\n"
old_handler = signal.signal(signal.SIGALRM, on_timer)
signal.setitimer(signal.ITIMER_REAL, 0.5, 0.5)
log1, log2 = itertools.tee(run_dev.stdout)
if key not in log1:
failure = True
elif not failure:
run_dev.send_signal(signal.SIGINT)
signal.setitimer(signal.ITIMER_REAL, 0, 0)
signal.signal(signal.SIGALRM, old_handler)
return failure, ''.join(log2)
if __name__ == '__main__': if __name__ == '__main__':
print("Testing development server start!") print("Testing development server start!")
logfile_name = '/tmp/run-dev-output' with subprocess.Popen(
with open(logfile_name, 'wb', buffering=0) as logfile:
run_dev = subprocess.Popen(
[os.path.join(TOOLS_DIR, "run-dev.py")], [os.path.join(TOOLS_DIR, "run-dev.py")],
stdout=logfile, stderr=subprocess.STDOUT) bufsize=1, # line buffered
failure, log = start_server(logfile_name) stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
universal_newlines=True) as run_dev:
run_dev.send_signal(signal.SIGINT) failure, log = start_server(run_dev)
run_dev.wait()
if 'Traceback' in log: if 'Traceback' in log:
failure = True failure = True