From 75d92c89ec86da5cfc99d711a2b80cd097e7c6fa Mon Sep 17 00:00:00 2001 From: U2FsdGVkX1 Date: Fri, 22 Nov 2024 14:32:42 +0800 Subject: [PATCH] Add retry for request_response_cycle (https://github.com/pytest-dev/pytest-xprocess/issues/154) --- CHANGELOG.rst | 7 +++++++ tests/test_process_initialization.py | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f161578..b734374 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,10 @@ +1.0.2 (2024-11-22) +------------------ + +- Add retry for request_response_cycle. Fixed + https://github.com/pytest-dev/pytest-xprocess/issues/154 + + 1.0.1 (2024-04-31) ------------------ diff --git a/tests/test_process_initialization.py b/tests/test_process_initialization.py index 03a7c49..7aee234 100644 --- a/tests/test_process_initialization.py +++ b/tests/test_process_initialization.py @@ -1,5 +1,6 @@ import socket import sys +import time from pathlib import Path import pytest @@ -12,11 +13,18 @@ def request_response_cycle(tcp_port, data): """test started server instance by sending request and checking response""" - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: - sock.connect(("localhost", tcp_port)) - sock.sendall(bytes(data, "utf-8")) - received = str(sock.recv(1024), "utf-8") - return received == data.upper() + for attempt in range(4): + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.connect(("localhost", tcp_port)) + sock.sendall(bytes(data, "utf-8")) + received = str(sock.recv(1024), "utf-8") + return received == data.upper() + except socket.error as e: + if attempt < 3: + time.sleep(1) + else: + raise e @pytest.mark.parametrize("proc_name", ["s1", "s2", "s3"])