Skip to content

Commit

Permalink
update tests CONNECT_TIMEOUT, add fallback clear process for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
voidZXL committed Nov 18, 2024
1 parent 4506b0f commit 42d8794
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from utilmeta import UtilMeta
from typing import Union
from pathlib import Path
from utilmeta.utils import ignore_errors
# from django import VERSION as DJANGO_VERSION
TEST_PATH = os.path.dirname(__file__)
SERVICE_PATH = os.path.join(TEST_PATH, 'server')
PARAMETRIZE_CONFIG = False
CONNECT_TIMEOUT = 3
CONNECT_TIMEOUT = 15
CONNECT_INTERVAL = 0.2
WINDOWS = os.name == 'nt'

Expand Down Expand Up @@ -297,6 +298,18 @@ def run_server(backend: str = None, port: int = None, asynchronous: bool = None)
cleanup()


@ignore_errors
def clear_server_process(server):
kill_child_processes(server.pid, signal.SIGTERM if WINDOWS else signal.SIGKILL)
if WINDOWS:
try:
os.kill(server.pid, signal.SIGBREAK)
except (PermissionError, OSError, WindowsError):
pass
else:
server.terminate()


def make_live_process(backend: str = None, port: int = None, cmdline: bool = False):
@pytest.fixture(scope="module")
def service_process(service: UtilMeta):
Expand Down Expand Up @@ -336,28 +349,20 @@ def service_process(service: UtilMeta):
server.start()
#

import socket
cnt = 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
while True:
if s.connect_ex((service.host, service.port)) == 0:
break
time.sleep(CONNECT_INTERVAL)
cnt += 1
if cnt > (CONNECT_TIMEOUT / CONNECT_INTERVAL):
return

try:
import socket
cnt = 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
while True:
if s.connect_ex((service.host, service.port)) == 0:
break
time.sleep(CONNECT_INTERVAL)
cnt += 1
if cnt > (CONNECT_TIMEOUT / CONNECT_INTERVAL):
return
yield server
finally:
kill_child_processes(server.pid, signal.SIGTERM if WINDOWS else signal.SIGKILL)
if WINDOWS:
try:
os.kill(server.pid, signal.SIGBREAK)
except (PermissionError, OSError, WindowsError):
pass
else:
server.terminate()
clear_server_process(server)
return service_process


Expand All @@ -378,28 +383,20 @@ def command_process():
os.environ.pop('DJANGO_SETTINGS_MODULE')
server = subprocess.Popen(cmd, env=os.environ.copy(), cwd=str(cwd or os.getcwd()))

if port:
import socket
cnt = 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
while True:
if s.connect_ex(('127.0.0.1', port)) == 0:
break
time.sleep(CONNECT_INTERVAL)
cnt += 1
if cnt > (CONNECT_TIMEOUT / CONNECT_INTERVAL):
return

try:
if port:
import socket
cnt = 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
while True:
if s.connect_ex(('127.0.0.1', port)) == 0:
break
time.sleep(CONNECT_INTERVAL)
cnt += 1
if cnt > (CONNECT_TIMEOUT / CONNECT_INTERVAL):
return
yield server
finally:
kill_child_processes(server.pid, signal.SIGTERM if WINDOWS else signal.SIGKILL)
if WINDOWS:
try:
os.kill(server.pid, signal.SIGBREAK)
except (PermissionError, OSError, WindowsError):
pass
else:
server.terminate()
clear_server_process(server)

return command_process

0 comments on commit 42d8794

Please sign in to comment.