Skip to content

Commit

Permalink
Fix log
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Mar 15, 2022
1 parent 2eca30c commit 949eda1
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions scripts/tests/run_python_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def EnqueueLogOutput(fp, tag, q):
fp.close()


def RedirectQueueThread(fp, tag, queue):
def RedirectQueueThread(fp, tag, queue) -> threading.Thread:
log_queue_thread = threading.Thread(target=EnqueueLogOutput, args=(
fp, tag, queue))
log_queue_thread.daemon = True
log_queue_thread.start()
return log_queue_thread


def DumpLogOutput(q: queue.Queue, timeout: int = 0):
Expand All @@ -88,6 +88,7 @@ def main(app: str, factoryreset: bool, wait_before_test: int, app_params: str, s
raise Exception("Failed to remove /tmp/chip* for factory reset.")

log_queue = queue.Queue()
log_cooking_threads = []

app_process = None
if app:
Expand All @@ -98,10 +99,10 @@ def main(app: str, factoryreset: bool, wait_before_test: int, app_params: str, s
logging.info(f"Execute: {app_args}")
app_process = subprocess.Popen(
app_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0)
RedirectQueueThread(app_process.stdout,
b"[\33[34mAPP \33[0m][\33[33mSTDOUT\33[0m]", log_queue)
RedirectQueueThread(app_process.stderr,
b"[\33[34mAPP \33[0m][\33[31mSTDERR\33[0m]", log_queue)
log_cooking_threads.append(RedirectQueueThread(app_process.stdout,
b"[\33[34mAPP \33[0m][\33[33mSTDOUT\33[0m]", log_queue))
log_cooking_threads.append(RedirectQueueThread(app_process.stderr,
b"[\33[34mAPP \33[0m][\33[31mSTDERR\33[0m]", log_queue))

try:
DumpLogOutput(log_queue, wait_before_test)
Expand All @@ -113,10 +114,10 @@ def main(app: str, factoryreset: bool, wait_before_test: int, app_params: str, s
logging.info(f"Execute: {script_command}")
test_script_process = subprocess.Popen(
script_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
RedirectQueueThread(test_script_process.stdout,
b"[\33[32mTEST\33[0m][\33[33mSTDOUT\33[0m]", log_queue)
RedirectQueueThread(test_script_process.stderr,
b"[\33[32mTEST\33[0m][\33[31mSTDERR\33[0m]", log_queue)
log_cooking_threads.append(RedirectQueueThread(test_script_process.stdout,
b"[\33[32mTEST\33[0m][\33[33mSTDOUT\33[0m]", log_queue))
log_cooking_threads.append(RedirectQueueThread(test_script_process.stderr,
b"[\33[32mTEST\33[0m][\33[31mSTDERR\33[0m]", log_queue))

test_script_exit_code = test_script_process.poll()
while test_script_exit_code is None:
Expand All @@ -138,6 +139,11 @@ def main(app: str, factoryreset: bool, wait_before_test: int, app_params: str, s
pass
test_app_exit_code = app_process.poll()

# There are some logs not cooked, so we wait until we have processed all logs.
# This procedure should be very fast since the related processes are finished.
for thread in log_cooking_threads:
thread.join()

try:
DumpLogOutput(log_queue)
except queue.Empty:
Expand Down

0 comments on commit 949eda1

Please sign in to comment.