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
import itertools
import os
import signal
import subprocess
import sys
import time
import types
from typing import Tuple
from lib import sanity_check
@ -12,36 +13,40 @@ sanity_check.check_venv(__file__)
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
def start_server(logfile_name: str) -> Tuple[bool, str]:
failure = True
key = "Quit the server with CTRL-C."
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)
def start_server(run_dev: subprocess.Popen) -> Tuple[bool, str]:
failure = False
i = 0
if key in ''.join(datalog):
failure = False
break
def on_timer(signum: int, frame: types.FrameType) -> None:
nonlocal failure, i
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__':
print("Testing development server start!")
logfile_name = '/tmp/run-dev-output'
with open(logfile_name, 'wb', buffering=0) as logfile:
run_dev = subprocess.Popen(
with subprocess.Popen(
[os.path.join(TOOLS_DIR, "run-dev.py")],
stdout=logfile, stderr=subprocess.STDOUT)
failure, log = start_server(logfile_name)
run_dev.send_signal(signal.SIGINT)
run_dev.wait()
bufsize=1, # line buffered
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
universal_newlines=True) as run_dev:
failure, log = start_server(run_dev)
if 'Traceback' in log:
failure = True