From 65c32ad9fbfd1b867061b83449a207b8258a0edf Mon Sep 17 00:00:00 2001 From: zainkabani <77307340+zainkabani@users.noreply.github.com> Date: Tue, 9 Aug 2022 17:09:53 -0400 Subject: [PATCH] Validates pgcat is closed after shutdown python tests (#116) * Validates pgcat is closed after shutdown python tests * Fix pgrep logic * Moves sigterm step to after cleanup to decouple * Replace subprocess with os.system for running pgcat --- tests/python/tests.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tests/python/tests.py b/tests/python/tests.py index 3ff99a0..a674cee 100644 --- a/tests/python/tests.py +++ b/tests/python/tests.py @@ -3,8 +3,6 @@ import psycopg2 import psutil import os import signal -import subprocess -from threading import Thread import time SHUTDOWN_TIMEOUT = 5 @@ -15,14 +13,18 @@ PGCAT_PORT = "6432" def pgcat_start(): pg_cat_send_signal(signal.SIGTERM) - pgcat_start_command = "./target/debug/pgcat .circleci/pgcat.toml" - subprocess.Popen(pgcat_start_command.split()) + os.system("./target/debug/pgcat .circleci/pgcat.toml &") def pg_cat_send_signal(signal: signal.Signals): for proc in psutil.process_iter(["pid", "name"]): if "pgcat" == proc.name(): os.kill(proc.pid, signal) + if signal == signal.SIGTERM: + # Returns 0 if pgcat process exists + time.sleep(2) + if not os.system('pgrep pgcat'): + raise Exception("pgcat not closed after SIGTERM") def connect_normal_db( @@ -67,8 +69,7 @@ def test_shutdown_logic(): ##### NO ACTIVE QUERIES SIGINT HANDLING ##### # Start pgcat - server = Thread(target=pgcat_start) - server.start() + pgcat_start() # Wait for server to fully start up time.sleep(2) @@ -92,12 +93,15 @@ def test_shutdown_logic(): else: # Fail if query execution succeeded raise Exception("Server not closed after sigint") + cleanup_conn(conn, cur) + pg_cat_send_signal(signal.SIGTERM) + + ##### END ##### ##### HANDLE TRANSACTION WITH SIGINT ##### # Start pgcat - server = Thread(target=pgcat_start) - server.start() + pgcat_start() # Wait for server to fully start up time.sleep(2) @@ -120,11 +124,13 @@ def test_shutdown_logic(): raise Exception("Server closed while in transaction", e.pgerror) cleanup_conn(conn, cur) + pg_cat_send_signal(signal.SIGTERM) + + ##### END ##### ##### HANDLE SHUTDOWN TIMEOUT WITH SIGINT ##### # Start pgcat - server = Thread(target=pgcat_start) - server.start() + pgcat_start() # Wait for server to fully start up time.sleep(3) @@ -151,6 +157,9 @@ def test_shutdown_logic(): raise Exception("Server not closed after sigint and expected timeout") cleanup_conn(conn, cur) + pg_cat_send_signal(signal.SIGTERM) + + ##### END ##### test_normal_db_access()