test-run-dev: Fix start_server implementation.

Previously, there were following problems with the implmentation:
 * Same file handle was being used to read and write. We used to do
   `seek(0)` and then `read()`. This had a chance to overwrite file
   data. Now we use different file handles to read and write data.
 * We were using text streams. Text streams cannot be used with
   `bufferring=0`. Now we use binary streams without buffering so that
   data is available for reading without any delay.
This commit is contained in:
Umair Khan 2018-01-01 09:25:44 +05:00 committed by showell
parent cd849bc3f1
commit 45b004818b
1 changed files with 19 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import sys
import time import time
import signal import signal
import subprocess import subprocess
from typing import Tuple
# check for the venv # check for the venv
from lib import sanity_check from lib import sanity_check
@ -16,19 +17,23 @@ from typing import IO
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
def start_server(logfile): def start_server(logfile_name: str) -> Tuple[bool, str]:
# type: (IO[str]) -> bool
failure = True failure = True
key = "Quit the server with CTRL-C." key = "Quit the server with CTRL-C."
datalog = []
with open(logfile_name, 'rb', buffering=0) as logfile:
for i in range(200): for i in range(200):
time.sleep(0.5) time.sleep(0.5)
print("Polling run-dev...") print("{}. Polling run-dev...".format(i))
logfile.seek(0) new_data = logfile.read().decode()
if key in logfile.read(): if new_data:
datalog.append(new_data)
if key in ''.join(datalog):
failure = False failure = False
break break
return failure return failure, ''.join(datalog)
def test_nagios(nagios_logfile): def test_nagios(nagios_logfile):
@ -68,19 +73,20 @@ def close_and_get_content(file_handle):
if __name__ == '__main__': if __name__ == '__main__':
print("Testing development server start!") print("Testing development server start!")
logfile = open('/tmp/run-dev-output', 'w+') logfile_name = '/tmp/run-dev-output'
logfile = open(logfile_name, 'wb', buffering=0)
# nagios_logfile = open('/tmp/test-nagios-output', 'w+') # nagios_logfile = open('/tmp/test-nagios-output', 'w+')
args = ["{}/run-dev.py".format(TOOLS_DIR)] args = ["{}/run-dev.py".format(TOOLS_DIR)]
run_dev = subprocess.Popen(args, stdout=logfile, stderr=logfile) run_dev = subprocess.Popen(args, stdout=logfile, stderr=logfile)
try: try:
failure = start_server(logfile) failure, log = start_server(logfile_name)
# We have moved API to a separate repo. `test_nagios` and # We have moved API to a separate repo. `test_nagios` and
# `check_send_receive_time` need to be updated. # `check_send_receive_time` need to be updated.
# failure = failure or not test_nagios(nagios_logfile) # failure = failure or not test_nagios(nagios_logfile)
finally: finally:
log = close_and_get_content(logfile) logfile.close()
# nagios_log = close_and_get_content(nagios_logfile) # nagios_log = close_and_get_content(nagios_logfile)
run_dev.send_signal(signal.SIGINT) run_dev.send_signal(signal.SIGINT)