From efc17aef8c0517eef7727dc9d9c6760ea5d89acd Mon Sep 17 00:00:00 2001 From: Nolan Eastin Date: Thu, 27 Mar 2025 20:29:16 -0700 Subject: [PATCH] fix: Let OS select an available port when running TestInstalledAppFlow tests.unit.test_flow.TestInstalledAppFlow attempts to create a new server at the specified host:port. New server creation occasionally results in 'address already in use' because the socket will be unavailable for a period of time after the socket is closed, resulting in flaky test failures. Work around this in the tests by letting the OS pick an available port each time. Fixes #381 --- tests/unit/test_flow.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/tests/unit/test_flow.py b/tests/unit/test_flow.py index 5941bb9..73cd569 100644 --- a/tests/unit/test_flow.py +++ b/tests/unit/test_flow.py @@ -18,7 +18,6 @@ import json import os import re -import random import socket import mock @@ -251,25 +250,15 @@ def instance(self): CLIENT_SECRETS_INFO, scopes=self.SCOPES ) - def is_port_in_use(self, port, host="localhost"): - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - return s.connect_ex((host, port)) == 0 - @pytest.fixture def port(self): # Creating a new server at the same port will result in # a 'Address already in use' error for a brief # period of time after the socket has been closed. - # Work around this in the tests by choosing a different port each time. - # https://stackoverflow.com/questions/6380057/python-binding-socket-address-already-in-use - random_port = -1 - for _ in range(10): - random_port = random.randrange(60400, 60900) - if not self.is_port_in_use(random_port): - break - else: - raise OSError("Could not find a free port") - yield random_port + # Work around this in the tests by letting the OS pick an available port each time. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("localhost", 0)) + return s.getsockname()[1] @pytest.fixture def socket(self, port):