Skip to content

Commit

Permalink
fix: add proper kill process to conftest. #249
Browse files Browse the repository at this point in the history
* fix: fix problem of process persistence post test session
* replace process.terminate() with kill() equivalent
* scope pytest fixtures to session level
  • Loading branch information
guilefoylegaurav committed Sep 16, 2022
1 parent 484a537 commit e7a78eb
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions integration_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@
import signal
import sys
from typing import List
import pytest
import subprocess
import pathlib
import os
import time

def spawn_process(command: List[str]) -> subprocess.Popen:
if sys.platform.startswith("win32"):
command[0] = "python"
process = subprocess.Popen(command, shell=True, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
return process
process = subprocess.Popen(command, preexec_fn=os.setsid)
return process


@pytest.fixture

def kill_process(process: subprocess.Popen) -> None:
if sys.platform.startswith("win32"):
process.send_signal(signal.CTRL_BREAK_EVENT)
process.kill()
return
os.killpg(os.getpgid(process.pid), signal.SIGKILL)




@pytest.fixture(scope="session")
def session():
os.environ["ROBYN_URL"] = "127.0.0.1"
current_file_path = pathlib.Path(__file__).parent.resolve()
base_routes = os.path.join(current_file_path, "./base_routes.py")
process = subprocess.Popen(["python3", base_routes])
command = ["python3", base_routes]
process = spawn_process(command)
time.sleep(5)
yield
process.terminate()
kill_process(process)




@pytest.fixture
@pytest.fixture(scope="session")
def default_session():
current_file_path = pathlib.Path(__file__).parent.resolve()
base_routes = os.path.join(current_file_path, "./base_routes.py")
process = subprocess.Popen(["python3", base_routes])
command = ["python3", base_routes]
process = spawn_process(command)
time.sleep(5)
yield
process.terminate()
kill_process(process)


@pytest.fixture
@pytest.fixture(scope="session")
def global_session():
os.environ["ROBYN_URL"] = "0.0.0.0"
current_file_path = pathlib.Path(__file__).parent.resolve()
base_routes = os.path.join(current_file_path, "./base_routes.py")
process = subprocess.Popen(["python3", base_routes])
command = ["python3", base_routes]
process = spawn_process(command)
time.sleep(1)
yield
process.terminate()
kill_process(process)


@pytest.fixture(scope="session")
Expand All @@ -43,8 +70,8 @@ def dev_session():
os.environ["ROBYN_PORT"] = "5001"
current_file_path = pathlib.Path(__file__).parent.resolve()
base_routes = os.path.join(current_file_path, "./base_routes.py")
process = subprocess.Popen(["python3", base_routes, "--dev"])
command = ["python3", base_routes, "--dev"]
process = spawn_process(command)
time.sleep(5)
yield
process.terminate()

kill_process(process)

0 comments on commit e7a78eb

Please sign in to comment.