You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jd...@apache.org on 2022/01/30 17:05:37 UTC

[qpid-dispatch] 01/09: DISPATCH-2323 Try binding to a port before handing it out as "free"

This is an automated email from the ASF dual-hosted git repository.

jdanek pushed a commit to branch jd_tryout
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git

commit 59095c5c0da6d0bc66204e02a599ff6d64f3a689
Author: Jiri Daněk <jd...@redhat.com>
AuthorDate: Sun Jan 30 01:31:33 2022 +0100

    DISPATCH-2323 Try binding to a port before handing it out as "free"
---
 tests/system_test.py | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/tests/system_test.py b/tests/system_test.py
index 280f0a3..46a7eb5 100755
--- a/tests/system_test.py
+++ b/tests/system_test.py
@@ -175,19 +175,37 @@ def get_local_host_socket(protocol_family='IPv4'):
     return s, host
 
 
-def port_available(port, protocol_family='IPv4'):
+def port_refuses_connection(port, protocol_family='IPv4'):
     """Return true if connecting to host:port gives 'connection refused'."""
     s, host = get_local_host_socket(protocol_family)
-    available = False
     try:
         s.connect((host, port))
-    except socket.error as e:
-        available = e.errno == errno.ECONNREFUSED
-    except:
-        pass
+    except OSError as e:
+        return e.errno == errno.ECONNREFUSED
+    finally:
+        s.close()
+
+    return False
+
 
-    s.close()
-    return available
+def port_permits_binding(port, protocol_family='IPv4'):
+    """Return true if binding to the port succeeds."""
+    s, _ = get_local_host_socket(protocol_family)
+    host = ""
+    try:
+        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # so that followup binders are not blocked
+        s.bind((host, port))
+    except OSError:
+        return False
+    finally:
+        s.close()
+
+    return True
+
+
+def port_available(port, protocol_family='IPv4'):
+    """Return true if a new server will be able to bind to the port."""
+    return port_refuses_connection(port, protocol_family) and port_permits_binding(port, protocol_family)
 
 
 def wait_port(port, protocol_family='IPv4', **retry_kwargs):

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org