Skip to content

Commit b70d148

Browse files
committed
Eliminate find_port() and check_port() functions
Now these functions are not needed anymore due to added free port auto resolving mechanism. Closes #141
1 parent 2da6bb0 commit b70d148

File tree

2 files changed

+1
-78
lines changed

2 files changed

+1
-78
lines changed

lib/utils.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import sys
44
import collections
55
import signal
6-
import random
76
import fcntl
87
import difflib
98
import time
109
import json
1110
import subprocess
12-
from gevent import socket
1311
from lib.colorer import color_stdout
1412

1513
try:
@@ -85,63 +83,6 @@ def print_tail_n(filename, num_lines=None):
8583
color_stdout(line, schema='tail')
8684

8785

88-
def check_port(port, rais=True, ipv4=True, ipv6=True):
89-
""" True -- it's possible to listen on this port for TCP/IPv4 or TCP/IPv6
90-
connections (UNIX Sockets in case of file path). False -- otherwise.
91-
"""
92-
try:
93-
if isinstance(port, integer_types):
94-
if ipv4:
95-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
96-
sock.bind(('127.0.0.1', port))
97-
sock.listen(5)
98-
sock.close()
99-
if ipv6:
100-
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
101-
sock.bind(('::1', port))
102-
sock.listen(5)
103-
sock.close()
104-
else:
105-
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
106-
sock.connect(port)
107-
except socket.error:
108-
if rais:
109-
raise RuntimeError(
110-
"The server is already running on port {0}".format(port))
111-
return False
112-
return True
113-
114-
115-
# A list of ports used so far. Avoid reusing ports
116-
# to reduce race conditions between starting and stopping servers.
117-
# We're using tarantoolctl for instance control, and it reports
118-
# a successful stop of the server before it really closes its
119-
# network sockets
120-
ports = {}
121-
122-
123-
is_ipv6_supported = check_port(port=0, rais=False, ipv4=False, ipv6=True)
124-
125-
126-
def find_port():
127-
global ports
128-
start_port = int(os.environ.get('TEST_RUN_TCP_PORT_START', '3000'))
129-
end_port = int(os.environ.get('TEST_RUN_TCP_PORT_END', '65535'))
130-
port = random.randrange(start_port, end_port + 1)
131-
132-
while port <= end_port:
133-
is_free = check_port(port, False, ipv4=True, ipv6=is_ipv6_supported)
134-
if port not in ports and is_free:
135-
ports[port] = True
136-
return port
137-
port += 1
138-
139-
# We've made a full circle, clear the list of used ports and start
140-
# from scratch
141-
ports = {}
142-
return find_port()
143-
144-
14586
def find_in_path(name):
14687
path = os.curdir + os.pathsep + os.environ["PATH"]
14788
for _dir in path.split(os.pathsep):

test/unittest/test_lib_utils.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
import socket
21
import unittest
32

4-
from hypothesis import given, settings
5-
from hypothesis.strategies import integers
6-
73
import lib.utils as utils
84

5+
96
class TestUtils(unittest.TestCase):
107
def test_extract_schema_from_snapshot(self):
118
snapshot_path = 'test/unittest/00000000000000000003.snap'
129
v = utils.extract_schema_from_snapshot(snapshot_path)
1310
self.assertEqual(v, (2, 3, 1))
1411

15-
@settings(max_examples=5)
16-
@given(port=integers(65100, 65535))
17-
def test_check_port(self, port):
18-
def open_socket(p):
19-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
20-
s.bind(('localhost', p))
21-
s.listen(0)
22-
return s
23-
status = utils.check_port(port, rais=False, ipv4=True, ipv6=False)
24-
self.assertEqual(status, True)
25-
s = open_socket(port)
26-
status = utils.check_port(port, rais=False, ipv4=True, ipv6=False)
27-
s.close()
28-
self.assertEqual(status, False)
29-
3012

3113
if __name__ == "__main__":
3214
unittest.main()

0 commit comments

Comments
 (0)