Skip to content

Commit d950e3b

Browse files
committed
Yet another strategy to get a free port from the server
1 parent 368c1ce commit d950e3b

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

tests/src/python/qgis_wrapped_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ def do_POST(self):
100100

101101

102102
if __name__ == '__main__':
103-
print('Starting server on %s:%s, use <Ctrl-C> to stop' %
104-
(QGIS_SERVER_HOST, QGIS_SERVER_PORT))
105103
server = HTTPServer((QGIS_SERVER_HOST, QGIS_SERVER_PORT), Handler)
104+
print('Starting server on %s:%s, use <Ctrl-C> to stop' %
105+
(QGIS_SERVER_HOST, server.server_port), flush=True)
106106

107107
def signal_handler(signal, frame):
108108
global qgs_app

tests/src/python/test_authmanager_endpoint.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""
1818
import os
1919
import sys
20+
import re
2021
import subprocess
2122
import tempfile
2223
import random
@@ -29,7 +30,6 @@
2930
# This will get replaced with a git SHA1 when you do a git archive
3031
__revision__ = '$Format:%H$'
3132

32-
from time import sleep
3333
from urllib.parse import quote
3434
from shutil import rmtree
3535

@@ -96,13 +96,18 @@ def setUpClass(cls):
9696
server_path = os.path.dirname(os.path.realpath(__file__)) + \
9797
'/qgis_wrapped_server.py'
9898
cls.server = subprocess.Popen([sys.executable, server_path],
99-
env=os.environ)
99+
env=os.environ, stdout=subprocess.PIPE)
100+
line = cls.server.stdout.readline()
101+
cls.port = int(re.findall(b':(\d+)', line)[0])
102+
assert cls.port != 0
103+
# Wait for the server process to start
100104
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"
101105

102106
@classmethod
103107
def tearDownClass(cls):
104108
"""Run after all tests"""
105109
cls.server.terminate()
110+
cls.server.wait()
106111
rmtree(QGIS_AUTH_DB_DIR_PATH)
107112
del cls.server
108113

tests/src/python/test_offline_editing_wfs.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import os
3333
import sys
34+
import re
3435
import subprocess
3536
from shutil import copytree, rmtree
3637
import tempfile
@@ -49,11 +50,7 @@
4950
try:
5051
QGIS_SERVER_WFST_DEFAULT_PORT = os.environ['QGIS_SERVER_WFST_DEFAULT_PORT']
5152
except:
52-
import socket
53-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
54-
s.bind(("", 0))
55-
QGIS_SERVER_WFST_DEFAULT_PORT = s.getsockname()[1]
56-
s.close()
53+
QGIS_SERVER_WFST_DEFAULT_PORT = '0' # Auto
5754

5855
qgis_app = start_app()
5956

@@ -97,7 +94,10 @@ def tearDownClass(cls):
9794
def setUp(self):
9895
"""Run before each test."""
9996
self.server = subprocess.Popen([sys.executable, self.server_path],
100-
env=os.environ)
97+
env=os.environ, stdout=subprocess.PIPE)
98+
line = self.server.stdout.readline()
99+
self.port = int(re.findall(b':(\d+)', line)[0])
100+
assert self.port != 0
101101
# Wait for the server process to start
102102
assert waitServer('http://127.0.0.1:%s' % self.port), "Server is not responding!"
103103
self._setUp()
@@ -108,15 +108,13 @@ def tearDown(self):
108108
self._clearLayer(self._getOnlineLayer('test_point'))
109109
# Kill the server
110110
self.server.terminate()
111+
self.server.wait()
111112
del self.server
112-
# Wait for the server process to stop
113-
sleep(2)
114113
# Delete the sqlite db
115114
os.unlink(os.path.join(self.temp_path, 'offlineDbFile.sqlite'))
116115
self._tearDown()
117116

118-
@classmethod
119-
def _getOnlineLayer(cls, type_name, layer_name=None):
117+
def _getOnlineLayer(self, type_name, layer_name=None):
120118
"""
121119
Return a new WFS layer, overriding the WFS cache
122120
"""
@@ -125,14 +123,14 @@ def _getOnlineLayer(cls, type_name, layer_name=None):
125123
parms = {
126124
'srsname': 'EPSG:4326',
127125
'typename': type_name,
128-
'url': 'http://127.0.0.1:%s/%s/?map=%s' % (cls.port,
129-
cls.counter,
130-
cls.project_path),
126+
'url': 'http://127.0.0.1:%s/%s/?map=%s' % (self.port,
127+
self.counter,
128+
self.project_path),
131129
'version': 'auto',
132130
'table': '',
133131
#'sql': '',
134132
}
135-
cls.counter += 1
133+
self.counter += 1
136134
uri = ' '.join([("%s='%s'" % (k, v)) for k, v in list(parms.items())])
137135
wfs_layer = QgsVectorLayer(uri, layer_name, 'WFS')
138136
assert wfs_layer.isValid()

tests/src/python/test_qgsserver_wfst.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import os
3939
import sys
40+
import re
4041
import subprocess
4142
from shutil import copytree, rmtree
4243
import tempfile
@@ -98,13 +99,18 @@ def setUpClass(cls):
9899
server_path = os.path.dirname(os.path.realpath(__file__)) + \
99100
'/qgis_wrapped_server.py'
100101
cls.server = subprocess.Popen([sys.executable, server_path],
101-
env=os.environ)
102+
env=os.environ, stdout=subprocess.PIPE)
103+
line = cls.server.stdout.readline()
104+
cls.port = int(re.findall(b':(\d+)', line)[0])
105+
assert cls.port != 0
106+
# Wait for the server process to start
102107
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"
103108

104109
@classmethod
105110
def tearDownClass(cls):
106111
"""Run after all tests"""
107112
cls.server.terminate()
113+
cls.server.wait()
108114
del cls.server
109115
# Clear all test layers
110116
for ln in ['test_point', 'test_polygon', 'test_linestring']:

0 commit comments

Comments
 (0)