Skip to content

Commit d3f8763

Browse files
authoredOct 3, 2016
Merge pull request #3564 from elpaso/test-wait-for-network3
Yet another strategy to get a free port from the server
2 parents 368c1ce + 9761a86 commit d3f8763

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
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: 12 additions & 9 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

@@ -45,14 +45,12 @@
4545
unittest,
4646
)
4747

48+
4849
try:
49-
QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT = os.environ['QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT']
50+
QGIS_SERVER_ENDPOINT_PORT = os.environ['QGIS_SERVER_ENDPOINT_PORT']
5051
except:
51-
import socket
52-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
53-
s.bind(("", 0))
54-
QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT = s.getsockname()[1]
55-
s.close()
52+
QGIS_SERVER_ENDPOINT_PORT = '0' # Auto
53+
5654

5755
QGIS_AUTH_DB_DIR_PATH = tempfile.mkdtemp()
5856

@@ -67,7 +65,7 @@ class TestAuthManager(unittest.TestCase):
6765
def setUpClass(cls):
6866
"""Run before all tests:
6967
Creates an auth configuration"""
70-
cls.port = QGIS_SERVER_AUTHMANAGER_DEFAULT_PORT
68+
cls.port = QGIS_SERVER_ENDPOINT_PORT
7169
# Clean env just to be sure
7270
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
7371
for ev in env_vars:
@@ -96,13 +94,18 @@ def setUpClass(cls):
9694
server_path = os.path.dirname(os.path.realpath(__file__)) + \
9795
'/qgis_wrapped_server.py'
9896
cls.server = subprocess.Popen([sys.executable, server_path],
99-
env=os.environ)
97+
env=os.environ, stdout=subprocess.PIPE)
98+
line = cls.server.stdout.readline()
99+
cls.port = int(re.findall(b':(\d+)', line)[0])
100+
assert cls.port != 0
101+
# Wait for the server process to start
100102
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"
101103

102104
@classmethod
103105
def tearDownClass(cls):
104106
"""Run after all tests"""
105107
cls.server.terminate()
108+
cls.server.wait()
106109
rmtree(QGIS_AUTH_DB_DIR_PATH)
107110
del cls.server
108111

‎tests/src/python/test_offline_editing_wfs.py

Lines changed: 14 additions & 16 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
@@ -47,13 +48,9 @@
4748

4849

4950
try:
50-
QGIS_SERVER_WFST_DEFAULT_PORT = os.environ['QGIS_SERVER_WFST_DEFAULT_PORT']
51+
QGIS_SERVER_OFFLINE_PORT = os.environ['QGIS_SERVER_OFFLINE_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_OFFLINE_PORT = '0' # Auto
5754

5855
qgis_app = start_app()
5956

@@ -66,7 +63,7 @@ class TestWFST(unittest.TestCase, OfflineTestBase):
6663
@classmethod
6764
def setUpClass(cls):
6865
"""Run before all tests"""
69-
cls.port = QGIS_SERVER_WFST_DEFAULT_PORT
66+
cls.port = QGIS_SERVER_OFFLINE_PORT
7067
# Create tmp folder
7168
cls.temp_path = tempfile.mkdtemp()
7269
cls.testdata_path = cls.temp_path + '/' + 'wfs_transactional' + '/'
@@ -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: 10 additions & 8 deletions
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
@@ -57,13 +58,9 @@
5758
)
5859

5960
try:
60-
QGIS_SERVER_WFST_DEFAULT_PORT = os.environ['QGIS_SERVER_WFST_DEFAULT_PORT']
61+
QGIS_SERVER_WFST_PORT = os.environ['QGIS_SERVER_WFST_PORT']
6162
except:
62-
import socket
63-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
64-
s.bind(("", 0))
65-
QGIS_SERVER_WFST_DEFAULT_PORT = s.getsockname()[1]
66-
s.close()
63+
QGIS_SERVER_WFST_PORT = '0' # Auto
6764

6865

6966
qgis_app = start_app()
@@ -74,7 +71,7 @@ class TestWFST(unittest.TestCase):
7471
@classmethod
7572
def setUpClass(cls):
7673
"""Run before all tests"""
77-
cls.port = QGIS_SERVER_WFST_DEFAULT_PORT
74+
cls.port = QGIS_SERVER_WFST_PORT
7875
# Create tmp folder
7976
cls.temp_path = tempfile.mkdtemp()
8077
cls.testdata_path = cls.temp_path + '/' + 'wfs_transactional' + '/'
@@ -98,13 +95,18 @@ def setUpClass(cls):
9895
server_path = os.path.dirname(os.path.realpath(__file__)) + \
9996
'/qgis_wrapped_server.py'
10097
cls.server = subprocess.Popen([sys.executable, server_path],
101-
env=os.environ)
98+
env=os.environ, stdout=subprocess.PIPE)
99+
line = cls.server.stdout.readline()
100+
cls.port = int(re.findall(b':(\d+)', line)[0])
101+
assert cls.port != 0
102+
# Wait for the server process to start
102103
assert waitServer('http://127.0.0.1:%s' % cls.port), "Server is not responding!"
103104

104105
@classmethod
105106
def tearDownClass(cls):
106107
"""Run after all tests"""
107108
cls.server.terminate()
109+
cls.server.wait()
108110
del cls.server
109111
# Clear all test layers
110112
for ln in ['test_point', 'test_polygon', 'test_linestring']:

0 commit comments

Comments
 (0)
Please sign in to comment.