summaryrefslogtreecommitdiff
path: root/tests/integration/harness.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/harness.py')
-rw-r--r--tests/integration/harness.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/tests/integration/harness.py b/tests/integration/harness.py
index 122c36d..b98ce83 100644
--- a/tests/integration/harness.py
+++ b/tests/integration/harness.py
@@ -49,12 +49,26 @@ def bdecode(data):
49 return value(0)[1] 49 return value(0)[1]
50 50
51 51
52def free_port(): 52def free_port(attempts=50):
53 probe = socket.socket() 53 """A port free for both protocols, because the tracker binds both.
54 probe.bind(("127.0.0.1", 0)) 54
55 port = probe.getsockname()[1] 55 Asking the kernel for a free tcp port says nothing about the udp port of
56 probe.close() 56 the same number, and anything on the machine holding that one makes the
57 return port 57 tracker exit at once with "Address already in use".
58 """
59 for _ in range(attempts):
60 tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
61 udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
62 try:
63 tcp.bind(("127.0.0.1", 0))
64 udp.bind(("127.0.0.1", tcp.getsockname()[1]))
65 return tcp.getsockname()[1]
66 except OSError:
67 continue
68 finally:
69 tcp.close()
70 udp.close()
71 raise AssertionError("no port free for both tcp and udp after %d tries" % attempts)
58 72
59 73
60class Tracker: 74class Tracker: